/*-------------------------------------------------------------------------
 * 功能描述：MBIDocument
 * 作者：xulisong
 * 创建时间： 2019/6/28 10:57:07
 * 版本号：v1.0
 *  -------------------------------------------------------------------------*/

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

namespace RevitToJBim.MBI
{
    /// <summary>
    /// 项目传输数据结构
    /// </summary>
    public class MBIDocument
    {
        public string MBIName { get; set; }
        public string ModelId { get; set; }
        public string PlanName { get; set; }
        public double BaseLevel { get; set; }
        public Dictionary<string, List<BimObject>> Elements { get; private set; }
        /// <summary>
        /// 附加元素Elements信息
        /// </summary>
        /// <param name="document"></param>
        public void AttachElements(BimDocument document)
        {
            var bimObjects = document.BimObjects;
            var group = bimObjects.GroupBy(bim => bim.ElementType);
            Dictionary<string, List<BimObject>> dic = new Dictionary<string, List<BimObject>>();
            foreach (var collection in group)
            {
                dic[collection.Key + "s"] = collection.ToList();
            }
            Elements = dic;
        }

        public static MBIDocument CreateDocument(Document document, string param)
        {
            MBIDocument dto = new MBIDocument();
            JObject jObject = JObject.Parse(param);
            dto.MBIName = jObject.GetValueEx("MBIName");//待读取
            dto.ModelId = jObject.GetValueEx("ModelId");

#if DEBUG
            //dto.MBIName = "Pj1101010038";//待读取
            //dto.ModelId = "47e62800220811ea962a7d2b043144ce";
#endif



            dto.PlanName = document.GetUseView()?.Name;
            dto.BaseLevel = document.GetUseView()?.GenLevel.Elevation.FromApi().Round(2)??0;
            return dto;
        }
    }
}
