using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using static CollaborativePlatformMain.CADStartUtil.AcadDirs;

namespace CollaborativePlatformMain.CADStartUtil
{
    /// <summary>
    /// 
    /// <para>文件名(File Name):     StartCADUtil.cs</para>
    /// 
    /// <para>描述(Description):     启动CAD并打开图纸</para>
    /// 
    /// <para>数据表(Tables):        nothing</para>
    /// 
    /// <para>作者(Author):          Ou Rui Song</para>
    /// 
    /// <para>日期(Create Date):     2024年5月7日11:31:34</para>
    /// 
    /// 修改记录(Revision History):
    ///     R1:
    ///         修改作者:
    ///         修改日期:
    ///         修改理由:
    /// 
    /// </summary>
    public class StartCADUtil
    {

        static string location;

        public static void StartCADMath()
        {
            try
            {
                //获取本机cad路径
                AcadDirs regedits = new AcadDirs();

                //未安装CAD
                if (regedits.Dirs.Count == 0)
                {
                    MessageBox.Show(DateTime.Now + "当前电脑未安装 AutoCAD ");
                    return;
                }

                //未安装2019版CAD
                List<AcadProductKey> cadPathpks = regedits.Dirs.Where(x => x.ProductName.Equals("AutoCAD 2019 - 简体中文 (Simplified Chinese)")).ToList();
                if (cadPathpks.Count == 0)
                {
                    Console.WriteLine(DateTime.Now + "当前电脑未安装 AutoCAD 2019 - 简体中文 (Simplified Chinese)");
                    Console.ReadLine();
                    return;
                }

                var pros = Process.GetProcessesByName("acad");
                if (pros.Length != 0)
                {
                    //已打开cad

                }
                else
                {
                    location = cadPathpks[0].Location;

                    // 创建一个脚本文件并写入要执行的命令
                    //string scriptFile = location + "\\autocad_script.scr";
                    //File.WriteAllText(scriptFile, "DFBIMDwgComparisonCooperate\n");

                    // 启动AutoCAD进程并运行脚本文件
                    ProcessStartInfo startInfo = new ProcessStartInfo
                    {
                        FileName = location + "\\acad.exe",
                        //Arguments = $"/b \"{scriptFile}\"",
                        UseShellExecute = false,
                        RedirectStandardError = true,
                        RedirectStandardInput = true,
                        RedirectStandardOutput = true,
                        CreateNoWindow = true
                    };

                    using (Process autocadProcess = new Process { StartInfo = startInfo })
                    {
                        autocadProcess.Start();

                        // 等待AutoCAD进程完成
                        autocadProcess.WaitForInputIdle();

                        // 删除脚本文件
                        //File.Delete(scriptFile);
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }


    }
}
