/* ==============================================================================
 * 功能描述：SpaceExtension  
 * 创 建 者：Garrett
 * 创建日期：2019/6/26 17:40:25
 * ==============================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using SAGA.RevitUtils;
using SAGA.RevitUtils.Extends;

namespace RevitToJBim.Extension
{
    /// <summary>
    /// SpaceExtension
    /// </summary>
    public static class SpaceExtension
    {
        public readonly static string UseablePhaseName = "阶段1";

        /// <summary>
        /// 获取系统使用的阶段Id
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        public static ElementId GetUsePhaseId(this Document doc)
        {
            var phase = GetUsePhase(doc);
            if (phase == null)
            {
                return ElementId.InvalidElementId;
            }
            return phase.Id;
        }
        /// <summary>
        /// 获取系统使用的阶段
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        public static Phase GetUsePhase(this Document doc)
        {
            var elements = doc.GetElements<Phase>(BuiltInCategory.OST_Phases);
            foreach (var element in elements)
            {
                var tempName = element.Name.Replace(" ", "").Trim();
                if (UseablePhaseName == tempName)
                {
                    return element;
                }
            }
            return null;
        }

        public static ElementId GetCurrentPhaseId(this Space space)
        {
            return space.GetParameterElementId(BuiltInParameter.ROOM_PHASE_ID) ?? ElementId.InvalidElementId;
        }
        /// <summary>
        /// 判断是否是阶段1的空间
        /// </summary>
        /// <param name="space"></param>
        /// <returns></returns>
        public static bool IsPhase1Space(this Space space)
        {
            var doc = space.Document;
            var useId = GetUsePhaseId(doc);
            return space.GetCurrentPhaseId() == useId;
        }
        /// <summary>
        /// 空间标高是否是当前使用视图标高
        /// </summary>
        /// <param name="space"></param>
        /// <returns></returns>
        public static bool IsViewLevel(this Space space)
        {
            var doc = space.Document;
            var useViewId = doc.GetUseView();
            if (useViewId == null)
            {
                return false;
            }
            return space.Level?.Id == useViewId.GenLevel?.Id;
        }
    }
}
