"""
import_fonts.py — Importa as fontes Aurora Arcana para o cliente ZMMO.

Cria (UFontFace, loading policy = Inline; .ttf embutido no .uasset):
  Content/ZMMO/UI/Fonts/Cinzel/   FF_Cinzel_Regular | _SemiBold | _Bold | _ExtraBold
  Content/ZMMO/UI/Fonts/Rajdhani/ FF_Rajdhani_Regular | _Medium | _SemiBold | _Bold

Source dos .ttf: Tools/Templates/Zeus_Widget/shared/assets/fonts/ (source-of-truth,
não duplicar — UFontFace importa como Inline e embute o .ttf no .uasset).

Nota: Typeface/TypefaceEntry/FontData NÃO são expostos ao Python (UE 5.7), então
a Composite Font (Font_Cinzel/Font_Rajdhani) não é criada por script. Como o
FUIStyleText usa um peso fixo por papel, o FSlateFontInfo referencia o FF_* direto.

Idempotente. Como rodar:
  - Editor: console Python -> exec(open(r"f:/.../Scripts/import_fonts.py").read())
  - Ou via MCP nwiro execute_python.
"""

import os
import unreal

SRC_DIR = r"f:/ZeusProject/Tools/Templates/Zeus_Widget/shared/assets/fonts"
DEST_ROOT = "/Game/ZMMO/UI/Fonts"

FAMILIES = {
    "Cinzel": [
        ("Regular",   "Cinzel-Regular.ttf"),
        ("SemiBold",  "Cinzel-SemiBold.ttf"),
        ("Bold",      "Cinzel-Bold.ttf"),
        ("ExtraBold", "Cinzel-ExtraBold.ttf"),
    ],
    "Rajdhani": [
        ("Regular",  "Rajdhani-Regular.ttf"),
        ("Medium",   "Rajdhani-Medium.ttf"),
        ("SemiBold", "Rajdhani-SemiBold.ttf"),
        ("Bold",     "Rajdhani-Bold.ttf"),
    ],
}

asset_tools = unreal.AssetToolsHelpers.get_asset_tools()


def import_font_face(ttf_path, dest_pkg_path, asset_name):
    task = unreal.AssetImportTask()
    task.filename = ttf_path
    task.destination_path = dest_pkg_path
    task.destination_name = asset_name
    task.replace_existing = True
    task.automated = True
    task.save = True
    task.factory = unreal.FontFileImportFactory()

    asset_tools.import_asset_tasks([task])

    full = "{}/{}".format(dest_pkg_path, asset_name)
    face = unreal.load_asset(full)
    if face is None:
        unreal.log_error("[import_fonts] FALHOU import: {}".format(full))
        return False

    try:
        face.set_editor_property("loading_policy", unreal.FontLoadingPolicy.INLINE)
        unreal.EditorAssetLibrary.save_asset(full)
    except Exception as e:
        unreal.log_warning("[import_fonts] loading_policy INLINE falhou em {}: {}".format(full, e))

    unreal.log_warning("[import_fonts] OK {} (class={})".format(full, type(face).__name__))
    return True


def main():
    unreal.log_warning("[import_fonts] === inicio ===")
    ok = 0
    total = 0
    for family, weights in FAMILIES.items():
        dest_pkg = "{}/{}".format(DEST_ROOT, family)
        for weight_name, ttf in weights:
            total += 1
            ttf_path = os.path.join(SRC_DIR, ttf).replace("\\", "/")
            if not os.path.isfile(ttf_path):
                unreal.log_error("[import_fonts] .ttf nao encontrado: {}".format(ttf_path))
                continue
            asset_name = "FF_{}_{}".format(family, weight_name)
            if import_font_face(ttf_path, dest_pkg, asset_name):
                ok += 1
    unreal.log_warning("[import_fonts] === fim: {}/{} faces ===".format(ok, total))


main()
