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

APKS = Path(__file__).resolve().parent.parent / "apks"
for name in ["maribank_sg_base.apk", "maribank_sg_arm64.apk"]:
    p = APKS / name
    if not p.exists():
        print(name, "missing")
        continue
    with zipfile.ZipFile(p) as z:
        dex = [n for n in z.namelist() if n.endswith(".dex")]
        print("\n", name, "dex:", dex)
        if not dex:
            so = [n for n in z.namelist() if n.endswith(".so")][:5]
            print("  native:", so)
            continue
        d = b"".join(z.read(n) for n in dex)
        for needle in [
            b"safemode.b",
            b"safemode/catchs",
            b"safemode/util",
            b"USB_ADB",
            b"RISK_USB",
            b"lib/safemode/",
        ]:
            print(" ", needle.decode(), d.count(needle))
        classes = sorted(set(m.group().decode()[1:-1].replace("/", ".")
                             for m in re.finditer(rb"Lcom/shopee/bke/lib/safemode/[^;]{1,80};", d)))
        for c in classes:
            if not c.endswith(".R") and ".R$" not in c:
                print(" ", c)
