#!/usr/bin/env python3
"""Poll the nearest public-predecessor exit candidate with branch-state watches."""
from __future__ import annotations

from argparse import Namespace

import probe_runtime_input_path as runtime_input
from probe_runtime_input_path import DEFAULT_PREFIX, OUT
from probe_runtime_selected_pointer_multislot_savedata_poll import build_summary
from probe_runtime_selected_pointer_poll import write_outputs


OUTPUT_PREFIX = "runtime_selected_pointer_predecessor_nearest_exit_branch_state_poll"
SOURCE_SAVE = "data/public_savedata/flack3r/savedat2.dat"

# Public selector 1:0 save parses as map2_02d candidate tile 5,15.
# The closest geometry-only map2_02d -> map1_01a hint is the left edge
# sample 1,14, which the broad left-run sweep missed by one tile vertically.
TARGETED_EXIT_CANDIDATES = [
    {
        "sourceMap": "map2_02d",
        "targetMap": "map1_01a",
        "startTile": {"x": 5, "y": 15},
        "candidateTile": {"x": 1, "y": 14},
        "candidateSide": "left",
        "targetSpawn": {"x": 34, "y": 19, "side": "right"},
        "reason": "nearest reciprocal geometry hint from the public predecessor save tile",
    },
]

BRANCH_STATE_WATCH_VALUES = {
    "activeSelectionFlag": (0x00457744, 1),
    **{
        f"secondaryBranchState{index}": (0x0059E360 + index, 1)
        for index in range(12)
    },
}


def main() -> None:
    runtime_input.ROUTE_WATCH_VALUES = dict(runtime_input.ROUTE_WATCH_VALUES)
    runtime_input.ROUTE_WATCH_VALUES.update(BRANCH_STATE_WATCH_VALUES)
    args = Namespace(
        startup_wait=18.0,
        hold=0.7,
        gap=0.25,
        interval=0.02,
        prelude="input-path",
        sequence=[
            "predecessor-nearest-left-up-return=Down,Return,Left,Left,Left,Left,Up,Return",
            "predecessor-nearest-up-left-return=Down,Return,Up,Left,Left,Left,Left,Return",
            "predecessor-nearest-left-up-overrun=Down,Return,Left,Left,Left,Left,Up,Left,Return",
        ],
        slot_source=[f"1={SOURCE_SAVE}"],
        case_aliases=True,
        staged_kind="public predecessor nearest-exit branch-state watch",
        prefix=DEFAULT_PREFIX,
        out_dir=OUT,
        output_prefix=OUTPUT_PREFIX,
    )
    summary = build_summary(args)
    summary["objective"] = "public predecessor nearest-exit poll with branch-state watch values"
    summary["targetedExitCandidates"] = TARGETED_EXIT_CANDIDATES
    summary["branchStateWatchValues"] = BRANCH_STATE_WATCH_VALUES
    write_outputs(summary, OUT, OUTPUT_PREFIX)
    print(f"wrote predecessor nearest-exit branch-state poll -> {OUT / (OUTPUT_PREFIX + '.html')}")


if __name__ == "__main__":
    main()
