/*-------------------------------------------------------------------------
 * 功能描述：ExportDataBuilder
 * 作者：xulisong
 * 创建时间： 2019/6/24 9:59:47
 * 版本号：v1.0
 *  -------------------------------------------------------------------------*/

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using RevitExport.Export;
using RevitToJBim.Common;
using SAGA.RevitUtils.Extends;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RevitToJBim.Extension;
using Autodesk.Revit.DB.Architecture;

namespace RevitToJBim
{
    public class ExportDataBuilder
    {
        /// <summary>
        /// 创建数据源
        /// </summary>
        /// <returns></returns>
        public static List<ElementWrapper> CreateExportData(Document doc)
        {
            List<ElementWrapper> wrappers = new List<ElementWrapper>();
            //应对传入元素，不是document全集的情况
            //FilteredElementCollector collector = new FilteredElementCollector(doc, elements.Select(e => e.Id).ToList());
            #region Unit
            var formatOptions = doc.GetUnits().GetFormatOptions(UnitType.UT_Length);
            ElementWrapper wrapper = new ElementWrapper(formatOptions, null);
            wrappers.Add(wrapper);
            #endregion
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            wrappers.AddRange(collector.Clone().FilterElements<Wall>().Select(e => new ElementWrapper(e)));
            wrappers.AddRange(collector.Clone().FilterElements<CurveElement>(BuiltInCategory.OST_MEPSpaceSeparationLines).Select(e => new ElementWrapper(e)));
            //space
            var sourceSpaces = collector.Clone().FilterElements<SpatialElement>(BuiltInCategory.OST_MEPSpaces).OfType<Space>().Where(t=>t.IsSpace()).ToList();
            wrappers.AddRange(sourceSpaces.Select(e => new ElementWrapper(e)));
            //room
            var sourceRooms = collector.Clone().FilterElements<SpatialElement>(BuiltInCategory.OST_Rooms).OfType<Room>().ToList();
            wrappers.AddRange(sourceRooms.Select(e => new ElementWrapper(e)));

            var mepCurves = doc.FilterElements<MEPCurve>().ToList();
            wrappers.AddRange(mepCurves.Select(e => new ElementWrapper(e)));
            //wrappers.AddRange(collector.Clone().FilterElements<Pipe>().Select(e => new ElementWrapper(e)));
            //wrappers.AddRange(collector.Clone().FilterElements<Duct>().Select(e => new ElementWrapper(e)));
            var familyInstances = collector.Clone().FilterElements<FamilyInstance>();
            foreach (FamilyInstance familyInstance in familyInstances)
            {
                var tempWrapper = ElementWrapperFactory.CreateWrapper(familyInstance);
                if (tempWrapper != null)
                {
                    wrappers.Add(tempWrapper);
                }
            }
            wrappers.AddRange(collector.Clone().FilterElements<Level>().Select(e => new ElementWrapper(e)));
            wrappers.AddRange(collector.Clone().FilterElements<Grid>().Select(e => new ElementWrapper(e)));
            return wrappers;
        }
    }
}
