/* ==============================================================================
 * 功能描述：ElementExtension  
 * 创 建 者：Garrett
 * 创建日期：2019/6/25 11:51:43
 * ==============================================================================*/
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 Autodesk.Revit.DB.Mechanical;
using RevitToJBim.Common;
using RevitToJBim.MBI;
using SAGA.RevitUtils.Extends;

namespace RevitToJBim.Extension
{
    /// <summary>
    /// ElementExtension
    /// </summary>
    public static class ElementExtension
    {
        public static string GetFamilyName(this Element element)
        {
            return element.GetFamily()?.Name;
        }
        public static string GetFamilySymbolName(this Element element)
        {
            return element.GetFamilySymbol()?.Name;
        }
        /// <summary>
        /// 获取设备的种族类型编码 ATFC
        /// 族名称的命名规则：ATFC-风机盘管
        /// </summary>
        /// <returns></returns>
        public static string GetFamilyCode(this Element element)
        {
            string code = "";
            if (element is FamilyInstance fi)
            {
                string familyName = fi.GetFamilyName();
                if (familyName == null) return code;
                //族名称的命名规则：ATFC-风机盘管
                int index = familyName.IndexOf('-');
                if (index != -1 && index + 1 != familyName.Length)
                    code = familyName.Substring(0, familyName.IndexOf('-'));
                //移除前面和后面的空格
                code = code.Trim();
            }

            return code;
        }
        /// <summary>
        /// 判断是否为设备 设备族为4位
        /// ATVR - 多联机 - 室内机 - 双向气流 - 天花板嵌入式
        /// </summary>
        /// <param name="fi"></param>
        /// <returns></returns>
        public static bool IsEquipment(this Element element)
        {
            bool result = false;
            if (element is FamilyInstance fi)
            {
                var family = fi.GetFamilyName();
                result = Regex.IsMatch(family, $"{MBIRegexPattern.IsEquip}");
            }

            return result;
        }
        /// <summary>
        /// 判断是否为设备部件 设备族为6位
        /// </summary>
        /// <param name="fi"></param>
        /// <returns></returns>
        public static bool IsEquipmentPart(this Element element)
        {
            bool result = false;
            if (element is FamilyInstance fi)
            {
                var family = fi.GetFamilyName();
                result = Regex.IsMatch(family, $"{MBIRegexPattern.IsEquipPart}");
            }

            return result;
        }
        /// <summary>
        /// 判断是否为信标
        /// </summary>
        /// <param name="elem"></param>
        /// <returns></returns>
        public static bool IsBeacon(this Element elem)
        {
            var family = elem.GetFamilyName();
            return family != null && (Regex.IsMatch(family, MBIRegexPattern.IsBeacon));
        }

        /// <summary>
        /// 判断是否为空间，判断周长是否为零
        /// 如果周长为零，是删除的空间
        /// </summary>
        /// <param name="elem"></param>
        /// <param name="ischeckzero">是否检查周长为零</param>
        /// <returns></returns>
        public static bool IsSpace(this Element elem, bool ischeckzero = true)
        {
            var isspace = false;
            if (elem is Space space)
            {
                //空间比较特殊，周长为零就相当于删除
                isspace = !ischeckzero || !(space.IsDeleteSpace());
                string id = space.Id.ToString();
                //限制所用空间的阶段
                if (isspace)
                {
                    //增加过滤效率，如果上一步正确菜继续往下执行。
                    isspace = space.IsPhase1Space();
                }
                if (isspace)
                {
                    isspace = space.IsViewLevel();
                }
            }

            return isspace;
        }
    }
}
