using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace CollaborativePlatformMain.CADStartUtil
{
    /// <summary>
    /// 
    /// <para>文件名(File Name):     SendToCAD.cs</para>
    /// 
    /// <para>描述(Description):     发送命令到CAD</para>
    /// 
    /// <para>数据表(Tables):        nothing</para>
    /// 
    /// <para>作者(Author):          Ou Rui Song</para>
    /// 
    /// <para>日期(Create Date):     2024年5月7日11:28:52</para>
    /// 
    /// 修改记录(Revision History):
    ///     R1:
    ///         修改作者:
    ///         修改日期:
    ///         修改理由:
    /// 
    /// </summary>
    public class SendToCAD
    {
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        private static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);

        public static void SendCommandToAutoCAD(string toSend, IntPtr hwnd)
        {
            const int WM_COPYDATA = 0x4A;
            COPYDATASTRUCT cds = new COPYDATASTRUCT();
            cds.dwData = new IntPtr(1);
            string data = toSend + "\0";
            cds.cbData = data.Length * Marshal.SystemDefaultCharSize;
            cds.lpData = Marshal.StringToCoTaskMemAuto(data);
            SendMessageW(hwnd, WM_COPYDATA, IntPtr.Zero, ref cds);
            Marshal.FreeCoTaskMem(cds.lpData);
        }

        private struct COPYDATASTRUCT
        {
            public IntPtr dwData;

            public int cbData;

            public IntPtr lpData;

        }

    }



    /// <summary>
    /// 
    /// <para>文件名(File Name):     AcadDirs.cs</para>
    /// 
    /// <para>描述(Description):     CAD注册表环境读取</para>
    /// 
    /// <para>数据表(Tables):        nothing</para>
    /// 
    /// <para>作者(Author):          Li Wen Jin</para>
    /// 
    /// <para>日期(Create Date):     2023年10月20日13:43:58</para>
    /// 
    /// 修改记录(Revision History):
    ///     R1:
    ///         修改作者:
    ///         修改日期:
    ///         修改理由:
    /// 
    /// </summary>
    public class AcadDirs
    {
        public List<AcadProductKey> Dirs { get; }
        /// <summary>
        /// 通过注册表获取本机所有cad的安装路径
        /// </summary>
        public AcadDirs()
        {
            Dirs = new List<AcadProductKey>();

            RegistryKey Adsk = Registry.CurrentUser.OpenSubKey(@"Software\Autodesk\AutoCAD");
            if (Adsk == null)
            {
                //Console.WriteLine("Adsk == null");
                return;
            }
            foreach (string ver in Adsk.GetSubKeyNames())
            {
                try
                {
                    RegistryKey emnuAcad = Adsk.OpenSubKey(ver);
                    var curver = emnuAcad.GetValue("CurVer");
                    if (curver == null)
                    {
                        //Console.WriteLine("curver == null");
                        continue;
                    }
                    string app = curver.ToString();
                    string fmt = @"Software\Autodesk\AutoCAD\{0}\{1}";
                    emnuAcad = Registry.LocalMachine.OpenSubKey(string.Format(fmt, ver, app));
                    if (emnuAcad == null)
                    {
                        fmt = @"Software\Wow6432Node\Autodesk\AutoCAD\{0}\{1}";
                        emnuAcad = Registry.LocalMachine.OpenSubKey(string.Format(fmt, ver, app));
                    }

                    var acadLocation = emnuAcad.GetValue("AcadLocation");
                    if (acadLocation == null)
                    {
                        //Console.WriteLine("acadLocation == null");
                        continue;
                    }
                    string location = acadLocation.ToString();
                    if (File.Exists(location + "\\acad.exe"))
                    {
                        var produ = emnuAcad.GetValue("ProductName");
                        if (produ == null)
                        {
                            //Console.WriteLine("produ == null");
                            continue;
                        }
                        string productname = produ.ToString();
                        var release = emnuAcad.GetValue("Release");
                        if (release == null)
                        {
                            //Console.WriteLine("release == null");
                            continue;
                        }
                        string[] strVer = release.ToString().Split('.');
                        var pro = new AcadProductKey()
                        {
                            ProductKey = emnuAcad,
                            Location = location,
                            ProductName = productname,
                            Version = double.Parse(strVer[0] + "." + strVer[1]),
                        };
                        Dirs.Add(pro);
                    }
                }
                catch { }
            }
        }

        public struct AcadProductKey
        {
            /// <summary>
            /// 注册表位置
            /// </summary>
            public RegistryKey ProductKey;
            /// <summary>
            /// cad安装路径
            /// </summary>
            public string Location;
            /// <summary>
            /// cad名称
            /// </summary>
            public string ProductName;
            /// <summary>
            /// cad版本号 17.1之类的
            /// </summary>
            public double Version;
        }
    }

}
