"""End-of-season screens: champion, season summary, whole-run player statistics (seasonend.s)."""
ORIGIN = 0x2E7000                      # r12: moved from $2E3000 (4 KB full); stub table stays there
STUB = 0x2E3000


def install(data, assemble, patch):
    code = assemble('seasonend', ORIGIN)
    assert len(code) < 0x3000
    patch(ORIGIN, b'\xFF' * len(code), code)
    disp = ORIGIN - STUB - 2                        # bra.w: 4 bytes per entry, like the table it replaces
    assert 0 < disp < 0x8000
    stub = (bytes.fromhex('6000') + disp.to_bytes(2, 'big')) * 3
    patch(STUB, b'\xFF' * len(stub), stub)          # +0 / +4 / +8 entries keep their old addresses
    patch(0x172E4, bytes.fromhex('3f3c007861009e6c'), bytes.fromhex('4ef9002e30004e71'))
    # simulate.s: Start on the result screen jumps to $172FA; go through the summary check first.
    hits = [a for a in range(0x2FD000, 0x2FD400, 2) if data[a:a + 6] == bytes.fromhex('4ef9000172fa')]
    assert len(hits) == 1, hits
    patch(hits[0], bytes.fromhex('4ef9000172fa'), bytes.fromhex('4ef9002e3004'))
