using CollaborativePlatformMain.DFEntity.MessageSubUtil;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CollaborativePlatformMain.Util
{
    /// <summary>
    /// 
    /// <para>文件名(File Name):     CreatLevelUtil.cs</para>
    /// 
    /// <para>描述(Description):     创建标高工具类</para>
    /// 
    /// <para>数据表(Tables):        nothing</para>
    /// 
    /// <para>作者(Author):          Ou Rui Song</para>
    /// 
    /// <para>日期(Create Date):     2024年4月28日15:50:23</para>
    /// 
    /// 修改记录(Revision History):
    ///     R1:
    ///         修改作者:
    ///         修改日期:
    ///         修改理由:
    /// 
    /// </summary>
    public class CreatLevelUtil
    {


        /// <summary>
        /// 添加标高楼层
        /// </summary>
        /// <param name="levelTableEntities">标高数据</param>
        /// <param name="levelHight">标高高度</param>
        /// <param name="levelNum">标高个数</param>
        /// <param name="updw">地上、地下</param>
        public static void AddLevelFool(ref ObservableCollection<LevelTableEntity> levelTableEntities, string levelHight, string levelNum, string updw)
        {
            //添加的层高
            double levelh = Convert.ToInt32(levelHight);
            levelTableEntities = new ObservableCollection<LevelTableEntity>(levelTableEntities.OrderBy(x => x.FloorHeight));
            if (levelTableEntities.Count > 1)
            {
                levelTableEntities.First().FloorHeight = levelh;
            }
            levelTableEntities = new ObservableCollection<LevelTableEntity>(levelTableEntities.OrderBy(x => x.StructuralElevation));
            //个数
            int num = int.Parse(levelNum);


            Regex reg = new Regex(@"^(\-|\+)?\d+(\.\d+)?$");
            if (updw.Equals("地上"))
            {
                LevelTableEntity max = levelTableEntities.Last();
                for (int i = 0; i < num; i++)
                {
                    string newNmae = "";
                    if (reg.IsMatch(max.LevelNmae))
                    {
                        newNmae = (double.Parse(max.LevelNmae) + (i + 1)) + "";
                    }
                    else
                    {
                        newNmae = max.LevelNmae;

                    }
                    double structuralElevation = max.FloorHeight / 1000 + max.StructuralElevation;
                    double sum = Math.Round(structuralElevation + levelh / 1000 * (i), 2);

                    LevelTableEntity levelTableCreationEntity = new LevelTableEntity(Guid.NewGuid().ToString(), newNmae, sum, levelh);
                    if (i == num - 1)
                    {
                        levelTableCreationEntity.FloorHeight = 0;
                    }
                    levelTableEntities.Insert(levelTableEntities.Count - 1, levelTableCreationEntity);

                }

            }
            else
            {

                LevelTableEntity min = levelTableEntities.FirstOrDefault();

                for (int i = 0; i < num; i++)
                {
                    string newNmae = "";
                    if (reg.IsMatch(min.LevelNmae))
                    {
                        double nundex = double.Parse(min.LevelNmae);
                        if (nundex == 1)
                        {
                            nundex = 0;
                        }
                        newNmae = (nundex - (i + 1)) + "";
                    }
                    else
                    {
                        newNmae = min.LevelNmae;

                    }

                    double sum = Math.Round(min.StructuralElevation - (levelh * (i + 1)) / 1000, 2);

                    LevelTableEntity levelTableCreationEntity = new LevelTableEntity(Guid.NewGuid().ToString(), newNmae, sum, levelh);
                    levelTableEntities.Insert(0, levelTableCreationEntity);

                }

            }
            levelTableEntities = new ObservableCollection<LevelTableEntity>(levelTableEntities.OrderByDescending(x => x.StructuralElevation));
        }


    }
}
