import logging

FORMAT = "[%(levelname)s] %(name)s %(filename)s:%(lineno)d: %(message)s"
logging.basicConfig(format=FORMAT)
logging.getLogger().setLevel(logging.DEBUG)

import json
import zipfile as z
import re
from pathlib import Path
import argparse

from androscalpel import Apk, IdType, IdMethodType, ins, DexString, IdMethod, Code, utils  # type: ignore

RE_CHECK_DEXFILE = re.compile(r"classes\d*.dex")


def is_dexfile(filename: str) -> bool:
    return bool(RE_CHECK_DEXFILE.fullmatch(filename))


def load_apk(path_apk: Path) -> Apk:
    print(f"[+] Load bytecode ")
    apk = Apk()
    with z.ZipFile(path_apk) as zipf:
        for file in filter(is_dexfile, zipf.namelist()):
            print(f"[-] {file}")
            with zipf.open(file, "r") as dex_f:
                dex = dex_f.read()
                apk.add_dex_file(dex)
    return apk


if __name__ == "__main__":
    parser = argparse.ArgumentParser("Dump the name of classes defined in an apk")
    parser.add_argument("apk", type=Path)
    args = parser.parse_args()
    apk = load_apk(args.apk)
    cls = list(map(str, apk.classes.keys()))
    cls.sort()
    for cl in cls:
        print(str(cl))
