# 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


def script_pre(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)

    mipmap = os.path.join(decompileDir, "res/mipmap-anydpi-v26")
    values = os.path.join(decompileDir, "res/values-anydpi-v26")
    if os.path.exists(mipmap):
        distutils.dir_util.remove_tree(mipmap)
    if os.path.exists(values):
        distutils.dir_util.remove_tree(values)
    logging.info("-----debug-----")


def script_last(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    sdk_id = channel_sdk_info['id']
    if sdk_id == "549":
        # smali_classes6
        s1 = os.path.join(extract_dir, "smali")
        smali_classes2_dir = create_mutil_smali(extract_dir)
        # s3 = os.path.join(smali_classes2_dir, "com")
        s3 = 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(extract_dir, "smali_classes3/com")
        smali_classes2_dir = create_mutil_smali(extract_dir)
        s3 = os.path.join(smali_classes2_dir, "com")
        list_package = ["huawei", "LWService", "lzy", "netease", "onevcat", "q1", "reversekp", "samsung", "sdk",
                        "tencent", "u8", "unikuwei", "unity3d", "yulong"]
        if not os.path.exists(s3):
            distutils.dir_util.mkpath(s3)
        move_package(s1, s3, list_package)


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 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):
            files = os.listdir(tmp)
            if len(files) == 0:
                smali_classes2_dir = tmp
                break
            f_idx += 1
        else:
            smali_classes2_dir = tmp
            break
    # endwhile
    if not os.path.exists(smali_classes2_dir):
        os.mkdir(smali_classes2_dir)
    return smali_classes2_dir


if __name__ == '__main__':
    script_pre("", "f:\\ztest", "", "", "")
