/* ==============================================================================
 * 功能描述:数据检查规范
 * 创 建 者:Garrett
 * 创建日期:2018/11/7 15:40:23
 * ==============================================================================*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using SAGA.DotNetUtils;
using SAGA.DotNetUtils.NPOI;
using ServiceRevitLib.Common;

namespace ServiceRevitLib.Utils
{
    /// <summary>
    /// DataCheckRule
    /// </summary>
    class DataCheckRule
    {
        public static string MCRPath = Path.Combine(MBIConst.MBIResourcePath, @"DataCheck\模型检查结果输出格式-模版.xlsx");
        /// <summary>
        /// 获取设备编码检查Cateogry 列表
        /// </summary>
        /// <returns></returns>
        public static List<DCR_CodeCheckCategory> GetCodeCheckCategories()
        {
            var list = NPOIHelper
                .ConvertExcelSheetToModel<DCR_CodeCheckCategory>(MCRPath);
            return list;
        }

        /// <summary>
        /// 获取预定义的连接件类
        /// </summary>
        /// <returns></returns>
        public static List<DCR_Connector> GetPreDefineConnectors()
        {
            var list = NPOIHelper
                .ConvertExcelSheetToModel<DCR_Connector>(MCRPath);
            return list;
        }

        /// <summary>
        /// 获取可识别的管道系统名称
        /// </summary>
        /// <returns></returns>
        public static List<DCR_MEPSystem> GetMepSystems()
        {
            var list = NPOIHelper
                .ConvertExcelSheetToModel<DCR_MEPSystem>(MCRPath);
            return list;
        }
        /// <summary>
        /// 获取设备关联的系统类型
        /// </summary>
        /// <returns></returns>
        public static List<DCR_EquipReferSystem> GetEquipReferSystems()
        {
            var list = NPOIHelper
                .ConvertExcelSheetToModel<DCR_SystemReferEquip>(MCRPath);
            List<Tuple<string, string>> tuples = new List<Tuple<string, string>>();
            //将集合转化为:系统名称-设备名称
            list.ForEach(t => t.Equips.ForEach(tt => tuples.Add(new Tuple<string, string>(t.SystemName, tt))));
            //将集合转化为:设备名称-多个系统名称
            var newList = tuples.GroupBy(t => t.Item2).Select(tt => new DCR_EquipReferSystem()
            { EquipName = tt.Key, Systems = tt.Select(t => t.Item1).ToList() }).ToList();
            return newList;
        }
    }
    /// <summary>
    /// 参考-可识别的系统名称
    /// </summary>
    [SheetInfo(SheetName = "参考-可识别的系统名称", RowStartIndex = 0)]
    public class DCR_MEPSystem
    {
        [CellIndex(0)]
        public string Name { get; set; }
    }
    /// <summary>
    /// 参考-revit分类
    /// </summary>
    [SheetInfo(SheetName = "参考-revit分类", RowStartIndex = 0)]
    public class DCR_CodeCheckCategory
    {
        [CellIndex(0)]
        public string Name { get; set; }
    }
    /// <summary>
    /// 设备类是否有连接件
    /// </summary>
    [SheetInfo(SheetName = "参考-连接件对照表", RowStartIndex = 1)]
    public class DCR_Connector
    {
        [CellIndex(4)]
        public string Name { get; set; }

        [CellIndex(5)]
        public string Code { get; set; }

        [CellIndex(6)]
        public string PName { get; set; }

        [CellIndex(7)]
        public string PCode { get; set; }

        [CellIndex(8)]
        public string HvacConnector { get; set; }

        [CellIndex(9)]
        public string PipeConnector { get; set; }
    }
    /// <summary>
    /// 参考-管网与相关设备
    /// </summary>
    [SheetInfo(SheetName = "参考-管网及相关设备", RowStartIndex = 3)]
    public class DCR_SystemReferEquip
    {
        [CellIndex(3)]
        public string SystemName { get; set; }
        [CellIndex(4)]
        public string ReferEquipName { get; set; }

        private List<string> m_Equips;

        public List<string> Equips
        {
            get
            {
                if (m_Equips == null)
                    m_Equips = ConverToList(ReferEquipName);
                return m_Equips;
            }
        }

        public List<string> ConverToList(string str)
        {
            var list = new List<string>();
            if (str.IsNotNullEmpty())
            {
                Regex regex = new Regex(@"^[A-Z]{4}");
                list = str.Split(new[] { ',', ',' })
                    .Where(t => Regex.IsMatch(t, $"{RegexConstPattern.IsEquip}"))
                    .Select(tt => regex.Match(tt).Value).ToList();
            }

            return list;
        }
    }
    /// <summary>
    /// 设备关联的系统
    /// </summary>
    public class DCR_EquipReferSystem
    {
        /// <summary>
        /// 设备名称
        /// </summary>
        public string EquipName { get; set; }
        /// <summary>
        /// 系统名称
        /// </summary>
        public List<string> Systems { get; set; }
    }
}