/*-------------------------------------------------------------------------
 * 功能描述：ElementWrapperFactory
 * 作者：xulisong
 * 创建时间： 2019/6/24 8:44:46
 * 版本号：v1.0
 *  -------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using RevitExport;
using RevitExport.Export;
using RevitToJBim.Extension;
using RevitToJBim.MBI;
using SAGA.RevitUtils.Extends;
using SAGA.RevitUtils.MEP;

namespace RevitToJBim.Common
{
    /// <summary>
    /// 解析对象创建工厂
    /// </summary>
    public class ElementWrapperFactory
    {
        /// <summary>
        /// 根据element创建带分类对象的解析信息。便于快速过滤.
        /// 注:生成wrapper时，为了保证统一性，最好统一调用该方法
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static ElementWrapper CreateWrapper(Element element)
        {
            if (element is FamilyInstance fi)
            {
                CreateWrapper(fi);
            }
            return new ElementWrapper(element);
        }
        /// <summary>
        /// 创建FamilyInstance的wrapper
        /// </summary>
        /// <param name="familyInstance"></param>
        /// <returns></returns>
        public static ElementWrapper CreateWrapper(FamilyInstance familyInstance)
        {
            var useFamilyType = FamilyType.Undefine;
            var category = familyInstance.Category;          
            if (category != null)
            {
                var enumValue = (BuiltInCategory)category.Id.IntegerValue;
                #region category转换
                switch (enumValue)
                {
                    case BuiltInCategory.OST_Doors:
                        {
                            useFamilyType = FamilyType.Door;
                            break;
                        }
                    case BuiltInCategory.OST_Windows:
                        {
                            useFamilyType = FamilyType.Window;
                            break;
                        }
                    case BuiltInCategory.OST_Columns:
                        {
                            useFamilyType = FamilyType.Column;
                            break;
                        }
                } 
                #endregion
            }

            if (useFamilyType == FamilyType.Undefine)
            {
                //判断部件和设备
                var familyName = familyInstance.GetFamilyName();
                if (Regex.IsMatch(familyName, MBIRegexPattern.IsEquip)|| Regex.IsMatch(familyName, MBIRegexPattern.IsEquipPart))
                    useFamilyType = FamilyType.Facility;
                else if (Regex.IsMatch(familyName, MBIRegexPattern.IsBeacon))
                {
                    useFamilyType = FamilyType.Beacon;
                }
            }
            if (useFamilyType == FamilyType.Undefine)
            {
                var connectors = familyInstance.GetAllConnectors();
                if (connectors.Any(c => c.Domain == Domain.DomainHvac || c.Domain == Domain.DomainPiping))
                {
                    useFamilyType = FamilyType.Other;
                }
            }

            if (useFamilyType == FamilyType.Undefine)
            {
                return null;
            }

            ElementWrapper wrapper = new ElementWrapper(familyInstance);
            wrapper.Category = CategoryGenerator.BuildingCategory(useFamilyType);
            return wrapper;

        }
    }
}
