# coding:utf8

__author__ = 'dong'

import os
import logging
import xml.dom.minidom
import re
import shutil
import glob
from xml.etree.ElementTree import ElementTree, Element
import distutils.dir_util
import gw_file_system
import gw_data_center
import gw_apk_tool


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


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

def script_pre(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    sdk_id = channel_sdk_info['id']
    if sdk_id == "597":
        game_mode_config = os.path.join(extract_dir, "res/xml-v22/game_mode_config.xml")
        os.remove(game_mode_config)

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


def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    sdk_id = channelSdkInfo['id']
    if sdk_id == "597" or sdk_id == "591" :
        s1 = os.path.join(decompileDir, "smali_classes4")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = smali_classes2_dir
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, ["androidx", "android"])

        s1 = os.path.join(decompileDir, "smali_classes4", "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_one(s1, s3)

        s1 = os.path.join(decompileDir, "smali_classes5","com","netease")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"com","netease")
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, ["mpay", "ngpush", "ntunisdk"])

        s1 = os.path.join(decompileDir, "smali_classes8","com","netease")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"com","netease")
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, ["party", "unisdk"])

        s1 = os.path.join(decompileDir, "smali_classes8","com","netease")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"com","netease")
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, ["party", "unisdk"])

        s1 = os.path.join(decompileDir, "smali_classes5","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, ["netease"])

        s1 = os.path.join(decompileDir, "smali_classes6/androidx")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir, "androidx")
            list_package = ["documentfile","drawerlayout","fragment","interpolator","legacy","lifecycle","loader","localbroadcastmanager","multidex"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes7/com")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir, "com")
            list_package = ["lzy","meizu","mp4parser","netease","tencent","xiaomi","yulong"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes8/com/netease/ntunisdk")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir, "com/netease/ntunisdk")
            list_package = ["modules","ngpush","okhttp","permissionkit","piclib","protocollib","receiver","ui"," zxing","ngdetect","ngshare","okio"," picedit"," protocolapp","qrcode","shortcuts","util"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

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

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

        s1 = os.path.join(decompileDir, "smali_classes9","com/netease/ntunisdk")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"com/netease/ntunisdk")
            list_package = ["ui","unifix","util","zxing"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes9","com/netease/ntunisdk")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"com/netease/ntunisdk")
            list_package = ["application","ccmoment","clipboard","cpubound","download","external","httpdns","langutil","lbscommon","base","child","coolpad_sdk","cutout","extend","gamemode","imageutil","lbs"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes9","com/netease/ntunisdk")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"com/netease/ntunisdk")
            list_package = ["modules","ngdetect","ngpush","ngshare","okio"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes9","com/netease/ntunisdk")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"com/netease/ntunisdk")
            list_package = ["permissionkit","piclib","protocolapp","qrcode","sdkpersonalinfolist","picedit","placeholdertest","protocollib","receiver","shortcuts"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes9","com/netease")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"com/netease")
            list_package = ["ntunisdk"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes9","com")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"com")
            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_classes3")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir)
            list_package = ["androidx"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes9","com/netease/ntunisdk")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"com/netease/ntunisdk")
            list_package = ["application","ccmoment","clipboard","cpubound","download","external","httpdns","langutil","lbscommon","base","child","coolpad_sdk","cutout","extend","gamemode","imageutil","lbs"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes6","com/netease/ntunisdk")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"com/netease/ntunisdk")
            list_package = ["modules","ngdetect","ngpush","ngshare","okio"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes6","com/netease/ntunisdk")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"com/netease/ntunisdk")
            list_package = ["permissionkit","piclib","protocolapp","qrcode","sdkpersonalinfolist","picedit","placeholdertest","protocollib","receiver","shortcuts"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes6","com/netease")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"com/netease")
            list_package = ["ntunisdk"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes7","androidx")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"androidx")
            list_package = ["activity","appcompat","asynclayoutinflater","collection","core","customview","drawerlayout","annotation","arch","cardview","coordinatorlayout","cursoradapter","documentfile","interpolator"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali_classes8","com")
        if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"com")
            list_package = ["googlecode","jakewharton","lzy","meizu","mp4parser","netease","tencent","xiaomi","yulong"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)


    if sdk_id == "549" or sdk_id=="591":
        s1 = os.path.join(decompileDir, "smali_classes7", "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, ["netease"])

        # s1 = os.path.join(decompileDir, "smali_classes8", "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, ["netease"])


    if sdk_id=="597":
        ""
    else:
        move_smallsheep(decompileDir)

    return


def move_smallsheep(decompileDir):
    s1 = os.path.join(decompileDir, "smali_classes13/com")
    smali_classes_new = gw_apk_tool.create_mutil_smali(decompileDir)
    s3 = os.path.join(decompileDir, smali_classes_new, "com")
    list_package = ["tencent","xiaomi","yulong"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    gw_apk_tool.move_package(s1, s3, list_package)

    s1 = os.path.join(decompileDir, "smali_classes13/com/netease")
    smali_classes_new = gw_apk_tool.create_mutil_smali(decompileDir)
    s3 = os.path.join(decompileDir, smali_classes_new, "com/netease")
    list_package = ["androidcrashhandler","blqx","cloud","gl","inner","mpay","ngpush","ntunisdk","aoe","cc","download","godlikeshare","mc","ngdetect","ntsharesdk","pharos"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    gw_apk_tool.move_package(s1, s3, list_package)

    s1 = os.path.join(decompileDir, "smali_classes7","androidx")
    if os.path.exists(s1):
            smali_classes2_dir = create_mutil_smali(decompileDir)
            s3 = os.path.join(smali_classes2_dir,"androidx")
            list_package = ["legacy","lifecycle","loader","localbroadcastmanager","print","savedstate","slidingpanelayout"]
            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 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 is_reverse_qihoo(file):
    with open(file, 'r+') as f:
        file_str = f.read()
        if 'KFSDK' in file_str:
            return True
        else:
            return False


def replace_init_code(decompileDir):
    smali_files = glob.glob(os.path.join(decompileDir, 'smali*/com/qihoo/'))
    if len(smali_files) >= 1:
        iter_smali = iter(smali_files)
        while True:
            try:
                qihoo_file_path = iter_smali.next()
                file = os.path.join(qihoo_file_path, 'gamecenter/sdk/matrix/Matrix.smali')
                if os.path.exists(file):
                    logging.info('Matrix exits in %s' % qihoo_file_path)
                    if is_reverse_qihoo(file):
                        with open(file, 'r+') as f:
                            cont = f.read()
                            change_str = '0x82c'
                            str = '0x82b'
                            cont = cont.replace(change_str, str)
                            logging.info('replace_str: ' + cont)
                        with open(file, 'w+') as tf:
                            tf.write(cont)

            except StopIteration:
                break


def replace_application(extract_dir):
    goal_file = os.path.join(extract_dir, 'smali_classes3/com/lilithgame/sgame/wxapi/QihooApplication.smali')
    print(os.path.isfile(goal_file))
    if os.path.isfile(goal_file):
        logging.info('method_proceed: ' + 'goal_file')
        print(goal_file)
        with open(goal_file, 'r+') as f:
            cont = f.read()
            change_str = 'Landroid/support/multidex/MultiDexApplication'
            str = 'Lcom/kf/framework/KFApplication'
            cont = cont.replace(change_str, str)
            logging.info('replace_str: ' + cont)
        with open(goal_file, 'w+') as tf:
            tf.write(cont)


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_one(src, dst):
    if os.path.exists(src):
        distutils.dir_util.copy_tree(src, dst)
        distutils.dir_util.remove_tree(src)


if __name__ == '__main__':
    replace_init_code("D:\work\lyjymb")
