/*-------------------------------------------------------------------------
 * 功能描述:ParsePipe
 * 作者:xulisong
 * 创建时间: 2019/6/13 16:54:19
 * 版本号:v1.0
 *  -------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB.Plumbing;
using JBIM;
using JBIM.Definition;
using RevitExport;
using RevitExport.Export;
using RevitToJBim.Common;
using RevitToJBim.ParseData;
using SAGA.RevitUtils.Extends;
using SAGA.RevitUtils.MEP;
using JPipe=JBIM.Component.Pipe;

namespace RevitToJBim.ComponentParse
{
    [UsableParseAttribute]
    public class ParsePipe : ParseBase
    {
        public override List<string> FastIndex()
        {
            return new List<string>() { typeof(Pipe).FullName };
        }
        public override bool Match(ElementWrapper wrapper)
        {
            return wrapper.RefElement is Pipe;
        }

        protected override List<BimId> ParseInner(ElementWrapper wrapper, JBimParseContext context)
        {
            /*
             * 解析思路:1、解析当前对象具体信息。
             *           2、维护一些构件的关联管理关系,可以放在这个方法中,也可以放在ArrangeRefElements中
             *
             */
            if (!(wrapper.RefElement is Pipe pipe))
            {
                return null;
            }
            JPipe jPipe = new JPipe();
            ParseCore.AttachObject(jPipe, wrapper);
            jPipe.Diameter =pipe.Diameter.FtToUse();
            var locations = new List<XYZ>();
            locations.Add(BimConvert.ConvertToXYZ(pipe.GetCurve().StartPoint()));
            locations.Add(BimConvert.ConvertToXYZ(pipe.GetCurve().EndPoint()));
            jPipe.Location = GeometryLocation.CreateLineLocation(locations);
            Polygon outLine = new Polygon(locations);
            jPipe.OutLine.Add(outLine);
            context.AddBimObject(jPipe);
            #region 关联数据处理相关
            #region 系统关系
            var pipeId = pipe.Id.ToString();
            var systemTypeName=pipe.Document.GetElement(pipe.MEPSystem.GetTypeId());
            jPipe.MepSystemTypeName = systemTypeName.Name;
            #endregion
            #region Connector连接关系
            var connectors = pipe.GetConnectors();
            ElementOneToManyRel relMany = new ElementOneToManyRel(pipeId) { RelatedObjects = new List<string>() };
            relMany.SetElementType(TypeDefinition.Property_ConnectedIds);
            foreach (var refConnector in connectors)
            {
                relMany.RelatedObjects.Add(RevitIdGenerator.GetConnectorId(refConnector));
            }

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

        public override List<ElementWrapper> ArrangeRefElements(ElementWrapper wrapper, JBimParseContext context)
        {
            if (!(wrapper.RefElement is Pipe pipe))
            {
                return null;
            }
            //创建MepSystem
           // var mepSystemElementWrapper = new ElementWrapper(pipe.MEPSystem);
            var wrappers=new List<ElementWrapper>() { };
            var connectors = pipe.GetConnectors();
            foreach (var connector in connectors)
            {
                wrappers.Add(ParseCore.GetConnectorWrapper(connector));
            }            
            return wrappers;
        }
    }
}