/*-------------------------------------------------------------------------
 * 功能描述:ParseDoor
 * 作者:xulisong
 * 创建时间: 2019/6/25 17:53:11
 * 版本号:v1.0
 *  -------------------------------------------------------------------------*/

using RevitExport.Export;
using RevitToJBim.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JFamilyType = RevitToJBim.Common.FamilyType;
using JBIM;
using Autodesk.Revit.DB;
using JBIM.Component;
using JBIM.Definition;
using RevitToJBim.Extension;

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

        protected override List<BimId> ParseInner(ElementWrapper wrapper, JBimParseContext context)
        {
            if (!(wrapper.RefElement is FamilyInstance fi))
            {
                return null;
            }
            Door jObject = new Door();
            ParseCore.AttachObject(jObject, wrapper);
            context.AddBimObject(jObject);
            jObject.Location = ParseCore.GetLocation(fi.Location);
            //FamilyName
            jObject.FamilyName = fi.GetFamilyName();
            jObject.FamilySymbol = fi.GetFamilySymbolName();
            if (fi.Host != null)
            {
                jObject.Owner = context.Parser.ParseElement(ElementWrapperFactory.CreateWrapper(fi.Host))?.FirstOrDefault();
                jObject.Thick = (fi.Host as Autodesk.Revit.DB.Wall).Width.FtToUse();
            }
            var polygonPath = RevitUtil.GetWindowDoorLocation(fi);
            if (polygonPath != null && polygonPath.Any())
            {
                Polygon outLine = new Polygon(BimConvert.ConvertToXYZs(polygonPath,true));
                jObject.OutLine.Add(outLine);
            }
            jObject.HandDirection = BimConvert.ConvertToDirection(fi.HandOrientation);
            jObject.FaceDirection = BimConvert.ConvertToDirection(fi.FacingOrientation);
            return new List<BimId>() { jObject.Id };
        }

    }
}