# coding:utf8

__author__ = '灌篮高手酷派'

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

import distutils.dir_util
import gw_apk_tool
import gw_data_center

def replace_life_method(extract_dir):
    goal_files = glob.glob(os.path.join(extract_dir, 'smali*/com/yingxiong/hero/UnityPlayerActivity.smali'))
    if len(goal_files) == 0:
        return
    goal_file = goal_files[0]
    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 = '.method protected onResume()V'
            str = '.method public onResume()V\n' \
                  'invoke-static {}, Lcom/kf/framework/SDKPluginWrapper;->onResume()V'
            cont = cont.replace(change_str, str)

            change_str_1 = '.method protected onPause()V'
            str_1 = '.method public onPause()V\n' \
                    'invoke-static {}, Lcom/kf/framework/SDKPluginWrapper;->onPause()V'
            cont = cont.replace(change_str_1, str_1)
            logging.info('replace_str: ' + cont)
        with open(goal_file, 'w+') as tf:
            tf.write(cont)


def script(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    logging.info('------------channelSdkInfo:' + str(channelSdkInfo))

    if channelSdkInfo['id'] == '549':
        replace_life_method(decompileDir)


def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    logging.info('------------channelSdkInfo:' + str(channelSdkInfo))

    if channelSdkInfo['id'] == '591' or channelSdkInfo['id'] == '549':
        s1 = os.path.join(decompileDir, "smali_classes2")
        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)

    if channelSdkInfo['id'] == '512' or channelSdkInfo['id'] == '589':
        s1 = os.path.join(decompileDir, "smali_classes2","com")
        if os.path.exists(s1):
            smali_classes_new = create_mutil_smali(decompileDir)
            s3 = os.path.join(decompileDir, smali_classes_new,"com")
            list_package = ["netease","qiniu","sina","squareup","tencent","thinking","unity3d","wechat","yingxiong"]
            if not os.path.exists(s3):
                distutils.dir_util.mkpath(s3)
            move_package(s1, s3, list_package)

    if channelSdkInfo['id'] == '597':
        move(decompileDir)
    else:
        ""


def move(decompileDir):
    s1 = os.path.join(decompileDir, "smali_classes2/com")
    smali_classes_new = gw_apk_tool.create_mutil_smali(decompileDir)
    s3 = os.path.join(decompileDir, smali_classes_new, "com")
    list_package = ["alibaba","aliyun","baidu","bdchero","bloc","crisisfire","example","gme","google","hero","hu","netease","qiniu","sina","squareup"]

    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    gw_apk_tool.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 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)


if __name__ == '__main__':
    replace_life_method("D:\\work\\xmy")
