/*-------------------------------------------------------------------------
 * 功能描述：ParseConnector
 * 作者：xulisong
 * 创建时间： 2019/6/18 11:26:05
 * 版本号：v1.0
 *  -------------------------------------------------------------------------*/

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 JBIM;
using JBIM.Definition;
using JBIM.Relationship;
using RevitExport;
using RevitExport.Export;
using RevitToJBim.Common;
using RevitToJBim.ParseData;
using SAGA.RevitUtils.MEP;
using JConnector = JBIM.Component.Connector;

namespace RevitToJBim.ComponentParse
{
    [UsableParse]
    public class ParseConnector : ParseBase
    {
        public override List<string> FastIndex()
        {
            return new List<string>() { typeof(Connector).FullName, typeof(MechanicalSystemType).FullName };
        }
        public override bool Match(ElementWrapper wrapper)
        {
            return wrapper.GetObject<Connector>() != null;
        }

        protected override List<BimId> ParseInner(ElementWrapper wrapper, JBimParseContext context)
        {
            var connector = wrapper.GetObject<Connector>();
            if (connector == null)
            {
                return null;
            }
            JConnector jConnector = ParseCore.CreateConnector(connector);
            jConnector.SourceId = wrapper.SourceId;
            context.AddBimObject(jConnector);

            #region 所属类别解析
            //直接递归方式获取
            var owner = connector.Owner;
            if (owner != null)
            {
                ElementWrapper systemTypeWrapper = ElementWrapperFactory.CreateWrapper(owner);
                jConnector.Owner = context.Parser.ParseElement(systemTypeWrapper)?.FirstOrDefault();
            }
            #endregion

            #region 所属系统解析
            //维护关联并递归
            var connectorId = jConnector.SourceId;
            var mepSystemTypeId = connector.MEPSystem?.GetTypeId();
            if (mepSystemTypeId != null)
            {
                var systemTypeName = owner.Document.GetElement(mepSystemTypeId);
                jConnector.MepSystemTypeName = systemTypeName.Name;
            }
           
            #endregion
            #region 关联Connector
            //这里只维护关联，不进行递归
            var refConnectors = connector.GetReferenceConnectors();
            ElementOneToManyRel relMany = new ElementOneToManyRel(connectorId){RelatedObjects=new List<string>()};
            relMany.SetElementType(TypeDefinition.Property_ConnectedIds);
            foreach (var refConnector in refConnectors)
            {
                relMany.RelatedObjects.Add(RevitIdGenerator.GetConnectorId(refConnector));
            }

            if (relMany.RelatedObjects.Any())
            {
                context.RelationShips.Add(relMany);
            }
            #endregion
            return new List<BimId>() { jConnector.Id };
        }

        public override List<ElementWrapper> ArrangeRefElements(ElementWrapper wrapper, JBimParseContext context)
        {
            #region MyRegion
            //if (!(wrapper.RefObject is Connector connector))
            //{
            //    return null;
            //}

            //if (connector.MEPSystem == null)
            //{
            //    return null;
            //}
            ////创建MepSystem
            //var mepSystemElementWrapper = new ElementWrapper(connector.MEPSystem);
            //return new List<ElementWrapper>() { mepSystemElementWrapper }; 
            #endregion
            return null;
        }
    }
}
