"""Helper functions."""
from __future__ import annotations

from datetime import datetime, timezone
from typing import Any

from pint import Quantity

DIRECTIONS = [
    "N",
    "NNE",
    "NE",
    "ENE",
    "E",
    "ESE",
    "SE",
    "SSE",
    "S",
    "SSW",
    "SW",
    "WSW",
    "W",
    "WNW",
    "NW",
    "NNW",
]
UTC = timezone.utc


def degrees_to_cardinal(degree: float | Quantity[float]) -> str:
    """Convert degrees to a cardinal direction."""
    return DIRECTIONS[
        round(
            (degree.m if isinstance(degree, Quantity) else degree)
            / (360.0 / (directions_count := len(DIRECTIONS)))
        )
        % directions_count
    ]


def truebool(val: Any | None) -> bool:
    """Return `True` if the value passed in matches a "True" value, otherwise `False`.

    "True" values are: 'true', 't', 'yes', 'y', 'on' or '1'.
    """
    return val is not None and str(val).lower() in ("true", "t", "yes", "y", "on", "1")


def utc_timestamp_from_epoch(epoch: int | None) -> datetime | None:
    """Return the UTC timestamp from an epoch value."""
    return None if epoch is None else datetime.fromtimestamp(epoch, UTC)
