from __future__ import annotations

import argparse
from pathlib import Path

from .dictionary_tool import generate_dictionary, load_dictionary, save_dictionary
from .font_builder import build_font_from_dictionary
from .specimen import build_specimen
from .translator import translate_en_to_lang, translate_lang_to_en


def _cmd_generate_dictionary(args: argparse.Namespace) -> int:
    specs = generate_dictionary(count=args.count, model=args.model)
    save_dictionary(specs, Path(args.out))
    print(f"Dictionary saved: {args.out} ({len(specs)} roots)")
    return 0


def _cmd_build_font(args: argparse.Namespace) -> int:
    out = build_font_from_dictionary(
        dict_path=Path(args.dict),
        out_path=Path(args.out),
        family_name=args.family,
    )
    print(f"Font saved: {out}")
    print(f"Glyph map: {out.with_name('glyph_map.json')}")
    return 0


def _cmd_translate(args: argparse.Namespace) -> int:
    specs = load_dictionary(Path(args.dict))
    if args.reverse:
        translated = translate_lang_to_en(args.text, specs)
    else:
        translated = translate_en_to_lang(args.text, specs)
    print(translated)
    return 0


def _cmd_build_specimen(args: argparse.Namespace) -> int:
    out = build_specimen(
        dict_path=Path(args.dict),
        font_path=Path(args.font),
        out_path=Path(args.out),
    )
    print(f"Specimen saved: {out}")
    return 0


def build_parser() -> argparse.ArgumentParser:
    p = argparse.ArgumentParser(description="Agglutinative language toolkit")
    sp = p.add_subparsers(dest="command", required=True)

    g = sp.add_parser("generate-dictionary", help="Generate root dictionary using g4f")
    g.add_argument("--count", type=int, default=96, help="Number of roots")
    g.add_argument("--model", default="gpt-5-mini", help="g4f model name")
    g.add_argument("--out", default="data/dictionary.json", help="Output dictionary JSON")
    g.set_defaults(func=_cmd_generate_dictionary)

    b = sp.add_parser("build-font", help="Build .ttf font from dictionary")
    b.add_argument("--dict", default="data/dictionary.json", help="Input dictionary JSON")
    b.add_argument("--out", default="dist/LogiGlyph.ttf", help="Output TTF path")
    b.add_argument("--family", default="LogiGlyph", help="Font family name")
    b.set_defaults(func=_cmd_build_font)

    t = sp.add_parser("translate", help="Translate between English and symbol language")
    t.add_argument("--dict", default="data/dictionary.json", help="Input dictionary JSON")
    t.add_argument("--text", required=True, help="Text to translate")
    t.add_argument("--reverse", action="store_true", help="Translate from language to English")
    t.set_defaults(func=_cmd_translate)

    s = sp.add_parser("build-specimen", help="Build HTML specimen for the symbol font")
    s.add_argument("--dict", default="data/dictionary.json", help="Input dictionary JSON")
    s.add_argument("--font", default="dist/LogiGlyph.ttf", help="Font path used in @font-face")
    s.add_argument("--out", default="dist/specimen.html", help="Output HTML path")
    s.set_defaults(func=_cmd_build_specimen)

    return p


def main() -> int:
    parser = build_parser()
    args = parser.parse_args()
    return args.func(args)


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