/*-------------------------------------------------------------------------
 * 功能描述：TestExport
 * 作者：xulisong
 * 创建时间： 2019/6/18 15:43:54
 * 版本号：v1.0
 *  -------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.IO;
using Autodesk.Revit.DB;
using MBIRevitBase.Services;
using MBIRevitBase.Tools;
using Newtonsoft.Json;
using RevitExport.Export;
using RevitToJBim.ComponentParse;
using RevitToJBim.JsonConverter;
using RevitToJBim.MBI;
using ServiceRevitLib.Mode;

namespace RevitToJBim
{
    public class MbiExport
    {
        public static string Export(Document document, string param)
        {
            string result = null;
            //导出类
            try
            {
                var wrappers = ExportDataBuilder.CreateExportData(document);
                ExportInstance report = new ExportInstance(wrappers);
                //解析类
                JBimParseContext context = new JBimParseContext(ParseCore.GetUseParsers());
                context.Parser.Parse(report);

                //构建需要结果
                var mbiDocument = MBIDocument.CreateDocument(document, param);
                mbiDocument.AttachElements(context.GetResultDocument());
                /*
                 * 序列化的变化封装在这里，增加MBIDocument传输数据对象，
                 * 增加一些和JBIM无关，又和当前业务逻辑相关的属性
                 */
                var json = BimJsonUtil.Serializer(mbiDocument);
                var resultResponse = UploadService.UploadExportFile(json);
                result = BimJsonUtil.Serializer((ResultBase)resultResponse);

                //保存到本地
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
                string path = Path.Combine($"{document.PathName}_{fileName}.json");
                //File.WriteAllText(path, json.ToString());
            }
            catch (Exception ex)
            {
                result = BimJsonUtil.Serializer(ResultBase.Exception(ex.Message + ex.StackTrace));
            }

            return result;
        }
    }
}
