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

using System;
using System.Linq;
using System.Text.RegularExpressions;
using Autodesk.Revit.DB;
using SAGA.DotNetUtils.Extend;
using ServiceRevitLib.Common;
using SAGA.RevitUtils.Extends;

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

                #region

                string resultMsg = null;
                ResultState resultState = 0;
                string planName = "";

                //判断是否存在Saga标记
                var document = m_Doc;
                var sagaPlans = document.GetElements<ViewPlan>()
                    .Where(t => t.ViewType == ViewType.FloorPlan && t.Name.Contains("-saga")).ToList();
                if (sagaPlans.Count == 0)
                {
                    resultState = ResultState.Failure;
                    resultMsg = "缺少saga标记";
                }
                else if (sagaPlans.Count() >= 2)
                {
                    resultState = ResultState.Failure;
                    resultMsg = "有多个saga标记";
                }
                else
                {
                    //只有一个saga标记
                    var sagaPlan = sagaPlans.FirstOrDefault();
                    if (sagaPlan != null)
                    {
                        planName = sagaPlan.Name;
                        //打标记的楼层名称必需为指定格式；B1,B1M,F1,F1M,RFM,RF
                        if (Regex.IsMatch(planName, $"{RegexConstPattern.IsMBIView}"))
                        {
                            var rfLevel = document.GetLevels().FirstOrDefault(t =>
                                System.Text.RegularExpressions.Regex.IsMatch(t.Name, $"{RegexConstPattern.IsRF}"));
                            if (rfLevel == null)
                            {
                                resultState = ResultState.Failure;
                                resultMsg = $"缺少RF标高";
                            }
                            else
                            {
                                if (rfLevel.Elevation.IsThanEq(sagaPlan.GenLevel.Elevation))
                                {
                                    resultState = ResultState.Success;
                                }
                                else
                                {
                                    resultState = ResultState.Failure;
                                    resultMsg = $"RF标高的位置不正确，请检查";
                                }
                            }
                        }
                        else
                        {
                            resultState = ResultState.Failure;
                            resultMsg = $"楼层 {planName} 不符合楼层命名规范";
                        }
                    }
                }

                Content.Add(new SagaCheckResult() { PlanName = planName, Result = resultState, ResultMsg = resultMsg });

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

        }
    }
}
