from __future__ import annotations

from pathlib import Path
from typing import Any


OFFICIAL_LIVERY_PROJECTS_DIRNAME = "OfficialLiveryProjects"
OFFICIAL_LIVERY_LIBRARY_DIRNAME = "OfficialLiveryLibrary"
OFFICIAL_LIVERY_PACKAGE_EXPORT_DIRNAME = "OfficialLiveryPackageExport"
PREVIEW_ASSETS_DIRNAME = "PreviewAssets"
SAVED_CAR_TEMPLATES_DIRNAME = "SavedCarTemplates"
LOCAL_SKINS_DIRNAME = "LocalSkins"
TRASH_DIRNAME = "Trash"


def _base_root(base_root: str | Path) -> Path:
    return Path(base_root).expanduser()


def _child(base_root: str | Path, dirname: str, *, create: bool = False) -> Path:
    root = _base_root(base_root) / dirname
    if create:
        root.mkdir(parents=True, exist_ok=True)
    return root


def projects_root(base_root: str | Path, *, create: bool = False) -> Path:
    return _child(base_root, OFFICIAL_LIVERY_PROJECTS_DIRNAME, create=create)


def library_root(base_root: str | Path, *, create: bool = False) -> Path:
    return _child(base_root, OFFICIAL_LIVERY_LIBRARY_DIRNAME, create=create)


def package_export_root(base_root: str | Path, *, create: bool = False) -> Path:
    return _child(base_root, OFFICIAL_LIVERY_PACKAGE_EXPORT_DIRNAME, create=create)


def preview_assets_root(base_root: str | Path, *, create: bool = False) -> Path:
    root = library_root(base_root, create=create) / PREVIEW_ASSETS_DIRNAME
    if create:
        root.mkdir(parents=True, exist_ok=True)
    return root


def saved_car_templates_root(base_root: str | Path, *, create: bool = False) -> Path:
    root = library_root(base_root, create=create) / SAVED_CAR_TEMPLATES_DIRNAME
    if create:
        root.mkdir(parents=True, exist_ok=True)
    return root


def local_skins_root(base_root: str | Path, *, create: bool = False) -> Path:
    root = library_root(base_root, create=create) / LOCAL_SKINS_DIRNAME
    if create:
        root.mkdir(parents=True, exist_ok=True)
    return root


def trash_root(base_root: str | Path, *, create: bool = False) -> Path:
    root = library_root(base_root, create=create) / TRASH_DIRNAME
    if create:
        root.mkdir(parents=True, exist_ok=True)
    return root


def roots(base_root: str | Path, *, create: bool = False) -> dict[str, Any]:
    base = _base_root(base_root)
    return {
        "baseRoot": str(base),
        "officialLiveryProjectsRoot": str(projects_root(base, create=create)),
        "officialLiveryLibraryRoot": str(library_root(base, create=create)),
        "officialLiveryPackageExportRoot": str(package_export_root(base, create=create)),
        "officialPreviewAssetsRoot": str(preview_assets_root(base, create=create)),
        "officialSavedCarTemplatesRoot": str(saved_car_templates_root(base, create=create)),
        "officialLocalSkinsRoot": str(local_skins_root(base, create=create)),
        "officialTrashRoot": str(trash_root(base, create=create)),
    }
