# 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

from xml.etree import ElementTree as ET


def script_pre(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    # s3 = decompileDir + "/smali_classes3"
    s1 = os.path.join(decompileDir, "smali", "com")
    s3 = os.path.join(decompileDir, "smali_classes3", "com")
    # distutils.dir_util.copy_tree(src, assets)
    list = ["alibaba", "coolcloud", "huawei", "taobao", "xiaomi"]
    if not os.path.exists(s3):
        distutils.dir_util.mkpath(s3)
    # os.mkdir(s3)
    for d in list:
        src = os.path.join(s1, d)
        if os.path.exists(src):
            dst = os.path.join(s3, d)
            # shutil.copy(src, dst)
            distutils.dir_util.copy_tree(src, dst)
            distutils.dir_util.remove_tree(src)

    arm64 = os.path.join(decompileDir, "lib", "arm64-v8a")
    if os.path.exists(arm64):
        distutils.dir_util.remove_tree(arm64)
    logging.info("-----debug-----")

    if channelSdkInfo['id'] == "345":
        # loading_png9 = os.path.join(SDK, "100qu/ForRes/drawable-hdpi/loading_bg.9.png")
        loading_png9 = os.path.join(decompileDir, "res/drawable-hdpi/loading_bg.png")
        remove_source(loading_png9)

        # sdk_public = os.path.join(SDK, "100qu/ForRes/values/public.xml")
        # modify_xml(sdk_public, "loading_bg")


def remove_source(path_source):
    logging.info(path_source)
    if not os.path.exists(path_source):
        logging.info("path_source_not_exits")
        return
    os.remove(path_source)


def modify_xml(path_xml, name_xml):
    root_node = ET.parse(path_xml)
    root = root_node.getroot()

    name = 'name'
    providers = root.findall("./public/[@name='loading_bg']")
    if providers is not None:
        for provider in providers:
            # 使用try 主要是为了 防止此属性不在时，导致的错误，而程序终止
            try:
                root.remove(provider)
            except:
                ""

    root_node.write(path_xml, 'utf-8')


if __name__ == '__main__':
    # script_pre("", "f:\\ztest", "", "", "")
    modify_xml("G:\\autopack2.0_kuaifa\\tool\\config\\sdk4\\public.xml", "loading_bg")
