# coding:utf8

__author__ = 'Snow'

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


def script_init(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    sdk_id = channel_sdk_info['id']
    # gw_data_center.pack_small = False
    logging.info('script_init: ' + str(channel_sdk_info))


def script_first(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    return
    logging.info("-----debug exec first func-----")
    # game_apk = os.path.join(decompileDir, "game.apk")
    game_apk = decompileDir + "/../game.apk"
    if os.path.exists(game_apk):
        # A3AEECD8
        gw_file_system.delete_apk_file_by_aapt(game_apk, "assets/39285EFA.dex")
        gw_file_system.delete_apk_file_by_aapt(game_apk, "assets/A3AEECD8.dex")
    logging.info("-----debug-----")


def script(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    # s3 = decompileDir + "/smali_classes3"
    # A3AEECD8.dex
    return
    dex = decompileDir + "../39285EFA.dex"
    asserts = os.path.join(decompileDir, "assets/39285EFA.dex")
    if os.path.exists(asserts) and os.path.exists(os.path.join(dex)):
        distutils.dir_util.copy_tree(dex, asserts)

    dex = decompileDir + "../A3AEECD8.dex"
    asserts = os.path.join(decompileDir, "assets/A3AEECD8.dex")
    if os.path.exists(asserts) and os.path.exists(os.path.join(dex)):
        distutils.dir_util.copy_tree(dex, asserts)


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

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


def del_qihoo(decompileDir):
    qihoo = os.path.join(decompileDir, 'smali_classes3/com/qihoo')
    if os.path.exists(qihoo):
        shutil.rmtree(qihoo)


def add_openFloatWindow_method(decompileDir):
    r_file_path = 'smali_classes4/com/qihoo/gamecenter/sdk/matrix/Matrix.smali'
    src_file = os.path.join(decompileDir, r_file_path)
    print(src_file)
    if os.path.exists(src_file):
        with open(src_file, 'r+') as f:
            lines = f.readlines()
            method = '.method public static openFloatWindow(Landroid/app/Activity;)V\n'
            if method not in lines:
                method = ['.method public static openFloatWindow(Landroid/app/Activity;)V\n',
                          '    .locals 0\n',
                          '    return-void\n',
                          '.end method']
                for l in method:
                    lines.append(l)
        with open(src_file, 'w+') as fl:
            fl.write(''.join(lines))


if __name__ == '__main__':
    del_qihoo("C:\\Users\\Administrator\\Desktop\\wlwz")
