ctx.emit_progress(0, "Инициализация...")

if sys.platform.startswith("win"):
    data_ext = "data.win"
    json_ext = "data.win.json"
    res_ext = ".win"
    ch1_folder = "chapter1_windows"
    ch3_folder = "chapter3_windows"
    ch4_folder = "chapter4_windows"
    ch5_folder = "chapter5_windows"
else:
    data_ext = "game.ios"
    json_ext = "data.mac.json"
    res_ext = ".ios"
    ch1_folder = "chapter1_mac"
    ch3_folder = "chapter3_mac"
    ch4_folder = "chapter4_mac"
    ch5_folder = "chapter5_mac"

ctx.emit_progress(5, "Патчим выбор главы...")
original_data_sel = os.path.join(ctx.selected_folder, data_ext)
bak_data_sel = os.path.join(ctx.selected_folder, f"game.orig.{res_ext.strip('.')}" if sys.platform == "darwin" else f"data.orig.{res_ext.strip('.')}")
result_data_sel = os.path.join(ctx.patch_folder, f"data_sel{res_ext}")

ctx.applypatch(
    original_path=original_data_sel,
    patch_file=os.path.join(temp_dir, "patch", "ch_sel", json_ext),
    result_path=result_data_sel
)

ctx.emit_progress(15, "Патчим первую главу...")
orig_ch1 = os.path.join(ctx.selected_folder, ch1_folder, data_ext)
bak_ch1 = os.path.join(ctx.selected_folder, ch1_folder, f"data.orig.{res_ext.strip('.')}" if sys.platform.startswith("win") else f"game.orig.{res_ext.strip('.')}")
res_ch1 = os.path.join(ctx.patch_folder, f"data_1{res_ext}")
ctx.applypatch(
    original_path=orig_ch1,
    patch_file=os.path.join(temp_dir, "patch", "ch1", json_ext),
    result_path=res_ch1
)

ctx.emit_progress(35, "Патчим третью главу...")
orig_ch3 = os.path.join(ctx.selected_folder, ch3_folder, data_ext)
bak_ch3 = os.path.join(ctx.selected_folder, ch3_folder, f"data.orig.{res_ext.strip('.')}" if sys.platform.startswith("win") else f"game.orig.{res_ext.strip('.')}")
res_ch3 = os.path.join(ctx.patch_folder, f"data_3{res_ext}")
ctx.applypatch(
    original_path=orig_ch3,
    patch_file=os.path.join(temp_dir, "patch", "ch3", json_ext),
    result_path=res_ch3
)

ctx.emit_progress(55, "Патчим четвёртую главу...")
orig_ch4 = os.path.join(ctx.selected_folder, ch4_folder, data_ext)
bak_ch4 = os.path.join(ctx.selected_folder, ch4_folder, f"data.orig.{res_ext.strip('.')}" if sys.platform.startswith("win") else f"game.orig.{res_ext.strip('.')}")
res_ch4 = os.path.join(ctx.patch_folder, f"data_4{res_ext}")
ctx.applypatch(
    original_path=orig_ch4,
    patch_file=os.path.join(temp_dir, "patch", "ch4", json_ext),
    result_path=res_ch4
)

ctx.emit_progress(75, "Патчим пятую главу...")
orig_ch5 = os.path.join(ctx.selected_folder, ch5_folder, data_ext)
bak_ch5 = os.path.join(ctx.selected_folder, ch5_folder, f"data.orig.{res_ext.strip('.')}" if sys.platform.startswith("win") else f"game.orig.{res_ext.strip('.')}")
res_ch5 = os.path.join(ctx.patch_folder, f"data_5{res_ext}")
ctx.applypatch(
    original_path=orig_ch5,
    patch_file=os.path.join(temp_dir, "patch", "ch5", json_ext),
    result_path=res_ch5
)

ctx.emit_progress(90, "Копируем файлы...")
copy_config = { "folders": {}, "files": {} }

copy_config["folders"][os.path.join(temp_dir, "patch", "copy", "chapter1")] = os.path.join(ctx.selected_folder, ch1_folder)
copy_config["folders"][os.path.join(temp_dir, "patch", "copy", "chapter3")] = os.path.join(ctx.selected_folder, ch3_folder)
copy_config["folders"][os.path.join(temp_dir, "patch", "copy", "chapter4")] = os.path.join(ctx.selected_folder, ch4_folder)
copy_config["folders"][os.path.join(temp_dir, "patch", "copy", "chapter5")] = os.path.join(ctx.selected_folder, ch5_folder)

for orig, bak in [(orig_ch1, bak_ch1), (orig_ch3, bak_ch3), (orig_ch4, bak_ch4), (orig_ch5, bak_ch5), (original_data_sel, bak_data_sel)]:
    if not os.path.exists(bak):
        copy_config["files"][orig] = bak

copy_config["files"][res_ch1] = orig_ch1
copy_config["files"][res_ch3] = orig_ch3
copy_config["files"][res_ch4] = orig_ch4
copy_config["files"][res_ch5] = orig_ch5
copy_config["files"][result_data_sel] = original_data_sel

ru_data_src = os.path.join(temp_dir, "patch", "copy", "ru_data.json")
ru_data_dest = os.path.join(ctx.selected_folder, "ru_data.json")
if os.path.exists(ru_data_src):
    copy_config["files"][ru_data_src] = ru_data_dest

bat_file = ctx.copy_files(copy_config)

ctx.emit_progress(95, "Очистка временных файлов...")
ctx.cleanup_files([
    result_data_sel, 
    res_ch1, 
    res_ch3, 
    res_ch4, 
    res_ch5, 
    bat_file
])

ctx.emit_progress(100, "Патчинг завершён!")