using Avalonia;
using Avalonia.Markup.Xaml.MarkupExtensions;
using Avalonia.Platform;
using MES.Utility.Core;
using Microsoft.Win32;
using System;
using System.IO;
using Ursa.Controls;
using 常用工具集.Base;

namespace 常用工具集.ViewModel._04破解及系统相关
{
    public class AdobeAcrobatXI破解ViewModel : ViewModelBase
    {
        public string FilePath { get; set; } = "";
        public DelegateCommand FindPathCmd { get; set; }
        public DelegateCommand CrackCmd { get; set; }

        public AdobeAcrobatXI破解ViewModel()
        {
            FindPathCmd = new DelegateCommand(FindPathCmdFunc);
            CrackCmd = new DelegateCommand(CrackCmdFunc);
        }



        private void FindPathCmdFunc(object obj)
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software");
            registryKey = registryKey.OpenSubKey("Adobe");
            if (registryKey == null)
            {
                MessageBox.ShowAsync("当前计算机未安装Adobe Acrobat XI");
                return;
            }
            registryKey = registryKey.OpenSubKey("Adobe Acrobat");
            if (registryKey == null)
            {
                MessageBox.ShowAsync("当前计算机未安装Adobe Acrobat XI");
                return;
            }
            registryKey = registryKey.OpenSubKey("11.0");
            if (registryKey == null)
            {
                MessageBox.ShowAsync("当前计算机未安装Adobe Acrobat XI");
                return;
            }
            registryKey = registryKey.OpenSubKey("InstallPath");
            if (registryKey == null)
            {
                MessageBox.ShowAsync("获得路径失败，请手动输入路径");
                return;
            }
            string path = registryKey.GetValue("").ToString();
            FilePath = path;
        }


        private void CrackCmdFunc(object obj)
        {
            try
            {
                string path = FilePath;
                if (path.IsNullOrEmpty())
                {
                    MessageBox.ShowAsync("请查找或者输入Adobe Acrobat XI安装目录");
                    return;
                }
                if (!Directory.Exists(path))
                {
                    MessageBox.ShowAsync("文件夹不存在，破解失败");
                    return;
                }
                path = path.Replace("\\", "/");
                if (!path.EndsWith("/"))
                    path += "/";
                path += "amtlib.dll";

                byte[] bytes = null;
                // 获取Pack URI
                string packUri = "avares://常用工具集/Assets/AcrobatXI/amtlib.dll";
                // 使用Pack URI打开文件并读取内容
                if (!File.Exists(path))
                {
                    MessageBox.ShowAsync("amtlib.dll文件不存在，请手动输入路径");
                    return;
                } 
                using (Stream stream = AssetLoader.Open(new Uri(packUri)))
                {
                    bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, bytes.Length);
                }
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    using (BinaryWriter bw = new BinaryWriter(fs))
                    {
                        bw.Write(bytes);
                    }
                }
                MessageBox.ShowAsync("破解完成");
            }
            catch (Exception ex)
            {
                MessageBox.ShowAsync("破解失败:" + ex.Message);
            }
        }
    }
}
