# coding:utf8
import os
import logging
import re
import glob
import shutil
import xml.etree.ElementTree as ET
import distutils.dir_util

import gw_file_system

ANDROID_NS = 'http://schemas.android.com/apk/res/android'


def script(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    return


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))
    return


def script_pre(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    return


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

    s1 = os.path.join(decompileDir, "smali/androidx")
    if channelSdkInfo['id']=="597" and os.path.exists(s1):

        s1 = os.path.join(decompileDir, "smali/androidx")
        smali_classes2_dir = create_mutil_smali(decompileDir)
        s3 = os.path.join(smali_classes2_dir, "androidx")
        list_package = ["documentfile","drawerlayout","fragment","interpolator","legacy","lifecycle","loader","localbroadcastmanager","multidex"]
        if not os.path.exists(s3):
            distutils.dir_util.mkpath(s3)
        move_package(s1, s3, list_package)

        smali_classes2_dir = create_mutil_smali(decompileDir)
        s3 = os.path.join(smali_classes2_dir, "androidx")
        list_package = ["preference", "print", "recyclerview", "room", "savedstate", "slidingpanelayout", "sqlite",
                        "swiperefreshlayout", "vectordrawable", "versionedparcelable", "viewpager", "work"]
        if not os.path.exists(s3):
            distutils.dir_util.mkpath(s3)
        move_package(s1, s3, list_package)

        s1 = os.path.join(decompileDir, "smali/androidx")
        smali_classes2_dir = create_mutil_smali(decompileDir)
        s3 = os.path.join(smali_classes2_dir, "androidx")
        list_package = ["activity", "appcompat", "asynclayoutinflater", "constraintlayout", "core", "customview",
                        "drawerlayout", "fragment", "interpolator", "lifecycle", "localbroadcastmanager", "multidex",
                        "annotation", "arch", "collection", "coordinatorlayout", "cursoradapter", "documentfile",
                        "exifinterface", "inspection", "legacy", "loader", " media"]
        if not os.path.exists(s3):
            distutils.dir_util.mkpath(s3)
        move_package(s1, s3, list_package)

    if channelSdkInfo['id']=="549" or channelSdkInfo['id']=="591":
        s1 = os.path.join(decompileDir, "smali_classes4/com")
        smali_classes2_dir = create_mutil_smali(decompileDir)
        s3 = os.path.join(smali_classes2_dir, "com")
        list_package = ["uc","umeng","unity3d","yasirkula","yulong","zy"]
        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


def handle_public_xml_param(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


def handle_public_xml(public_xml_path):
    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("offline_banners") > -1 or line.find("outfit7") > -1 or line.find(
                    "countries") > -1 or line.find(
                "county_codes") > -1 or line.find("felis_error_reporting_files") > -1:
                continue
            new_lines.append(line)
        f.seek(0)
        f.truncate()
        f.writelines(new_lines)
    return


def remove_navigate(decompileDir):
    tmp_res = os.path.join(decompileDir, "res", "navigation")
    tmp_public_xml = os.path.join(decompileDir, "res", "values", "public.xml")
    if os.path.exists(tmp_res):
        distutils.dir_util.remove_tree(tmp_res)
    if os.path.exists(tmp_public_xml):
        os.remove(tmp_public_xml)


if __name__ == '__main__':
    script_last("/tmp/ss", "G:/autopack2.0_kuaifa/tool/workspace/wdajl/15957/extract/", {"id": "309"}, "", "")
    # xml_path = "E:/apk/youhua/jingmenfengyue_216122/res/values/public.xml"
    # handle_public_xml(xml_path)
    # hideSplash("/mnt/share/yanghuang/python_test/cr")
