# 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):
    logging.info("in script")
    if channelSdkInfo['id'] == "549":
        logging.info("in 549")
        # 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")
        png_list = [
            "res/drawable-mdpi/abc_list_pressed_holo_dark.png",
            "res/drawable-hdpi/abc_list_pressed_holo_dark.png",
            "res/drawable-xhdpi/abc_list_pressed_holo_dark.png",
            "res/drawable-xxhdpi/abc_list_pressed_holo_dark.png",
            "res/drawable-mdpi/abc_list_selector_disabled_holo_dark.png",
            "res/drawable-hdpi/abc_list_selector_disabled_holo_dark.png",
            "res/drawable-xhdpi/abc_list_selector_disabled_holo_dark.png",
            "res/drawable-xxhdpi/abc_list_selector_disabled_holo_dark.png",
        ]
        for p in png_list:
            logging.info("in png_list")
            loading_png9 = os.path.join(decompileDir, p)
            remove_source(loading_png9)


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['id'] == '597':
        public_xml = os.path.join(extract_dir, "res/values/public.xml")
        handle_public_xml(public_xml, "attr-private")

        public_xml = os.path.join(extract_dir, "res/layout/design_bottom_sheet_dialog.xml")
        handle_public_xml(public_xml, "attr-private")

        public_xml = os.path.join(extract_dir, "res/layout/mtrl_layout_snackbar.xml")
        # handle_public_xml(public_xml, "attr-private")
        if os.path.exists(public_xml):
            os.remove(public_xml)

        public_xml = os.path.join(extract_dir, "res/layout-sw600dp/mtrl_layout_snackbar.xml")
        # handle_public_xml(public_xml, "attr-private")
        if os.path.exists(public_xml):
            os.remove(public_xml)

        public_xml = os.path.join(extract_dir, "res/values/public.xml")
        handle_public_xml(public_xml, "mtrl_layout_snackbar")

        public_xml = os.path.join(extract_dir, "res/layout/mtrl_layout_snackbar_include.xml")
        handle_public_xml(public_xml, "attr-private")

        public_xml = os.path.join(extract_dir, "res/drawable-v21/abc_dialog_material_background.xml")
        handle_public_xml(public_xml, "attr-private")

        public_xml = os.path.join(extract_dir, "res/values/styles.xml")
        handle_public_xml(public_xml, "attr-private")

        public_xml = os.path.join(extract_dir, "res/values-v21/styles.xml")
        handle_public_xml(public_xml, "attr-private")
    return


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

    # if channelSdkInfo['id'] == "597":
    remove_false_string(decompileDir + "/res/values/ids.xml")


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


def remove_source(path_source):
    logging.info(path_source)
    if not os.path.exists(path_source):
        logging.info("path_source_not_exits")
        return
    logging.info("in remove")
    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')


def remove_false_string(file_path):
    # 打开文本文件并读取内容
    with open(file_path, 'r') as file:
        lines = file.readlines()

    # 删除每一行中的指定字符串
    filtered_lines = [line.replace('false', '') for line in lines]

    # 打开文本文件以写入模式，将修改后的内容写回原文件
    with open(file_path, 'w') as file:
        file.writelines(filtered_lines)


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