"""UI layout helpers for small embedded displays such as 640x480 and 720x720."""

from __future__ import annotations

import math
from dataclasses import dataclass
from pathlib import Path

# Base screen
SCREEN_W = 640
SCREEN_H = 480

# Colors (R, G, B, A)
BG_COLOR = (24, 24, 32, 255)
PLAYBACK_BG_COLOR = (0, 0, 0, 255)
TEXT_COLOR = (220, 220, 220, 255)
HIGHLIGHT_BG = (50, 80, 160, 255)
HIGHLIGHT_TEXT = (255, 255, 255, 255)
DIM_TEXT = (140, 140, 150, 255)
ERROR_COLOR = (220, 60, 60, 255)
HEADER_BG = (32, 32, 48, 255)
STATUS_BG = (20, 20, 28, 255)

# Typography
FONT_SIZE = 20
HEADER_FONT_SIZE = 24
PLAYBACK_FONT_SIZE = 14

# Timing
TARGET_FPS = 30
FRAME_DELAY_MS = 1000 // TARGET_FPS
PLAYBACK_HUD_REFRESH_MS = 250
PLAYBACK_HUD_AUTOHIDE_MS = 2500

PLAYBACK_HUD_HIDDEN = 0
PLAYBACK_HUD_AUTO = 1
PLAYBACK_HUD_PINNED = 2

PACKAGE_ROOT = Path(__file__).resolve().parents[1]
ASSETS_DIR = PACKAGE_ROOT / "assets"
ICONS_DIR = ASSETS_DIR / "icons"
ICON_SIZE = 20

# Playback HUD / video viewport
PLAYBACK_HUD_TOP = 30
PLAYBACK_HUD_BOTTOM = 50
PLAYBACK_HUD_SIDE = 10
PLAYBACK_PROGRESS_H = 8
PLAYBACK_OVERLAY_BG = (10, 14, 22, 170)
PLAYBACK_OVERLAY_ACCENT = (84, 124, 224, 255)
PLAYBACK_OVERLAY_DIM = (220, 220, 220, 72)
PLAYBACK_PILL_BG = (8, 12, 18, 210)

PLAYBACK_HUD_ICON_SIZE = 16

# Font search paths (in order)
FONT_SEARCH_PATHS = [
    str(ASSETS_DIR / "NotoSans-Regular.ttf"),
    "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
    "/usr/share/fonts/TTF/DejaVuSans.ttf",
    "/usr/share/fonts/dejavu-sans-fonts/DejaVuSans.ttf",
    "/usr/share/fonts/google-noto-vf/NotoSans[wght].ttf",
    "/usr/share/fonts/liberation-sans-fonts/LiberationSans-Regular.ttf",
]


@dataclass(frozen=True)
class Layout:
    width: int
    height: int
    ui_scale: float
    font_size: int
    header_font_size: int
    playback_font_size: int
    header_h: int
    status_h: int
    list_top: int
    list_bottom: int
    list_item_h: int
    visible_items: int
    list_pad_left: int
    list_pad_right: int
    page_size: int
    icon_size: int
    playback_hud_top: int
    playback_hud_bottom: int
    playback_hud_side: int
    playback_progress_h: int
    playback_hud_icon_size: int
    playback_video_top: int
    playback_video_bottom: int
    playback_video_left: int
    playback_video_right: int
    playback_status_w: int
    playback_time_w: int
    playback_buffer_w: int
    playback_bottom_margin: int


def _scaled(value: int, scale: float, minimum: int = 1) -> int:
    return max(minimum, int(round(value * scale)))


def _ui_scale(width: int, height: int) -> float:
    width = max(1, width)
    height = max(1, height)
    return max(0.85, min(1.6, math.sqrt((width / SCREEN_W) * (height / SCREEN_H))))


def get_layout(width: int, height: int) -> Layout:
    width = max(1, width)
    height = max(1, height)
    scale = _ui_scale(width, height)

    font_size = _scaled(FONT_SIZE, scale)
    header_font_size = _scaled(HEADER_FONT_SIZE, scale)
    playback_font_size = _scaled(PLAYBACK_FONT_SIZE, scale)
    header_h = _scaled(40, scale)
    status_h = _scaled(32, scale)
    list_item_h = _scaled(28, scale)
    list_pad_left = _scaled(12, scale)
    list_pad_right = _scaled(12, scale)
    icon_size = _scaled(20, scale)
    playback_hud_top = _scaled(30, scale)
    playback_hud_bottom = _scaled(50, scale)
    playback_hud_side = _scaled(10, scale)
    playback_progress_h = _scaled(8, scale, minimum=4)
    playback_hud_icon_size = _scaled(16, scale)
    playback_bottom_margin = _scaled(20, scale)

    list_top = header_h
    list_bottom = max(list_top + list_item_h, height - status_h)
    visible_items = max(1, (list_bottom - list_top) // list_item_h)

    playback_status_w = max(_scaled(70, scale), min(width // 6, _scaled(110, scale)))
    playback_time_w = max(_scaled(156, scale), min(width // 3, _scaled(220, scale)))
    playback_buffer_w = max(_scaled(180, scale), min(width // 3, _scaled(240, scale)))

    return Layout(
        width=width,
        height=height,
        ui_scale=scale,
        font_size=font_size,
        header_font_size=header_font_size,
        playback_font_size=playback_font_size,
        header_h=header_h,
        status_h=status_h,
        list_top=list_top,
        list_bottom=list_bottom,
        list_item_h=list_item_h,
        visible_items=visible_items,
        list_pad_left=list_pad_left,
        list_pad_right=list_pad_right,
        page_size=visible_items,
        icon_size=icon_size,
        playback_hud_top=playback_hud_top,
        playback_hud_bottom=playback_hud_bottom,
        playback_hud_side=playback_hud_side,
        playback_progress_h=playback_progress_h,
        playback_hud_icon_size=playback_hud_icon_size,
        playback_video_top=playback_hud_top,
        playback_video_bottom=height - playback_hud_bottom,
        playback_video_left=0,
        playback_video_right=width,
        playback_status_w=playback_status_w,
        playback_time_w=playback_time_w,
        playback_buffer_w=playback_buffer_w,
        playback_bottom_margin=playback_bottom_margin,
    )


DEFAULT_LAYOUT = get_layout(SCREEN_W, SCREEN_H)
