# coding:utf8
import os
import xml.etree.ElementTree as ET
import logging
import re
import codecs
import gw_file_system

ANDROID_NAMESPACE = 'http://schemas.android.com/apk/res/android'
logger = logging.getLogger('script.py')


def script_second(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    logging.info('script_second: ' + str(channel_sdk_info))
    # 逆向渠道id

    if channel_sdk_info['SDKName'] == 'yltokf':
        xy = "com/yl"
        dir_xy = os.path.join(extract_dir, "smali", xy)
        if os.path.exists(dir_xy):
            return
        handle_source_in_mutil_smali(extract_dir, xy)

    if channel_sdk_info['SDKName'] == 'xytokf':
        xy = "com/xy"
        dir_xy = os.path.join(extract_dir, "smali", xy)
        if os.path.exists(dir_xy):
            return
        handle_source_in_mutil_smali(extract_dir, xy)
    if channel_sdk_info['SDKName'] == 'smallsheep':
        xy = "com/kf"
        dir_xy = os.path.join(extract_dir, "smali", xy)
        if os.path.exists(dir_xy):
            return
        handle_source_in_mutil_smali(extract_dir, xy)

    return


def handle_source_in_mutil_smali(decompileDir, source_target):
    f_idx = 2
    fist_smali_xy_dir = os.path.join(decompileDir, "smali", source_target)
    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):
            target_dir = os.path.join(decompileDir, tmp, source_target)
            logging.info(target_dir)
            if os.path.exists(target_dir):
                gw_file_system.copy_files(target_dir, fist_smali_xy_dir)
                gw_file_system.delete_file_folder(target_dir)
            f_idx += 1
        else:
            break
    # endwhile


if __name__ == '__main__':
    ""
    # script_last('', 'E:/script_test', {}, {}, {})
