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

using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using SAGA.RevitUtils.Extends;
using ServiceRevitLib.Common;
using ServiceRevitLib.Extend;
using ServiceRevitLib.Mode;

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

                #region

                var doc = m_Doc;
                var instances = doc.GetEqEcElements();
                var familyGroups = instances.GroupBy(t => t.GetFamilyName());
                foreach (IGrouping<string, Element> familyGroup in familyGroups)
                {
                    Element fi = familyGroup.FirstOrDefault();
                    if (fi == null) continue;
                    var result = GetCheckResult(fi);
                    if (result == null) continue;
                    result.FamilyName = familyGroup.Key;
                    Content.Add(result);
                }

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

        }


        /// <summary>
        /// 获取检测结果
        /// </summary>
        /// <param name="fi"></param>
        /// <returns></returns>
        private ParameterIntegrityCheckResult GetCheckResult(Element fi)
        {
            //检查项
            var checkParamNames = new List<string>() { MBIConst.EquipLocalName, MBIConst.EquipLocalID };

            var result = new ParameterIntegrityCheckResult();
            List<string> list = new List<string>();
            foreach (var paramName in checkParamNames)
            {
                var parameter = fi.GetParameter(paramName);
                if (parameter == null)
                    list.Add(paramName);
            }
            if (list.Any())
            {
                result.Result = ResultState.Failure;
                result.ResultMsg = $"缺失的参数为：{string.Join("、", list)}";
            }
            else
            {
                result.Result = ResultState.Success;
            }
            return result;
        }
    }
}
