/* ==============================================================================
 * 功能描述：SagaCheck  
 * 创 建 者：Garrett
 * 创建日期：2019/6/11 16:09:09
 * ==============================================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using SAGA.DotNetUtils.Extend;
using ServiceRevitLib.Common;
using SAGA.RevitUtils.Extends;
using SAGA.RevitUtils.MEP;
using ServiceRevitLib.Extend;
using ServiceRevitLib.Utils;

namespace ServiceRevitLib.Mode
{
    /// <summary>
    /// SagaCheck
    /// </summary>
    class SystemNameCheck : CheckBase
    {
        public SystemNameCheck()
        {
            ReferenceSheet = "参考-可识别的系统名称";
        }
        public override void Check()
        {
            try
            {
                base.Check();

                #region

                m_MEPSystems = DataCheckRule.GetMepSystems();

                var mepsystemTypes = new List<MEPSystemType>();
                mepsystemTypes.AddRange(m_Doc.GetMechanicalSystemTypes());
                mepsystemTypes.AddRange(m_Doc.GetPipingSystemTypes());
                foreach (var t in mepsystemTypes)
                {
                    var result = GetCheckResult(t);
                    Content.Add(result);
                }

                #endregion
            }
            catch (Exception e)
            {
                Result = ResultState.Failure;
                ResultMsg = $"{e.Message}\r\n{e.StackTrace}";
            }
           

        }
        private List<DCR_MEPSystem> m_MEPSystems;

        /// <summary>
        /// 获取检测结果
        /// </summary>
        /// <param name="fi"></param>
        /// <returns></returns>
        private SystemNameCheckResult GetCheckResult(MEPSystemType system)
        {
            var result = new SystemNameCheckResult();
            string systemName = system.Name;
            result.SystemName = systemName;
            result.SystemType = system is MechanicalSystemType ? "风管系统" : "管道系统";
            
            var item = m_MEPSystems.Where(t => SystemNameIsEqual(systemName, t.Name));
            if (item.Any())
            {
                result.Result = ResultState.Success;
            }
            else
            {
                result.Result = ResultState.Failure;
                result.ResultMsg = $"未知的系统名称，请按照系统类型命名规范修改";
            }
            
            return result;
        }
        /// <summary>
        /// 系统名称相等,模型中的系统名称包含就定义名称即可
        /// </summary>
        /// <param name="originName"></param>
        /// <param name="targetName"></param>
        /// <returns></returns>
        private bool SystemNameIsEqual(string originName, string targetName)
        {
            bool result = false;
            string n1 = originName.ToLower();
            string n2 = targetName.ToLower();
            return n1.Equals(n2);
        }
    }
}
