# coding:utf8

__author__ = '第五人格网易'

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


def script_init(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    # gw_data_center.pack_small = False
    return


def script_pre(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    sdk_id = channelSdkInfo["id"]
    if sdk_id != '512':  # 只有逆向渠道才需求这样
        return

    f_idx = 2
    dst = gw_file_system.get_full_path(os.path.join(decompileDir, 'smali', 'com/example/qidaosdk'))
    while True:
        logging.info("-----f_idx-----"+str(f_idx))
        tmp_smali = gw_file_system.get_full_path(os.path.join(decompileDir, 'smali_classes%d' % f_idx))
        tmp_smali =tmp_smali.replace('\\', '/')
        tmp_smali = re.sub('/+', '/',tmp_smali)
        if not os.path.exists(tmp_smali):
            logging.info("smali_not_exits:"+tmp_smali)
            break
        tmp = gw_file_system.get_full_path(os.path.join(decompileDir, 'smali_classes%d' % f_idx, 'com/example/qidaosdk/QidaoApplication.smali'))
        tmp = tmp.replace('\\', '/')
        tmp = re.sub('/+', '/', tmp)

        tmp_qidaosdk = gw_file_system.get_full_path(os.path.join(decompileDir, 'smali_classes%d' % f_idx, 'com/example/qidaosdk'))
        tmp_qidaosdk  = tmp_qidaosdk.replace('\\', '/')
        tmp_qidaosdk   = re.sub('/+', '/', tmp_qidaosdk)
        if os.path.exists(tmp):
            logging.info('tmp_smali:'+tmp)
            logging.info('dst:'+dst)
            distutils.dir_util.copy_tree(tmp_qidaosdk, dst)
            distutils.dir_util.remove_tree(tmp_qidaosdk)
            break
        else:
            f_idx += 1
    return


def script(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    return
    smali_classes2_dir = ''
    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)

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

    s1 = os.path.join(decompileDir, "smali_classes2", "com")
    smali_dir = create_mutil_smali(decompileDir)
    s3 = os.path.join(smali_dir, "com")
    list_package = ["netease", "nostra13", "sina", "ta", "unionpay", "yy"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    move_package(s1, s3, list_package)

    logging.info("-----debug-----")


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 script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    # logging.info('------------SDK:'+SDK)
    # logging.info('------------decompileDir:'+decompileDir)
    # logging.info('------------channelSdkInfo:'+str(channelSdkInfo))
    # logging.info('------------new_game_channel_info:'+str(new_game_channel_info))
    # logging.info('------------gameInfo:'+str(gameInfo))
    # logging.info("------rm baidu smali------")
    # restdir = decompileDir+"/"+"smali_classes2/com/baidu/gamesdk"
    # if os.path.exists(restdir):
    # shutil.rmtree(restdir)

    # s3 = decompileDir + "/smali_classes3"

    logging.info('------------channelSdkInfo:' + str(channelSdkInfo))
    if channelSdkInfo['id'] == '549' or channelSdkInfo['id'] == '413' or channelSdkInfo['SDKName'] == 'ludashi':
        del_hjr(decompileDir)
        add_do_not_compress(decompileDir)


def add_do_not_compress(extract_dir):
    goal_file = os.path.join(extract_dir, 'apktool.yml')
    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()
            print(cont)
            change_str = '- assets/res/video.mini.npk'
            str = '- assets/res/video.mini.npk\n' \
                  '- assets'
            cont = cont.replace(change_str, str)
        with open(goal_file, 'w+') as tf:
            tf.write(cont)


def del_hjr(decompileDir):
    x86 = os.path.join(decompileDir, 'smali_classes3/com/hjr')
    if os.path.exists(x86):
        shutil.rmtree(x86)


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


if __name__ == '__main__':
    del_hjr("C:\\Users\\Administrator\\Desktop\\identity")
    add_do_not_compress("C:\\Users\\Administrator\\Desktop\\identity")
