# coding:utf8
import os
import xml.etree.ElementTree as ET
import logging
import re
import codecs
import distutils.dir_util
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
    public_xml = os.path.join(extract_dir, "res/values/public.xml")
    handle_public_xml(public_xml, "smssdk_country_group")
    return


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 == "512":
        s1 = os.path.join(extract_dir, "smali")
        smali_classes2_dir = create_mutil_smali(extract_dir)
        s3 = smali_classes2_dir
        list_package = ["androidx"]
        if not os.path.exists(s3):
            distutils.dir_util.mkpath(s3)
        move_package(s1, s3, list_package)
    if sdk_id == "549":
        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 = ["example", "excelliance", "facebook", "flurry", "getui", "google"]
        if not os.path.exists(s3):
            distutils.dir_util.mkpath(s3)
        move_package(s1, s3, list_package)

        s1 = os.path.join(extract_dir, "smali_classes5/androidx")
        smali_classes2_dir = create_mutil_smali(extract_dir)
        s3 = os.path.join(smali_classes2_dir, "androidx")
        list_package = ["interpolator", "loader", "multidex", "slidingpanelayout", "versionedparcelable", "legacy",
                        "localbroadcastmanager", "print", "swiperefreshlayout", "viewpager", "lifecycle", "media",
                        "savedstate", "vectordrawable"]
        if not os.path.exists(s3):
            distutils.dir_util.mkpath(s3)
        move_package(s1, s3, list_package)
    return


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
    os.mkdir(smali_classes2_dir)
    return smali_classes2_dir


def handle_public_xml(public_xml_path, remove_node):
    if not os.path.exists(public_xml_path):
        logging.info('public_xml is null: ' + public_xml_path)
        return
    new_lines = []
    with open(public_xml_path, 'r+') as f:
        for line in f.readlines():
            # l = line.strip()
            if line.find(remove_node) > -1:
                continue
            new_lines.append(line)
        f.seek(0)
        f.truncate()
        f.writelines(new_lines)
    return


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