from __future__ import annotations

import math
from dataclasses import dataclass, field

from planemapper.constants import DISPLAY_HEIGHT, DISPLAY_WIDTH


@dataclass
class MapBounds:
    home_lat: float
    home_lon: float
    radius_nm: float
    width: int = field(default=DISPLAY_WIDTH)
    height: int = field(default=DISPLAY_HEIGHT)


def project(lat: float, lon: float, bounds: MapBounds) -> tuple[int, int]:
    deg_per_nm_lat = 1 / 60
    deg_per_nm_lon = 1 / (60 * math.cos(math.radians(bounds.home_lat)))
    px_per_nm_x = (bounds.width / 2) / bounds.radius_nm
    px_per_nm_y = (bounds.height / 2) / bounds.radius_nm
    x = bounds.width // 2 + int((lon - bounds.home_lon) / deg_per_nm_lon * px_per_nm_x)
    y = bounds.height // 2 - int((lat - bounds.home_lat) / deg_per_nm_lat * px_per_nm_y)
    return (x, y)
