//wangwei 2020.11.23
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 JRoom = JBIM.Component.Room;
using JBoundarySegment = JBIM.Component.BoundarySegment;
using Autodesk.Revit.DB;
using JBIM.Definition;
using RevitExport.Export;
using RevitToJBim.Extension;
using RevitToJBim.ParseData;
using Autodesk.Revit.DB.Architecture;
using SAGA.DotNetUtils.Extend;

namespace RevitToJBim.ComponentParse
{
    [UsableParse]
    //仿照ParseSpace
    public class ParseRoom : ParseBase
    {
        public override List<string> FastIndex()
        {
            return new List<string>() { typeof(Room).FullName };
        }
        public override bool Match(ElementWrapper wrapper)
        {
            return wrapper.RefElement is Room;
        }
        protected override List<BimId> ParseInner(ElementWrapper wrapper, JBimParseContext context)
        {
            if (!(wrapper.RefElement is Room room))
            {
                return null;
            }
            JRoom bimObject = new JRoom();
            ParseCore.AttachObject(bimObject, wrapper);
            bimObject.Name = GetSpaceDefaultName(room);
            var location = room.Location.GetPoint();
            if (location != null)
            {
                bimObject.Location =
                    GeometryLocation.CreatePointLocation(BimConvert.ConvertToXYZ(location));
            }
            bimObject.Height = room.GetParameterDouble(BuiltInParameter.ROOM_HEIGHT);

            //Parameters
            bimObject.Parameters = RevitUtil.GetRoomParameters(room);
            var options = new SpatialElementBoundaryOptions();
            //options.SpatialElementBoundaryLocation = SpatialElementBoundaryLocation.Finish;
            var segments = room.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 = null; //space.GetSpaceOutline();
            }

            context.AddBimObject(bimObject);
            return new List<BimId>();
        }

        //从SAGA.RevitUtils拷贝而来
        public static string GetSpaceDefaultName(Room room)
        {
            string defaultName = room.Name;
            var roomname = room.GetParameterString(BuiltInParameter.ROOM_NAME);
            if (roomname == "房间" || roomname.IsNullOrEmptyExt()) return defaultName;
            defaultName = roomname;
            return defaultName;
        }
    }
}
