import os

from pathlib import Path
from dataclasses import dataclass

from dzgui.const.constants import APP_NAME_LOWER, DEBUG_LOG, SYSTEM_LOG


@dataclass
class Xdg:
    config: Path
    columns: Path
    notes: Path
    resolution: Path
    history: Path
    version: Path
    system: Path
    debug: Path
    ips: Path
    filters: Path


def is_writeable(path_str: str) -> bool:
    if path_str is None:
        return False

    path = Path(path_str)

    if not path.exists():
        try:
            path.mkdir(parents=True)
        except OSError:
            return False
        path.unlink()
        return True

    try:
        file = Path(path / "temp")
        with open(file, "w") as f:
            f.write("")
    except Exception:
        return False
    file.unlink()
    return True


def get_xdg_paths() -> dict:
    home = Path.home()
    xdg_paths = {
        "XDG_CONFIG_HOME": home / ".config",
        "XDG_STATE_HOME": home / ".local" / "state",
        "XDG_DATA_HOME": home / ".local" / "share",
        "XDG_CACHE_HOME": home / ".cache",
    }

    resolved_paths = {}
    for path in xdg_paths:
        rp = os.environ.get(path)
        if rp is not None and is_writeable(rp):
            resolved_paths[path] = Path(rp)
        else:
            resolved_paths[path] = xdg_paths[path] / APP_NAME_LOWER
    return resolved_paths


def parse_filepaths(xdg: dict) -> Xdg:
    config = xdg["XDG_CONFIG_HOME"]
    state = xdg["XDG_STATE_HOME"]

    config = config / "config.json"

    system = state / "logs" / SYSTEM_LOG
    debug = state / "logs" / DEBUG_LOG

    columns = state / "dzg.columns.json"
    notes = state / "dzg.notes.json"
    resolution = state / "dzg.res.json"
    history = state / "dzg.history"
    versions = state / "dzg.versions"
    ips = state / "ips.csv"
    filters = state / "dzg.filters.json"

    return Xdg(
        config,
        columns,
        notes,
        resolution,
        history,
        versions,
        system,
        debug,
        ips,
        filters
    )
