#if WINDOWS
using System.Runtime.InteropServices;
using Microsoft.Maui.Controls;
using Microsoft.UI.Windowing;
using WinRT.Interop;

namespace Hua.Todo.Maui.Platforms.Windows
{
    /// <summary>
    /// Windows 平台窗口操作服务。
    /// </summary>
    public class WindowsWindowService
    {
        private const int SW_HIDE = 0;
        private const int SW_SHOW = 5;
        private const int SW_RESTORE = 9;

        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        /// <summary>
        /// 隐藏窗口（不退出进程）。
        /// </summary>
        /// <param name="window">MAUI Window。</param>
        public void HideWindow(Window window)
        {
            if (window == null) return;

            var platformWindow = window.Handler?.PlatformView;
            if (platformWindow == null) return;

            var nativeWindow = (Microsoft.UI.Xaml.Window)platformWindow;
            var hWnd = WindowNative.GetWindowHandle(nativeWindow);
            if (hWnd == IntPtr.Zero) return;

            ShowWindow(hWnd, SW_HIDE);
        }

        /// <summary>
        /// 恢复窗口并显示。
        /// </summary>
        /// <param name="window">MAUI Window。</param>
        public void RestoreWindow(Window window)
        {
            if (window == null) return;

            var platformWindow = window.Handler?.PlatformView;
            if (platformWindow == null) return;

            var nativeWindow = (Microsoft.UI.Xaml.Window)platformWindow;
            var hWnd = WindowNative.GetWindowHandle(nativeWindow);
            if (hWnd == IntPtr.Zero) return;

            ShowWindow(hWnd, SW_SHOW);
            ShowWindow(hWnd, SW_RESTORE);
        }

        /// <summary>
        /// 最小化窗口。
        /// </summary>
        /// <param name="window">MAUI Window。</param>
        public void MinimizeWindow(Window window)
        {
            if (window == null) return;

            var platformWindow = window.Handler?.PlatformView;
            if (platformWindow == null) return;

            var nativeWindow = (Microsoft.UI.Xaml.Window)platformWindow;
            var appWindow = nativeWindow.AppWindow;

            if (appWindow != null)
            {
                var overlappedPresenter = appWindow.Presenter as OverlappedPresenter;
                if (overlappedPresenter != null)
                {
                    overlappedPresenter.Minimize();
                }
            }
        }
    }
}
#endif
