/*-------------------------------------------------------------------------
 * 功能描述:ParseFamilyJoinObject
 * 作者:xulisong
 * 创建时间: 2019/6/24 9:29:00
 * 版本号:v1.0
 *  -------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using JBIM;
using JBIM.Component;
using RevitExport.Export;
using RevitToJBim.Common;
using JFamilyType=RevitToJBim.Common.FamilyType;
using RevitToJBim.ParseData;
using SAGA.RevitUtils.MEP;

namespace RevitToJBim.ComponentParse
{
    [UsableParse]
    public class ParseFamilyJoinObject : ParseBase
    {
        public override List<string> FastIndex()
        {
            return new List<string>() { CategoryGenerator .BuildingCategory(JFamilyType.Other)};
        }
        public override bool Match(ElementWrapper wrapper)
        {
            return wrapper.Category == CategoryGenerator.BuildingCategory(JFamilyType.Other);
        }

        protected override List<BimId> ParseInner(ElementWrapper wrapper, JBimParseContext context)
        {
            if (!(wrapper.RefElement is FamilyInstance familyInstance))
            {
                return null;
            }
            OtherJoinObject jObject = new OtherJoinObject();
            ParseCore.AttachObject(jObject, wrapper);
            context.AddBimObject(jObject);
            #region Connector连接关系
            var connectors = GetConnectors(familyInstance);
            if (connectors.Any())
            {
                ElementOneToManyRel relMany = ParseCore.GetConnectorRels(familyInstance,connectors);
                context.RelationShips.Add(relMany);
            }
            #endregion
            return new List<BimId>() { jObject.Id };
        }

        public override List<ElementWrapper> ArrangeRefElements(ElementWrapper wrapper, JBimParseContext context)
        {
            if (!(wrapper.RefElement is FamilyInstance fi))
            {
                return null;
            }

            var wrappers = new List<ElementWrapper>() { };
            var connectors = GetConnectors(fi);
            foreach (var connector in connectors)
            {
                wrappers.Add(ParseCore.GetConnectorWrapper(connector));
            }
            return wrappers;
        }

        public List<Autodesk.Revit.DB.Connector> GetConnectors(Element familyInstance)
        {   
               return  familyInstance.GetAllConnectors();
        }
    }
}