# coding:utf8

__author__ = 'dong'

import os
import logging
import xml.dom.minidom
import re
import shutil
import glob
import distutils.dir_util
import os

def script_second(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    logging.info('------------channelSdkInfo:' + str(channelSdkInfo))
    if channelSdkInfo['id'] == '597':
        logging.info("remove_navigate")
        remove_navigate(decompileDir)


def script(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    logging.info('------------channelSdkInfo:' + str(channelSdkInfo))
    if channelSdkInfo['id'] == '597':
        logging.info("remove_navigate")
        remove_navigate(decompileDir)


def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    logging.info('------------channelSdkInfo:' + str(channelSdkInfo))
    if channelSdkInfo['id'] == "549" or channelSdkInfo['id'] == "597":
        s1 = os.path.join(decompileDir, "smali_classes11/com/bytedance")
        if os.path.exists(s1):
            smali_classes_new = create_mutil_smali(decompileDir)
            s3 = os.path.join(decompileDir, smali_classes_new)
            list_package = ["module"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        move_11_bytedance(decompileDir, "smali_classes11")
        move_11_bytedance(decompileDir, "smali_classes12")

        s1 = os.path.join(decompileDir, "smali_classes10")
        if os.path.exists(s1):
            smali_classes_new = create_mutil_smali(decompileDir)
            s3 = os.path.join(decompileDir, smali_classes_new)
            list_package = ["com"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes11/com")
        if os.path.exists(s1):
            smali_classes_new = create_mutil_smali(decompileDir)
            s3 = os.path.join(decompileDir, smali_classes_new)
            list_package = ["bytedance"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes11/com/huawei")
        if os.path.exists(s1):
            smali_classes_new = create_mutil_smali(decompileDir)
            s3 = os.path.join(decompileDir, smali_classes_new)
            list_package = ["hms"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes11/com")
        if os.path.exists(s1):
            smali_classes_new = create_mutil_smali(decompileDir)
            s3 = os.path.join(decompileDir, smali_classes_new)
            list_package = ["huawei", "hermes", "google"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes17/com/bytedance/ttgame/module")
        if os.path.exists(s1):
            smali_classes_new = create_mutil_smali(decompileDir)
            s3 = os.path.join(decompileDir, smali_classes_new)
            list_package = ["usercenter", "voice", "vtranslate", "webview"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes40/com/bytedance/ttgame")
        if os.path.exists(s1):
            smali_classes_new = create_mutil_smali(decompileDir)
            s3 = os.path.join(decompileDir, smali_classes_new)
            list_package = ["core"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)
        move_all(decompileDir)

    if channelSdkInfo['id'] =="591":
       move_package_under_all(decompileDir, "smali_classes13","com", "lzy")

def move_all(decompileDir):
    move_package_under_all(decompileDir, "smali_classes12", "com", "bytedance")
    move_package_under_all(decompileDir, "smali_classes12", "com/bytedance", "ttgame")

    move_package_under_all(decompileDir, "smali_classes12", "com/bytedance/ttgame", "module")
    move_package_under_all(decompileDir, "smali_classes12", "com/bytedance/ttgame/module", "share")

    move_package_under_all(decompileDir, "smali_classes14", "com", "ss")
    move_package_under_all(decompileDir, "smali_classes14", "com/ss", "android")
    move_package_under_all(decompileDir, "smali_classes14", "com/ss/android", "bytedcert")

    move_package_under_all(decompileDir, "smali_classes23", "com/huawei", "agconnect")
    move_package_under_all(decompileDir, "smali_classes6", "", "androidx")
    move_package_under_all(decompileDir, "smali_classes106", "com", "a")
    move_package_under_all(decompileDir, "smali_classes117", "com/bytedance", "android")
    move_package_under_all(decompileDir, "smali_classes120", "com/bytedance/apm6", "commonevent")
    move_package_under_all(decompileDir, "smali_classes6", "androidx", "activity")
    move_package_under_all(decompileDir, "smali_classes7", "com/bytedance", "common")
    move_package_under_all(decompileDir, "smali_classes179", "com/bytedance/frameworks", "apm")
    move_package_under_all(decompileDir, "smali_classes210", "com/bytedance/sdk", "account")
    move_package_under_all(decompileDir, "smali_classes210", "com/bytedance/sdk/account", "api")
    move_package_under_all(decompileDir, "smali_classes220", "com/bytedance/ttgame", "account")
    move_package_under_all(decompileDir, "smali_classes8", "com/bytedance/ttgame/module", "abtest")
    move_package_under_all(decompileDir, "smali_classes274", "com/bytedance/ttgame/module/compliance", "api")
    move_package_under_all(decompileDir, "smali_classes97", "com/huawei/hms", "aaid")

    move_package_under_all(decompileDir, "smali_classes105","com", "bytedance")
    move_package_under_all(decompileDir, "smali_classes105","com/bytedance", "ttgame")
    move_package_under_all(decompileDir, "smali_classes146","com/bytedance/frameworks", "apm")
    move_package_under_all(decompileDir, "smali_classes177","com/bytedance/sdk", "account")
    move_package_under_all(decompileDir, "smali_classes177","com/bytedance/sdk/account", "api")
    move_package_under_all(decompileDir, "smali_classes187","com/bytedance/ttgame", "account")
    move_package_under_all(decompileDir, "smali_classes198","com/bytedance/ttgame/module/compliance", "api")
    move_package_under_all(decompileDir, "smali_classes235","com/bytedance/apm6", "commonevent")
    move_package_under_all(decompileDir, "smali_classes96","com/huawei/hms", "aaid")


def move_package(s1, s3, list_package):
    for d in list_package:
        src = os.path.join(s1, d)
        if os.path.exists(src):
            dst = os.path.join(s3, d)
            distutils.dir_util.copy_tree(src, dst)
            distutils.dir_util.remove_tree(src)


# 移动包下面所有包
def move_package_under_all(decompileDir, smali_classes, move_dir, except_dir):
    s1 = os.path.join(decompileDir, smali_classes, move_dir)
    if not os.path.exists(s1):
        return
    for name in os.listdir(s1):
        if name == except_dir:
            continue
        if "." in name:  # 是文件
            continue
        smali_classes_new = create_mutil_smali(decompileDir)
        s3 = os.path.join(decompileDir, smali_classes_new, move_dir)
        list_package = [name]
        if not os.path.exists(s3):
            distutils.dir_util.mkpath(s3)
        move_package(s1, s3, list_package)


def move_11_bytedance(decompileDir, smali_classes):
    s1 = os.path.join(decompileDir, smali_classes, "/com/bytedance")
    if os.path.exists(s1):
        smali_classes_new = create_mutil_smali(decompileDir)
        s3 = os.path.join(decompileDir, smali_classes_new)
        list_package = ["rocketapi", "wrapper_account", "wrapper_alog", "wrapper_appcloud", "wrapper_applog"]
        if not os.path.exists(s3):
            distutils.dir_util.mkpath(s3)
        move_package(s1, s3, list_package)

    s1 = os.path.join(decompileDir, smali_classes, "/com/bytedance")
    if os.path.exists(s1):
        smali_classes_new = create_mutil_smali(decompileDir)
        s3 = os.path.join(decompileDir, smali_classes_new)
        list_package = ["wrapper_download", "wrapper_encrypt", "wrapper_jsbridge", "wrapper_librarian", "wrapper_net"]
        if not os.path.exists(s3):
            distutils.dir_util.mkpath(s3)
        move_package(s1, s3, list_package)

    s1 = os.path.join(decompileDir, smali_classes, "/com/bytedance")
    if os.path.exists(s1):
        smali_classes_new = create_mutil_smali(decompileDir)
        s3 = os.path.join(decompileDir, smali_classes_new)
        list_package = ["wrapper_pangolinsdk", "wrapper_push", "wrapper_sdk_monitor", "wrapper_service_manager",
                        "wrapper_setting_manager"]
        if not os.path.exists(s3):
            distutils.dir_util.mkpath(s3)
        move_package(s1, s3, list_package)

    s1 = os.path.join(decompileDir, smali_classes, "/com/bytedance")
    if os.path.exists(s1):
        smali_classes_new = create_mutil_smali(decompileDir)
        s3 = os.path.join(decompileDir, smali_classes_new)
        list_package = ["wrapper_share", "wrapper_smartrouter", "wrapper_utility"]
        if not os.path.exists(s3):
            distutils.dir_util.mkpath(s3)
        move_package(s1, s3, list_package)


def create_mutil_smali(decompileDir):
    f_idx = 2
    while True:
        # tmp = gw_file_system.get_full_path(os.path.join(decompileDir, 'smali_classes%d' % f_idx))
        tmp = os.path.join(decompileDir, 'smali_classes%d' % f_idx)
        tmp = tmp.replace('\\', '/')
        tmp = re.sub('/+', '/', tmp)
        if os.path.exists(tmp):
            f_idx += 1
        else:
            smali_classes2_dir = tmp
            break
    # endwhile
    os.mkdir(smali_classes2_dir)
    return smali_classes2_dir


def remove_navigate(decompileDir):
    tmp_res = os.path.join(decompileDir, "res", "navigation")
    tmp_public_xml = os.path.join(decompileDir, "res", "values", "public.xml")
    if os.path.exists(tmp_res):
        distutils.dir_util.remove_tree(tmp_res)
    if os.path.exists(tmp_public_xml):
        os.remove(tmp_public_xml)


if __name__ == '__main__':
    # del_qihoo("C:\\Users\\Administrator\\Desktop\\zt")
    # remove_navigate("G:\\autopack2.0_kuaifa\\tool\\workspace\\lrs360\\13651\\extract")
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes12", "com","bytedance")
    #  move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes12", "com/bytedance","ttgame")
    # 这个可能可以不要
    #  move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes12", "com/bytedance/ttgame","module")
    #  move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes12", "com/bytedance/ttgame/module","share")
    # 14 smali 开始
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes14", "com","ss")
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes14", "com/ss","android")
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes14", "com/ss/android","bytedcert")

    # 23
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes23", "com/huawei","agconnect")
    # 6
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes6", "","androidx")
    # 106
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes106", "com","a")
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes117", "com/bytedance","android")
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes120", "com/bytedance/apm6","commonevent")
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes6", "androidx","activity")
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes7", "com/bytedance","common")
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes179", "com/bytedance/frameworks","apm")
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes210", "com/bytedance/sdk","account")
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes210", "com/bytedance/sdk/account","api")
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes220", "com/bytedance/ttgame","account")
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes8", "com/bytedance/ttgame/module","abtest")
    # move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes274", "com/bytedance/ttgame/module/compliance","api")
    #move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes97","com/huawei/hms", "aaid")

    #move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes105","com", "bytedance")
    #move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes105","com/bytedance", "ttgame")
    #move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes146","com/bytedance/frameworks", "apm")
    #move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes177","com/bytedance/sdk", "account")
    #move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes177","com/bytedance/sdk/account", "api")
    #move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes187","com/bytedance/ttgame", "account")
    #move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes198","com/bytedance/ttgame/module/compliance", "api")
    #move_package_under_all("G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract", "smali_classes235","com/bytedance/apm6", "commonevent")

    decompileDir="G:\\autopack2.0_kuaifa\\tool\\workspace\\wlxxkp\\16075\\extract"
    move_package_under_all(decompileDir, "smali_classes96","com/huawei/hms", "aaid")
