using AudioWallpaper.Tools;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AudioWallpaper.OtherWallpaper {
    public class ExecutableWallpaper {
        public IntPtr Handle = IntPtr.Zero;
        public IntPtr MainWindowHandle = IntPtr.Zero;
        public String Url;
        public Process WallpaperProcess;
        private nint programIntPtr;

        public ExecutableWallpaper(String url) {
            this.Url = url;
        }
        public Process Start() {
            WallpaperProcess = new Process();
            WallpaperProcess.StartInfo = new ProcessStartInfo(Url);
            WallpaperProcess.Start();
            return WallpaperProcess;
        }
        public void ReShow(int x, int y, int w, int h) {

        }
        public IntPtr GetHandle() {
            if (Handle == IntPtr.Zero) {
                //获取Handle
                if (WallpaperProcess == null) {
                    throw new NullReferenceException("WallpaperProcess Is Null Or Close");
                }
                Handle = WallpaperProcess.Handle;
                Console.WriteLine(Handle);
            } else {
                return Handle;
            }
            return MainWindowHandle;
        }
        public IntPtr GetMainWindowHandle() {
            if (MainWindowHandle == IntPtr.Zero) {
                if (WallpaperProcess == null || WallpaperProcess.HasExited) {
                    throw new NullReferenceException("WallpaperProcess Is Null Or Close");
                }
                MainWindowHandle = WallpaperProcess.MainWindowHandle;
            } else {
                return MainWindowHandle;
            }
            Console.WriteLine(MainWindowHandle);
            return MainWindowHandle;
        }
        public void SetShowPatternWallpaper() {
            GetMainWindowHandle();
            GetHandle();
            Console.WriteLine("请输入句柄");
            int temph = int.Parse(Console.ReadLine());
            Console.WriteLine("..........");
            Console.WriteLine(temph);
            // 通过类名查找一个窗口，返回窗口句柄。
            programIntPtr = Win32.FindWindow("Progman", null);

            // 窗口句柄有效
            if (programIntPtr != IntPtr.Zero) {

                IntPtr result = IntPtr.Zero;

                // 向 Program Manager 窗口发送 0x52c 的一个消息，超时设置为0x3e8（1秒）。
                Win32.SendMessageTimeout(programIntPtr, 0x52c, IntPtr.Zero, IntPtr.Zero, 0, 0x3e8, result);

                // 遍历顶级窗口
                Win32.EnumWindows((hwnd, lParam) => {
                    // 找到包含 SHELLDLL_DefView 这个窗口句柄的 WorkerW
                    if (Win32.FindWindowEx(hwnd, IntPtr.Zero, "SHELLDLL_DefView", null) != IntPtr.Zero) {
                        // 找到当前 WorkerW 窗口的，后一个 WorkerW 窗口。
                        IntPtr tempHwnd = Win32.FindWindowEx(IntPtr.Zero, hwnd, "WorkerW", null);
                        // 隐藏这个窗口
                        Win32.ShowWindow(tempHwnd, 0);
                    }
                    return true;
                }, IntPtr.Zero);
            }
            Win32.SetParent(temph, programIntPtr);//这里会触发DisplayChange事件，有点玄学
            Win32.SetWindowPos(temph, IntPtr.Zero, 1920, 0, 1920, 1080, WindowPositionConstants.SWP_SHOWWINDOW);
        }
        /// <summary>
        /// 结束壁纸
        /// </summary>
        /// <returns>true 结束成功 flase 结束失败</returns>
        public bool Close() {
            if (WallpaperProcess != null && !WallpaperProcess.HasExited) {
                WallpaperProcess.Kill();
            } else {
                return true;
            }
            return WallpaperProcess.HasExited;
        }

    }
}
