/*-------------------------------------------------------------------------
 * 功能描述：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 JBIM.Definition;
using RevitExport.Export;
using RevitToJBim.Common;
using RevitToJBim.Extension;
using JFamilyType=RevitToJBim.Common.FamilyType;
using RevitToJBim.ParseData;
using SAGA.RevitUtils.Extends;
using SAGA.RevitUtils.MEP;

namespace RevitToJBim.ComponentParse
{
    [UsableParse]
    public class ParseOther : 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;
            }
            //ElementType
            Other jObject = new Other();
            //Name,SourceId
            ParseCore.AttachObject(jObject, wrapper);
            //FamilyName
            jObject.FamilyName = familyInstance.GetFamilyName();
            jObject.FamilySymbol = familyInstance.GetFamilySymbolName();
            //Location
            jObject.Location = GeometryLocation.CreatePointLocation(BimConvert.ConvertToXYZ(familyInstance.GetLocationPoint()));
            //OutLine
            var polygonPath = RevitUtil.GetBottomPolygon(familyInstance);
            if (polygonPath != null && polygonPath.Any())
            {
                Polygon outLine = new Polygon(BimConvert.ConvertToXYZs(polygonPath));
                jObject.OutLine.Add(outLine);
            }
            //Id
            context.AddBimObject(jObject);
            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 = fi.GetAllConnectors();
            foreach (var connector in connectors)
            {
                wrappers.Add(ParseCore.GetConnectorWrapper(connector));
            }
            return wrappers;
        }
    }
}
