using Autodesk.Revit.DB;
using ServiceRevitLib;
using System;
using System.IO;
using Newtonsoft.Json.Linq;
using SAGA.DotNetUtils.Extend;

//using RevitToJBim.Common;

namespace ExportStart
{
    class Program
    {
        static Program()
        {
            RevitCoreContext.Instance.Run();
        }
        [STAThread]
        static void Main(string[] args)
        {
            string command = null;
            string param = null;
            string path = null;
            if (args.Length > 0)
            {
                command = args[0];
            }
            if (args.Length > 1)
            {
                param = args[1];
            }
            if (args.Length > 2)
            {
                path = args[2];
            }
#if DEBUG
            if (string.IsNullOrWhiteSpace(command))
            {
                command = "DataExport";
                //command = "DataCheck";
                path = @"E:\导出测试\testSpace.rvt";
                path = @"E:\导出测试\编码修改后模型v1203\热源系统图模型第一版V1129.rvt";
                JObject jObject=new JObject();
                jObject.Add("ResultFileName", @"C:\Users\SAGACLOUD\AppData\Local\RevitService\Result_e26be2fd-2097-462b-bdd0-a2a86b616928.txt");
                param = jObject.ToString();

                //path= @"E:\导出测试\延庆园-B1.rvt";
                //path = @"E:\导出测试\testR18.rvt";
                //path = @"E:\导出测试\testR16.rvt";
            }
#endif
            if (string.IsNullOrEmpty(command))
            {
                Console.WriteLine("命令参数错误");
            }
            Console.WriteLine(command);
            Console.WriteLine(path);
            if (File.Exists(path) && Enum.TryParse(command, out CommandType commandType))
            {
                var app = RevitCoreContext.Instance.Application;
                var doc = app.OpenDocumentFile(path);
                string result = null;
                switch (commandType)
                {
                    case CommandType.DataCheck:
                        result=Check(doc,param);
                        break;
                    case CommandType.DataExport:
                        result=Export(doc,param);
                        break;
                }

                SaveResult(param, result);

                RevitCoreContext.Instance.Stop();
            }
            Console.WriteLine("ExportStart End");
            //强制退出
            Environment.Exit(0);
        }
        /// <summary>
        /// 保存检查或导出的结果
        /// </summary>
        /// <param name="param"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        private static void SaveResult(string param, string result)
        {
            Console.WriteLine(param);
            try
            {

                JObject jObject = JObject.Parse(param);
                string key = "ResultFileName";
                string path = jObject[key].ToString();
                if (path.IsNotNullEmpty())
                {
                    Console.WriteLine(path);
                    var  dir = Directory.GetParent(path);
                    if(!dir.Exists)
                        dir.Create();
                    File.AppendAllText(path, result);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        enum CommandType
        {
            None = 0,
            DataCheck,
            DataExport
        }


        public static string Export(Document doc,string param)
        {
            string result = null;
            try
            {
                Console.WriteLine("Start Export");
                result=RevitToJBim.MbiExport.Export(doc,param);

                Console.WriteLine("End Export");
            }
            catch (Exception e)
            {
                Console.WriteLine("导出失败");
                Console.WriteLine(e.StackTrace);
            }

            return result;
        }


        public static string Check(Document doc,string param)
        {
            string result = null;
            try
            {
                Console.WriteLine("Start DataCheck");
                result=ServiceDataCheckTest.Check(doc);

                Console.WriteLine("End DataCheck");
            }
            catch (Exception e)
            {
                Console.WriteLine("导出失败");
                Console.WriteLine(e.StackTrace);
            }

            return result;
        }


    }
}
