"""Window placement relative to screen and taskbar on Windows."""
from __future__ import annotations

import ctypes
import sys
from ctypes import wintypes


class _RECT(ctypes.Structure):
    _fields_ = [
        ("left", wintypes.LONG),
        ("top", wintypes.LONG),
        ("right", wintypes.LONG),
        ("bottom", wintypes.LONG),
    ]


def _native_work_rect() -> _RECT:
    rect = _RECT()
    ctypes.windll.user32.SystemParametersInfoW(0x0030, 0, ctypes.byref(rect), 0)
    return rect


def get_work_area(
    screen_width: int | None = None,
    screen_height: int | None = None,
) -> tuple[int, int, int, int]:
    """Work area as ``(left, top, right, bottom)`` in tkinter screen coordinates."""
    if sys.platform != "win32":
        sw = screen_width or 1920
        sh = screen_height or 1080
        return 0, 0, sw, sh - 40

    sw = screen_width or ctypes.windll.user32.GetSystemMetrics(0)
    sh = screen_height or ctypes.windll.user32.GetSystemMetrics(1)
    rect = _native_work_rect()
    metrics_w = ctypes.windll.user32.GetSystemMetrics(0)
    metrics_h = ctypes.windll.user32.GetSystemMetrics(1)

    if metrics_w <= 0 or metrics_h <= 0:
        return 0, 0, sw, sh - 40

    scale_x = sw / metrics_w
    scale_y = sh / metrics_h
    return (
        int(rect.left * scale_x),
        int(rect.top * scale_y),
        int(rect.right * scale_x),
        int(rect.bottom * scale_y),
    )


def max_window_height(
    screen_width: int,
    screen_height: int,
) -> int:
    left, top, right, bottom = get_work_area(screen_width, screen_height)
    return max(1, bottom - top)


def clamp_window_geometry(
    x: int,
    y: int,
    width: int,
    height: int,
    *,
    screen_width: int,
    screen_height: int,
) -> tuple[int, int, int]:
    """Return ``(x, y, height)`` fully inside the work area (no extra inset)."""
    left, top, right, bottom = get_work_area(screen_width, screen_height)

    max_height = max(1, bottom - top)
    max_width = max(1, right - left)
    height = min(height, max_height)
    width = min(width, max_width)

    x = max(left, min(x, right - width))
    y = max(top, min(y, bottom - height))
    return x, y, height


def margins_from_window(
    x: int,
    y: int,
    width: int,
    height: int,
    screen_width: int,
    screen_height: int,
) -> tuple[int, int]:
    """Derive bottom-right margins from a window rectangle."""
    _left, _top, right, bottom = get_work_area(screen_width, screen_height)
    margin_right = max(0, right - x - width)
    margin_above_taskbar = max(0, bottom - y - height)
    return margin_right, margin_above_taskbar


def get_bottom_right_position(
    window_width: int,
    window_height: int,
    margin_right: int = 10,
    margin_above_taskbar: int = 10,
    screen_width: int | None = None,
    screen_height: int | None = None,
) -> tuple[int, int]:
    """Top-left ``(x, y)`` so the window sits above the taskbar, bottom-right aligned."""
    sw = screen_width or (ctypes.windll.user32.GetSystemMetrics(0) if sys.platform == "win32" else 1920)
    sh = screen_height or (ctypes.windll.user32.GetSystemMetrics(1) if sys.platform == "win32" else 1080)

    left, top, right, bottom = get_work_area(sw, sh)
    x = right - margin_right - window_width
    y = bottom - margin_above_taskbar - window_height
    x, y, _height = clamp_window_geometry(
        x,
        y,
        window_width,
        window_height,
        screen_width=sw,
        screen_height=sh,
    )
    return x, y
