using Photino.NET;
using System.Drawing;
using System.Runtime.InteropServices;

namespace Lentia.Utils;

public static class WindowHelper {

    private static string baseDir = AppDomain.CurrentDomain.BaseDirectory;
    private static string iconPath = Path.Combine(baseDir, "src", "resources", "icon.ico");

    private const float StandardDpi = 96.0f;

    [DllImport("user32.dll")]
    static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    [DllImport("user32.dll")]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    const int GWL_STYLE = -16;
    const int WS_CAPTION = 0x00C00000;
    const int WS_THICKFRAME = 0x00040000;
    const int WS_MINIMIZEBOX = 0x00020000;
    const int WS_MAXIMIZEBOX = 0x00010000;
    const int WS_SYSMENU = 0x00080000;
    const uint SWP_FRAMECHANGED = 0x0020;
    const uint SWP_NOMOVE = 0x0002;
    const uint SWP_NOSIZE = 0x0001;
    const uint SWP_NOZORDER = 0x0004;

    public static Size GetScaledSize(PhotinoWindow window, int logicalWidth, int logicalHeight) {
        float currentDpi = window.ScreenDpi > 0 ? window.ScreenDpi : StandardDpi;
        float scaleFactor = currentDpi / StandardDpi;

        return new Size(
            (int)(logicalWidth * scaleFactor),
            (int)(logicalHeight * scaleFactor)
        );
    }

    public static void MakeStandardWindow(PhotinoWindow window, int width, int height, string page) {
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
            IntPtr hWnd = window.WindowHandle;
            int style = GetWindowLong(hWnd, GWL_STYLE);
            style |= WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU;
            SetWindowLong(hWnd, GWL_STYLE, style);
            SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
        }

        Size targetSize = GetScaledSize(window, width, height);
        window
            .SetResizable(true)
            .SetSize(targetSize)
            .SetMinSize(targetSize.Width, targetSize.Height)
            .Center()
            .Load(page);
    }

    public static void MakeLoginWindow(PhotinoWindow window) {
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
            IntPtr hWnd = window.WindowHandle;
            int style = GetWindowLong(hWnd, GWL_STYLE);
            style &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU);
            SetWindowLong(hWnd, GWL_STYLE, style);
            
            SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
        }

        Size loginSize = GetScaledSize(window, 310, 420);
        window
            .SetTitle("Lentia")
            .SetIconFile(iconPath)
            .SetResizable(false)
            .SetSize(loginSize)
            .Center();
    }
}