# -*- coding: utf-8 -*-
import subprocess
import zipfile
from pathlib import Path

APK = Path(__file__).resolve().parent.parent / "apks" / "seabank_ph_base.apk"
DEXDUMP = Path(r"C:\Users\Administrator\AppData\Local\Android\Sdk\build-tools\37.0.0\dexdump.exe")
targets = [
    "Lcom/shopee/bke/biz/user/viewmodel/RegisterViewModel;",
    "Lcom/shopee/bke/biz/user/ui/PhoneNumActivity;",
]
with zipfile.ZipFile(str(APK)) as zf:
    for name in zf.namelist():
        if not name.endswith(".dex"):
            continue
        data = zf.read(name)
        if b"RegisterViewModel" not in data:
            continue
        tmp = Path(__file__).resolve().parent.parent / "tmp" / "tmp_reg.dex"
        tmp.write_bytes(data)
        out = subprocess.check_output(
            [str(DEXDUMP), "-d", str(tmp)], universal_newlines=True, errors="replace"
        )
        for target in targets:
            if target.replace("L", "").replace(";", "") not in data.decode("latin1", errors="ignore"):
                continue
            print("=" * 60, name, target)
            cap = False
            for line in out.splitlines():
                if f"Class descriptor  : '{target}'" in line:
                    cap = True
                elif cap and line.startswith("  Class descriptor") and target not in line:
                    break
                if cap and ("name          :" in line or "type          :" in line):
                    print(line.strip().encode("ascii", "replace").decode())
