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

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

namespace ServiceRevitLib.Mode
{
    /// <summary>
    /// SagaCheck
    /// </summary>
    class PipeCheck : CheckBase
    {
        public override void Check()
        {
            try
            {
                base.Check();

                #region

                var document = m_Doc;

                var elements = document.FilterElements<MEPCurve>().ToList();
                foreach (var element in elements)
                {
                    if (element.IsStart())
                    {
                        //标记立管，所有连接点都未记录则删除                 
                        //if (!connectors.Any(c => c.IsConnected))
                        //{
                        //    item.ErrorCode = "001";
                        //    reportItem.ResultItems.Add(item);                      
                        //}
                        continue;
                    }
                    var item = new PipeCheckResult();
                    item.SystemName = element.GetSystemTypeName();
                    item.Id = element.Id.ToString();
                    var code = GetErrorCode(element, item.SystemName);
                    if (IsNeedExport(code))
                    {
                        item.Result = ResultState.Failure;
                        item.ResultMsg = GetErrorDescription(code);
                        Content.Add(item);
                    }
                }
                #endregion
            }
            catch (Exception e)
            {
                Result = ResultState.Failure;
                ResultMsg = $"{e.Message}\r\n{e.StackTrace}";
            }
           

        }
        /// <summary>
        /// 获取错误码
        /// </summary>
        /// <param name="element"></param>
        /// <param name="systemName"></param>
        /// <returns></returns>
        private PipeCheckErrorCode GetErrorCode(Element element,string systemName)
        {
            PipeCheckErrorCode errorCode = PipeCheckErrorCode.Code_000;
            Domain[] domains = new Domain[] { Domain.DomainPiping, Domain.DomainElectrical };
            do
            {
                foreach (Domain domain in domains)
                {
                    var connectors = element.GetConnectors(domain);
                    if (connectors.Any(c => !c.IsConnected))
                    {
                        errorCode = PipeCheckErrorCode.Code_001;
                        break;
                    }
                }
                var endElements = element.GetFirstElements(null, e => e is MEPCurve || e.IsEquipment());
                List<MEPCurve> curves = endElements.OfType<MEPCurve>().ToList();
                foreach (var mepCurve in curves)
                {
                    if (systemName != mepCurve.GetSystemTypeName())
                    {
                        errorCode = PipeCheckErrorCode.Code_002;
                        break;
                    }
                }

            } while (false);

            return errorCode;
        }
        /// <summary>
        /// 获取错误描述
        /// </summary>
        /// <param name="errorCode"></param>
        /// <returns></returns>
        private string GetErrorDescription(PipeCheckErrorCode errorCode)
        {
            string description = errorCode.GetDescription()+",请检查";
            return description;
        }
        /// <summary>
        /// 是否需要导出
        /// </summary>
        /// <param name="errorCode"></param>
        /// <returns></returns>
        private bool IsNeedExport(PipeCheckErrorCode errorCode)
        {
            return errorCode == PipeCheckErrorCode.Code_001 || errorCode == PipeCheckErrorCode.Code_002;
        }

        enum PipeCheckErrorCode
        {
            [Description("管段连接正确")]
            Code_000,
            [Description("末端未连接任何设备")]
            Code_001,
            [Description("管道两端连接了不同类型的管道")]
            Code_002,
            Code_003,
            [Description("误报")]
            Code_004
        }
    }
}
