#!/usr/bin/env python3
"""Write EdgeWeightGPS solver highway presets as one raw float32 value."""

from __future__ import annotations

import argparse
import struct
from pathlib import Path


DEFAULT_INSTALL_PATH = Path(
    "/var/home/salt/.var/app/com.valvesoftware.Steam/.local/share/Steam/steamapps/common/"
    "Cyberpunk 2077/red4ext/plugins/EdgeWeightGPS/solver_weights.bin"
)

PRESETS: dict[str, float] = {
    "vanilla": 1.00,
    "default": 0.80,
    "mild": 0.90,
    "strong": 0.70,
    "proof-highway-cheap": 0.35,
    "highway-expensive": 3.00,
}


def parse_multiplier(text: str) -> float:
    value = float(text)
    if not 0.0 < value < 20.0:
        raise argparse.ArgumentTypeError("multiplier must be greater than 0 and less than 20")
    return value


def write_weight(path: Path, value: float) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(struct.pack("<f", value))


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("preset", nargs="?", choices=sorted(PRESETS), default="default")
    parser.add_argument("--value", type=parse_multiplier, help="explicit highway multiplier")
    parser.add_argument("--output", type=Path, default=DEFAULT_INSTALL_PATH)
    parser.add_argument("--preset-dir", type=Path, help="write every named preset into this directory")
    args = parser.parse_args()

    if args.preset_dir:
        for name, value in PRESETS.items():
            path = args.preset_dir / f"{name}.bin"
            write_weight(path, value)
            print(f"{path}: {value}")
        return 0

    value = args.value if args.value is not None else PRESETS[args.preset]
    write_weight(args.output, value)
    print(f"{args.output}: {value}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
