"""DIMENSION 2 batch 12: the pack-reveal screen controller.

HYPOTHESES
 H1 FUN_18009bc40 is 'act on the revealed card at index N'. Its siblings in the
    same screen class implement send-to-club / send-to-transfer-list / quick
    sell, each building a {id,pile} vector and handing it to model slot 0xc0
    (pile 7=club, 5=trade) or to the discard path.
 H2 A 'store all' bulk variant, if it exists, builds a MULTI-element vector for
    the same slot 0xc0 call, i.e. one request with a list.

CONTROL: FutSquadSaveServerResponse -> 0x180171a60.
"""
import traceback, os, re

OUT = "/tmp/claude-1000/-home-alex-Documents-OpenFUT/8e521ca1-ca3e-4138-bb96-df1744dd1d30/scratchpad/packres"

try:
    print("CONTROL FutSquadSaveServerResponse ->",
          [hex(a) for a, v, e in class_deser("FutSquadSaveServerResponse")])

    print("=" * 78)
    print("SECTION 1 -- neighbours of FUN_18009bc40 in the same screen class")
    blob = []
    for va in (0x18009BC40, 0x18009BEC0, 0x18009AD90, 0x18009B160, 0x18009B800,
               0x18009B900, 0x18009BA00):
        try:
            s = dec(va)
        except Exception as e:
            s = "// err %s" % e
        hdr = "==== %#x %s len=%d ====" % (va, fname(va), len(s))
        print(hdr)
        print(s)
        blob.append(hdr + "\n" + s)

    print("=" * 78)
    print("SECTION 2 -- every function that calls model slot 0xc0 "
          "(the {id,pile} move submitter)")
    hits = []
    for r in xrefs_to(0x18011A830):
        ent = r[3]
        if not ent:
            continue
        hits.append(ent)
    seen = set()
    for ent in sorted(set(hits)):
        s = dec(ent)
        if "+ 0xc0))" in s or "+ 0xc0)\n" in s or "0xc0))(plVar" in s:
            print("---- %#x %s len=%d ----" % (ent, fname(ent), len(s)))
            print(s if len(s) < 7000 else s[:7000] + "\n...TRUNCATED len=%d" % len(s))
            blob.append("==== slot0xc0 %#x ====\n%s" % (ent, s))
            seen.add(ent)
    print("  total slot-0xc0 callers:", len(seen))

    with open(os.path.join(OUT, "d2_reveal_screen.txt"), "w") as f:
        f.write("\n".join(blob))
except Exception:
    traceback.print_exc()
