/* ==============================================================================
 * 功能描述：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 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 ConnectorCheck : CheckBase
    {
        public ConnectorCheck()
        {
            ReferenceSheet = "参考-连接件对照表";
        }
        public override void Check()
        {
            base.Check();

            #region

            bool unitResult = true;
            m_ProDefineConnectors = GetPreDefineConnectors();
            var elements = m_Doc.GetEqEcElements();

            foreach (var element in elements)
            {
                //检查设备或者部件
                var result = GetCheckResult(element);
                if (result == null) continue;
                Content.Add(result);
            }
            #endregion

        }
        

        private string m_HaveConnectorSing = "√";
        private List<DCR_Connector> m_ProDefineConnectors;

        public List<DCR_Connector> GetPreDefineConnectors()
        {
            var list = new List<DCR_Connector>();
            var alllist = DataCheckRule.GetPreDefineConnectors();
            foreach (DCR_Connector connector in alllist)
            {
                var pipeType = connector.PipeConnector;
                var hvacType = connector.HvacConnector;
                if (pipeType == m_HaveConnectorSing || hvacType == m_HaveConnectorSing)
                {
                    list.Add(connector);
                    if (connector.PName.IsNotNullEmpty() && connector.PCode.IsNotNullEmpty())
                    {
                        //部件单独提出来当做一项
                        var pconnector = new DCR_Connector()
                        {
                            Code = connector.PCode,
                            Name = connector.Name,
                            PipeConnector = connector.PipeConnector,
                            HvacConnector = connector.HvacConnector
                        };
                        list.Add(pconnector);
                    }
                }
            }
            return list;
        }
        /// <summary>
        /// 获取检测结果
        /// </summary>
        /// <param name="fi"></param>
        /// <returns></returns>
        private ConnectorCheckResult GetCheckResult(Element element)
        {
            if (element == null) return null;
            var result = new ConnectorCheckResult();
            var code = element.GetFamilyCode();
            var defineConType = GetDefineConType(code);
            if (defineConType == null) return null;
            result.FamilyName = element.GetFamilyName();
            result.Id = element.Id.ToString();
            result.Result = ResultState.Success;
            CheckConType(result, element, defineConType.Item1);
            CheckConType(result, element, defineConType.Item2);
            return result;
        }
        /// <summary>
        /// 获取定义的风和水连接件类型
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private Tuple<Domain, Domain> GetDefineConType(string code)
        {
            var item = m_ProDefineConnectors.FirstOrDefault(t => t.Code == code);
            if (item == null) return null;
            var pipeType = item.PipeConnector;
            var hvacType = item.HvacConnector;

            var pipeConType = pipeType == m_HaveConnectorSing ? Domain.DomainPiping : Domain.DomainUndefined;
            var hvacConType = hvacType == m_HaveConnectorSing ? Domain.DomainHvac : Domain.DomainUndefined;
            return new Tuple<Domain, Domain>(pipeConType, hvacConType);
        }
        /// <summary>
        /// 检查连接件类型是否存在
        /// </summary>
        /// <param name="result"></param>
        /// <param name="domain"></param>
        private void CheckConType(ConnectorCheckResult result, Element fi, Domain domain)
        {
            if (domain != Domain.DomainUndefined)
            {
                var connectors = fi.GetConnectors(domain);
                var domainStr = DomainToString(domain);
                if (connectors.Any())
                {
                    if (connectors.All(t => t.IsConnected)) return;
                    result.Result = ResultState.Failure;
                    result.ResultMsg += $"发现 {domainStr}连接件未连接;";
                }
                else
                {
                    result.Result = ResultState.Failure;
                    result.ResultMsg += $"缺少 {domainStr} 连接件;";
                }
            }
        }

        private string DomainToString(Domain domain)
        {
            string str = "";
            switch (domain)
            {
                case Domain.DomainPiping:
                    str = "水管";
                    break;
                case Domain.DomainHvac:
                    str = "风管";
                    break;
                default:
                    str = "未知";
                    break;

            }
            return str;
        }
    }
}
