using Avalonia.Platform;
using Base.Utility;
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;
using 常用工具集.Base;

namespace 常用工具集.ViewModel._04破解及系统相关
{
    public class 猫猫回收站ViewModel : ViewModelBase
    {
        public DelegateCommand Button1Cmd { get; set; }
        public DelegateCommand Button2Cmd { get; set; }
        public 猫猫回收站ViewModel()
        {
            Button1Cmd = new DelegateCommand(Button1CmdFunc);
            Button2Cmd = new DelegateCommand(Button2CmdFunc);
        }


        /// <summary>
        /// 编程猫猫回收站
        /// </summary>
        /// <param name="obj"></param>
        private void Button1CmdFunc(object obj)
        {
            RegistryKey user = Registry.CurrentUser;
            RegistryKey CLSID = user.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CLSID\\");
            RegistryKey icon = CLSID.OpenSubKey("{645FF040-5081-101B-9F08-00AA002F954E}", true);
            RegistryKey defaultIcon = icon.CreateSubKey("DefaultIcon");
            //把dll复制到c:/System目录下
            byte[] fullDllBytes = null;
            // 获取Pack URI
            string packUri = "avares://常用工具集/Assets/Cat/full.dll";
            // 使用Pack URI打开文件并读取内容
            using (Stream stream = AssetLoader.Open(new Uri(packUri)))
            {
                fullDllBytes = new byte[stream.Length];
                stream.Read(fullDllBytes, 0, fullDllBytes.Length);
            }


            byte[] emptyDllBytes = null;
            // 获取Pack URI
            string packUri2 = "avares://常用工具集/Assets/Cat/empty.dll";
            // 使用Pack URI打开文件并读取内容
            using (Stream stream = AssetLoader.Open(new Uri(packUri2)))
            {
                emptyDllBytes = new byte[stream.Length];
                stream.Read(emptyDllBytes, 0, emptyDllBytes.Length);
            }


            string path = Environment.GetEnvironmentVariable("USERPROFILE");
            string appData = path + "\\AppData";
            DirectoryInfo directoryInfo = new DirectoryInfo(appData);
            if (!directoryInfo.Exists)
                directoryInfo.Create();
            string myDir = appData + "\\catIco";
            DirectoryInfo directoryInfo2 = new DirectoryInfo(myDir);
            if (!directoryInfo2.Exists)
                directoryInfo2.Create();
            string fullDllPath = myDir + "\\full.dll";
            string emptyDllPath = myDir + "\\empty.dll";
            if (File.Exists(fullDllPath))
                File.Delete(fullDllPath);
            if (File.Exists(emptyDllPath))
                File.Delete(emptyDllPath);
            using (FileStream stream = new FileStream(fullDllPath, FileMode.Create))
                stream.Write(fullDllBytes, 0, fullDllBytes.Length);
            using (FileStream stream = new FileStream(emptyDllPath, FileMode.Create))
                stream.Write(emptyDllBytes, 0, emptyDllBytes.Length);
            defaultIcon.SetValue("", @"%USERPROFILE%\AppData\catIco\empty.dll,0", RegistryValueKind.ExpandString);
            defaultIcon.SetValue("Full", @"%USERPROFILE%\AppData\catIco\full.dll,0", RegistryValueKind.ExpandString);
            defaultIcon.SetValue("Empty", @"%USERPROFILE%\AppData\catIco\empty.dll,0", RegistryValueKind.ExpandString);
            //重新打开桌面
            DesktopRefurbish.DeskRef();
        }


        /// <summary>
        /// 还原
        /// </summary>
        /// <param name="obj"></param>
        private void Button2CmdFunc(object obj)
        {
            RegistryKey user = Registry.CurrentUser;
            RegistryKey CLSID = user.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CLSID\\");
            RegistryKey icon = CLSID.OpenSubKey("{645FF040-5081-101B-9F08-00AA002F954E}", true);
            RegistryKey defaultIcon = icon.CreateSubKey("DefaultIcon");
            defaultIcon.SetValue("", @"%SystemRoot%\System32\imageres.dll,-55", RegistryValueKind.ExpandString);
            defaultIcon.SetValue("Full", @"%SystemRoot%\System32\imageres.dll,-54", RegistryValueKind.ExpandString);
            defaultIcon.SetValue("Empty", @"%SystemRoot%\System32\imageres.dll,-55", RegistryValueKind.ExpandString);
            //重新打开桌面
            DesktopRefurbish.DeskRef();
        }

    } 
   
}
