/* ==============================================================================
 * 功能描述：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 ServiceRevitLib.Extend;
using ServiceRevitLib.Utils;

namespace ServiceRevitLib.Mode
{
    /// <summary>
    /// SagaCheck
    /// </summary>
    class FamilyNameCheck : CheckBase
    {
        public FamilyNameCheck()
        {
            ReferenceSheet = "参考-revit分类";
        }
        public override void Check()
        {
            try
            {
                base.Check();

                #region

                var ccategories = DataCheckRule.GetCodeCheckCategories();
                var categories = m_Doc.Settings.Categories;
                foreach (DCR_CodeCheckCategory ccategory in ccategories)
                {
                    try
                    {
                        var category = categories.get_Item(ccategory.Name);
                        if (category == null) continue;
                        BuiltInCategory builtInCategory = (BuiltInCategory)category.Id.IntegerValue;
                        var elements = m_Doc.GetElements<FamilyInstance>(builtInCategory);
                        var groupByFamily = elements.GroupBy(t => t.GetFamilyName());
                        foreach (IGrouping<string, FamilyInstance> grouping in groupByFamily)
                        {
                            var subElements = grouping.ToList();
                            var subElement = subElements.FirstOrDefault();
                            if (subElement is FamilyInstance fi)
                            {
                                var result = new FamilyNameCheckResult();
                                result.FamilyName = grouping.Key;
                                if (fi.IsEquipment() || fi.IsEquipmentPart() || fi.IsBeacon())
                                {
                                    result.Result = ResultState.Success;
                                }
                                else
                                {
                                    result.Result = ResultState.Failure;
                                    result.ResultMsg = "请检查族名称编码是否符合要求";
                                }
                                Content.Add(result);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }

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

        }
        
    }
}
