# coding:utf8

__author__ = 'dong'

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


def script_second(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    return


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):
    sdk_id = channelSdkInfo['id']
    if sdk_id == "597":
        s1 = os.path.join(decompileDir, "smali", "androidx")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir, "androidx")
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3,["activity","annotation","appcompat","arch","asynclayoutinflater","browser"])

        s1 = os.path.join(decompileDir, "smali", "androidx")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir, "androidx")
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3,["tracing","transition","vectordrawable","versionedparcelable","viewpager","viewpager2","webkit","work"])

        s1 = os.path.join(decompileDir, "smali", "androidx")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir, "androidx")
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3,["cardview","collection","concurrent","constraintlayout","coordinatorlayout","core","cursoradapter","customview","documentfile","drawerlayout","dynamicanimation"])

        s1 = os.path.join(decompileDir, "smali", "androidx")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir, "androidx")
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3,["emoji2","fragment","inspection","interpolator"])

        s1 = os.path.join(decompileDir, "smali", "androidx")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir, "androidx")
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3,["print","recyclerview","resourceinspection","room","savedstate","security","slidingpanelayout","sqlite","startup","swiperefreshlayout"])

        s1 = os.path.join(decompileDir, "smali", "androidx")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir, "androidx")
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3,["media","multidex","navigation"])

        s1 = os.path.join(decompileDir, "smali", "androidx")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir, "androidx")
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3,["legacy","lifecycle","loader","localbroadcastmanager"])
        # androidx 移动完毕

        s1 = os.path.join(decompileDir, "smali_classes2", "com/kingsoft/shiyou/omnisdk")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir, "com/kingsoft/shiyou/omnisdk")
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3,["plugin","project"])

        s1 = os.path.join(decompileDir, "smali_classes2", "com/kingsoft/shiyou/omnisdk")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir, "com/kingsoft/shiyou/omnisdk")
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3,["api","basic","channel","core"])
        # smali_classes2结束

        s1 = os.path.join(decompileDir, "smali_classes3", "com")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir, "com")
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3,["snyu","tencent","unionpay","unity3d","vmadalin","yulong"])
    return

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_11_bytedance(decompileDir):
    s1 = os.path.join(decompileDir, "smali_classes11/com/bytedance")
    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_classes11/com/bytedance")
    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_classes11/com/bytedance")
    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_classes11/com/bytedance")
    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 = 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")
