"""DIMENSION 2 batch 4: the UI handlers behind quick sell / move / duplicate.

HYPOTHESES
 H1 FUN_18002a040 CardsDiscardCard issues one DiscardCard request for one id;
    FUN_18002a0f0 CardsDiscardCardList issues ONE request with a list (Q4).
 H2 FUN_180039fb0 GetCardDuplicate reads the card field that createPack's
    duplicateItemIdList post-pass wrote (obj+0x10), proving what the list drives.
 H3 CardsSellCard FUN_18002aed0 is list-on-market (ISStart), and
    "send to transfer list" from the reveal is a MoveCard pile change, not a
    dedicated endpoint.
 H4 The coin credit after a quick sell comes from totalCredits in the response
    (obj+0x28) rather than being summed client-side from discardValue.
    RefreshUserCredit FUN_18002b870 and the response's virtual at vtable+0x20
    (0x180122420) are where to look.

CONTROL: FutSquadListServerResponse -> 0x180172140.
"""
import traceback, os

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


def dump(name, text):
    with open(os.path.join(OUT, name), "w") as f:
        f.write(text)


def show(label, va, save=None):
    s = dec(va)
    print("-" * 74)
    print("%s  %#x  len(src)=%d" % (label, va, len(s)))
    print(s)
    if save:
        dump(save, s)
    return s


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

    targets = [
        ("CardsDiscardCard", 0x18002A040),
        ("CardsDiscardCardByRes", 0x18002A0C0),
        ("CardsDiscardCardList", 0x18002A0F0),
        ("CardsMoveCard", 0x18002AB40),
        ("CardsMoveCardByRes", 0x18002ABE0),
        ("CardsMoveMultipleCards", 0x18002AC30),
        ("CardsSellCard", 0x18002AED0),
        ("CardsSwapCards", 0x18002B070),
        ("RemoveFromTradePile", 0x18002B900),
        ("RemoveAllSoldFromTradePile", 0x18002B8E0),
        ("RefreshUserCredit", 0x18002B870),
        ("GetCardDuplicate", 0x180039FB0),
        ("GetTradePileResults", 0x18003B100),
        ("AddCardBackToTradePile", 0x180039C70),
        ("CardsGetLastMoveCardId", 0x18002A200),
        ("respvt+0x20 FUN_180122420", 0x180122420),
        ("respvt+0x40 FUN_180126f00", 0x180126F00),
    ]
    all_src = []
    for lbl, va in targets:
        s = show(lbl, va)
        all_src.append("==== %s %#x ====\n%s\n" % (lbl, va, s))
    dump("d2_ui_handlers.txt", "\n".join(all_src))

except Exception:
    traceback.print_exc()
