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

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using RevitExport;
using RevitExport.Export;
using RevitToJBim.ComponentParse;
using SAGA.RevitUtils;
using SAGA.RevitUtils.Extends;

namespace RevitToJBim
{
    public class TestExport
    {
        public static Document Document { get; set; }
        public static void Export()
        {
            var elements = Document.GetElements(typeof(Pipe));
            var elements2 = Document.GetElements(typeof(SpatialElement)).OfType<Space>().ToList();
            var elements3 = Document.GetElements(typeof(Duct)).OfType<Duct>().ToList();
            elements.AddRange(elements2);
            elements.AddRange(elements3);
            var wrappers = ExportDataBuilder.CreateExportData(Document);// elements.Select(e => new ElementWrapper(e)).ToList();
            ExportInstance report = new ExportInstance(wrappers);
            JBimParseContext context = new JBimParseContext(ParseCore.GetUseParsers());
            var dd = false;
            context.Parser.Parse(report);
            var result = context.Serialize();
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
            string path = Path.Combine(@"D:\", $"{fileName}.json");
            File.WriteAllText(path, result);
            System.Diagnostics.Process.Start("notepad.exe", path);
        }
    }
    /// <summary>
    /// 测试提取数据
    /// </summary>
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class PickDataCommand : IExternalCommand, IExternalCommandAvailability
    {

        public  Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                TestExport.Document = commandData.Application.ActiveUIDocument.Document;
                TestExport.Export();
                TaskDialog dialog = new TaskDialog("导出");
                dialog.MainInstruction ="导出成功";
                dialog.Show();
            }
            catch (Exception e)
            {
                TaskDialog dialog = new TaskDialog("导出");
                dialog.MainInstruction =e.Message+"\n\t"+ e.StackTrace.ToString();
                dialog.Show();
                return Result.Cancelled;
            }
            return Result.Succeeded;
        }


        /// <summary>
        /// Onlys show the dialog when a document is open, as Dockable dialogs are only available
        /// when a document is open.
        /// </summary>
        public  bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
        {
            return false;
        }
    }
}
