/*-------------------------------------------------------------------------
 * 功能描述：WallExtension
 * 作者：xulisong
 * 创建时间： 2019/6/26 17:57:20
 * 版本号：v1.0
 *  -------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using SAGA.DotNetUtils.Extend;
using SAGA.RevitUtils;
using SAGA.RevitUtils.Extends;

namespace RevitToJBim.Extension
{
    public static class WallExtension
    {
        /// <summary>
        /// 获取墙底面的轮廓，主要处理门窗分割墙问题
        /// </summary>
        /// <param name="wall"></param>
        /// <returns></returns>
        public static List<PlanarFace> GetBottomFaces(this Wall wall)
        {
            var solids = Extension.GeometryElementExtension.GetSolids(wall);
            //var topZ = wall.GetTopStaticHeight();
            
            
            double volume = double.MinValue;
            List<PlanarFace> faces = new List<PlanarFace>();
            foreach (var solid in solids)
            {
                var tempVolume = solid.Volume;
                double topZ = double.MaxValue;
                //取体积最大的Solid
                if (tempVolume.IsThan(volume))
                {
                    volume = tempVolume;
                    faces.Clear();
                }
                else
                {
                    continue;
                }
                foreach (Face face in solid.Faces)
                {
                    if (face is PlanarFace planarFace && planarFace.FaceNormal.IsEqual(-XYZ.BasisZ))
                    {
                        var tempZ = planarFace.Origin.Z;
                        //取底面FaceNormal为（0，0，-1），Z最小
                        if (tempZ.IsLess(topZ))
                        {
                            faces.Clear();
                            topZ = tempZ;
                            faces.Add(planarFace);
                        }
                        else if(tempZ.IsEqual(topZ))
                        {
                            faces.Add(planarFace);
                        }
                       
                    }
                }
            }
            return faces;
        }
    }
}
