/*-------------------------------------------------------------------------
 * 功能描述：解析设备设施
 * 作者：Garrett
 * 创建时间： 
 * 版本号：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 Autodesk.Revit.DB.Plumbing;
using JBIM;
using JBIM.Component;
using JBIM.Definition;
using RevitExport;
using RevitExport.Export;
using RevitToJBim.Common;
using RevitToJBim.Extension;
using RevitToJBim.ParseData;
using SAGA.RevitUtils.Extends;
using SAGA.RevitUtils.MEP;
using FamilyType = RevitToJBim.Common.FamilyType;
using JEquip = JBIM.Component.Equipment;

namespace RevitToJBim.ComponentParse
{
    [UsableParse]
    public class ParseFacility : ParseBase
    {
        public override List<string> FastIndex()
        {
            return new List<string>() { CategoryGenerator.BuildingCategory(FamilyType.Facility) };
        }

        public override bool Match(ElementWrapper wrapper)
        {
            return wrapper.RefElement.IsEquipment()||wrapper.RefElement.IsEquipmentPart();
        }

        protected override List<BimId> ParseInner(ElementWrapper wrapper, JBimParseContext context)
        {
            if (!(wrapper.RefElement is FamilyInstance familyInstance))
            {
                return null;
            }
            //ElementType
            JEquip jObject = GetJEquip(familyInstance);
            //Name,SourceId
            ParseCore.AttachObject(jObject, wrapper);
            //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);
            }
            //FamilyName
            jObject.FamilyName = familyInstance.GetFamilyName();
            //Tag
            jObject.Tag = "";
            //Parameters
            jObject.Parameters = RevitUtil.GetFacilityParameters(familyInstance);

            //Id
            context.AddBimObject(jObject);

            #region 关联数据处理相关
            //Owner
            var parentElement = RevitUtil.GetEquipPartParent(familyInstance);
            if (parentElement != null&&jObject is EquipPart jPart)
            {
                jPart.Owner = context.Parser.ParseElement(ElementWrapperFactory.CreateWrapper(parentElement)).FirstOrDefault()?.Id;
            }
            
            #region Connector连接关系
            var connectors = familyInstance.GetAllConnectors();
            ElementOneToManyRel relMany = ParseCore.GetConnectorRels(familyInstance,connectors);

            if (relMany!=null)
            {
                context.RelationShips.Add(relMany);
            }
            #endregion
            
            #endregion
            return new List<BimId>() { jObject.Id };
        }

        public override List<ElementWrapper> ArrangeRefElements(ElementWrapper wrapper, JBimParseContext context)
        {
            var element = wrapper.RefElement;
            var wrappers = new List<ElementWrapper>() { };
            var connectors = element.GetAllConnectors();
            foreach (var connector in connectors)
            {
                wrappers.Add(ParseCore.GetConnectorWrapper(connector));
            }
            return wrappers;
        }

        private  JEquip GetJEquip(FamilyInstance fi)
        {
            JEquip jObject = null;
            if(fi.IsEquipment())
                jObject=new JEquip();
            else if (fi.IsEquipmentPart())
            {
                jObject = new EquipPart();
            }
            return jObject;
        }
    }
}
