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

namespace TodoList.Maui.Platforms.Windows
{
    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);

        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);
        }

        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);
        }

        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
