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

using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using SAGA.DotNetUtils.Logger;
using SAGA.RevitUtils.MEP;
using ServiceRevitLib.Extend;
using ServiceRevitLib.Mode;
using ServiceRevitLib.Utils;

namespace ServiceRevitLib.DataCheck.Mode
{
    /// <summary>
    /// SagaCheck
    /// </summary>
    class SystemReferEquipCheck : CheckBase
    {
        public SystemReferEquipCheck()
        {
            ReferenceSheet = "参考-管网及相关设备";
        }
        public override void Check()
        {
            try
            {
                base.Check();

                #region

                m_EquipReferSystems = DataCheckRule.GetEquipReferSystems();

                var elements = m_Doc.GetEqEcElements();
                foreach (Element element in elements)
                {
                    var result = GetCheckResult(element);
                    if (result == null) continue;
                    Content.Add(result);
                }

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

        }

        private List<DCR_EquipReferSystem> m_EquipReferSystems;
     

        /// <summary>
        /// 获取检测结果
        /// </summary>
        /// <param name="fi"></param>
        /// <returns></returns>
        private SystemReferEquipCheckResult GetCheckResult(Element element)
        {
            var result = new SystemReferEquipCheckResult();
            var code = element.GetFamilyCode();
            // 可以与设备连接的系统
            var referSystems = m_EquipReferSystems.FirstOrDefault(t => t.EquipName == code)?.Systems ?? new List<string>();
            if (referSystems.Count == 0) return null;

            result.FamilyName = element.GetFamilyName();
            result.Id = element.Id.ToString();

            var tuple = CheckConType(element, referSystems);
            if (tuple.Item2.Any())
            {
                result.ResultMsg = $"连接件所连接的管道系统与规范不一致，请检查";
                result.UnPassSystems += string.Join("、", tuple.Item2.ToArray());
                result.Result = ResultState.Failure;
            }
            else
            {
                result.Result = ResultState.Success;
            }
            result.PassSystems += string.Join("、", tuple.Item1.ToArray());

            return result;
        }

        /// <summary>
        /// 检查连接件连接的管道系统是否符合规范
        /// </summary>
        /// <param name="result">符合要求的系统名称</param>
        /// <param name="domain">不符合要求的系统名称</param>
        private Tuple<List<string>, List<string>> CheckConType(Element element, List<string> referSystems)
        {
            List<string> passList = new List<string>();
            List<string> unpassList = new List<string>();

            var connectors = element.GetAllConnectors();
            foreach (Connector connector in connectors)
            {
                try
                {
                    var mepSystem = connector.MEPSystem;
                    if (mepSystem == null) continue;
                    var elementId = mepSystem.GetTypeId();
                    var type = element.Document.GetElement(elementId);
                    if (!referSystems.Contains(type.Name))
                    {
                        unpassList.Add(type.Name);
                    }
                    else
                    {
                        passList.Add(type.Name);
                    }
                }
                catch (Exception e)
                {
                    Log4Net.Info($"读取连接件系统错误。Id:{element.Id},Path:{element.Document.PathName}");
                }
            }

            return new Tuple<List<string>, List<string>>(passList, unpassList);
        }


    }
}
