/*-------------------------------------------------------------------------
 * 功能描述：ParseSpace
 * 作者：xulisong
 * 创建时间： 2019/6/17 14:28:03
 * 版本号：v1.0
 *  -------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB.Mechanical;
using JBIM;
using RevitExport;
using RevitToJBim.Common;
using SAGA.RevitUtils.Extends;
using JSpace = JBIM.Component.Space;
using JBoundarySegment = JBIM.Component.BoundarySegment;
using Autodesk.Revit.DB;
using JBIM.Definition;
using RevitExport.Export;
using RevitToJBim.Extension;
using RevitToJBim.ParseData;

namespace RevitToJBim.ComponentParse
{
    [UsableParse]
    public class ParseSpace : ParseBase
    {
        public override List<string> FastIndex()
        {
            return new List<string>() {typeof(Space).FullName};
        }
        public override bool Match(ElementWrapper wrapper)
        {
            return wrapper.RefElement is Space;
        }
        protected override List<BimId> ParseInner(ElementWrapper wrapper, JBimParseContext context)
        {
            if (!(wrapper.RefElement is Space space))
            {
                return null;
            }
            //if (!space.IsSpace())
            //{
            //    return null;
            //}
            JSpace bimObject = new JSpace();
            ParseCore.AttachObject(bimObject, wrapper);
            var location = space.Location.GetPoint();
            if (location != null)
            {
                bimObject.Location =
                    GeometryLocation.CreatePointLocation(BimConvert.ConvertToXYZ(location));
            }
            bimObject.Height = (space.GetTopStaticHeight() - space.GetBaseStaticHeight()).FtToUse();

            //Parameters
            bimObject.Parameters = RevitUtil.GetSpaceParameters(space);
            var options = new SpatialElementBoundaryOptions();
            //options.SpatialElementBoundaryLocation = SpatialElementBoundaryLocation.Finish;
            var segments = space.GetBoundarySegments(options);
            if (segments != null)
            {
                foreach (IList<BoundarySegment> segmentList in segments)
                {
                    Polygon outLine = new Polygon();
                    List<BimId> segmentBimids = new List<BimId>();
                    foreach (BoundarySegment segment in segmentList)
                    {
                        var points = segment.GetCurve().GetPoints();
                        var usePoints = points.Select(xyz => BimConvert.ConvertToXYZ(xyz)).ToList();
                        outLine.AddRange(usePoints.GetRange(0, usePoints.Count - 1));

                        #region 解析线段类

                        JBoundarySegment jSegment = new JBoundarySegment();
                        jSegment.Curve.AddRange(usePoints);
                        var jSegmentId=context.AddBimObject(jSegment);
                        segmentBimids.Add(jSegmentId);
                        var referenceElementId = segment.ElementId;
                        if (referenceElementId != null)
                        {
                            ElementOneToOneRel rel = new ElementOneToOneRel(jSegmentId.ToString(), referenceElementId.ToString());
                            rel.SetElementType(TypeDefinition.Property_Reference);
                            rel.RelatingObjectIsBimId = true;
                            context.RelationShips.Add(rel);
                        }                 
                        #endregion

                    }                 
                    if (outLine.Any())
                    {
                        StandardUtil.ArrangeLoop(outLine);
                        bimObject.OutLine.Add(outLine);
                    }

                    if (segmentBimids.Any())
                    {
                        bimObject.BoundarySegments.Add(segmentBimids);
                    }
                }
                //外轮廓
                bimObject.OutLine2 = space.GetSpaceOutline();
            }
 
            context.AddBimObject(bimObject);
            return new List<BimId>();
        }
    }
}
