/* ==============================================================================
 * 功能描述：xyz坐标重叠检查
 * 创 建 者：Garrett
 * 创建日期：2018/10/23 15:08:55
 * ==============================================================================*/

using System;
using System.Linq;
using Autodesk.Revit.DB;
using SAGA.RevitUtils;
using SAGA.RevitUtils.Extends;
using ServiceRevitLib.Extend;
using ServiceRevitLib.Mode;

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

                #region

                var doc = m_Doc;
                var instances = doc.GetFamilyInstances().Where(t=>t.GetLocationPoint()!=null);
                var groups = instances.GroupBy(t => t.GetLocationPoint(), new XyzEqualComparer(0.01d));
                foreach (var group in groups)
                {
                    var key = group.Key;
                    if (key == null) continue;
                    var list = group.ToList();
                    if (list.Count == 1) continue;
                    var fi = list.FirstOrDefault();
                    var result = GetCheckResult(fi);
                    list.Remove(fi);
                    if (result == null) continue;

                    result.ResultMsg = $"与{string.Join(",", list.Select(t => t.Id.ToString()))}的坐标发生重叠，请检查";
                    Content.Add(result);
                }
                #endregion
            }
            catch (Exception e)
            {
                //Result = ResultState.Failure;
                //ResultMsg = $"{e.Message}\r\n{e.StackTrace}";
                Result = ResultState.Success;
                ResultMsg = $"";
            }
        }
        /// <summary>
        /// 获取检测结果
        /// </summary>
        /// <param name="fi"></param>
        /// <returns></returns>
        private XYZOverlapCheckResult GetCheckResult(Element fi)
        {
            var result = new XYZOverlapCheckResult(){ Result = ResultState.Failure};
            result.Id = fi.Id.ToString();
            result.FamilyName = fi.GetFamilyName();
            return result;
        }
    }

    class XYZOverlapCheckResult : ResultBase
    {
        public string FamilyName { get; set; }

        public string Id { get; set; }
    }
}
