# 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
import gw_file_system
import os
import shutil

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


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


def script(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    logging.info("-----debug-----")


def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    if channelSdkInfo['id'] == '521':
        # res = os.path.join(decompileDir, "res")
        # copy_ic_launcher(res)
        v26 = os.path.join(decompileDir, "res", "mipmap-anydpi-v26", "ic_launcher.xml")
        if os.path.exists(v26):
            os.remove(v26)
    return


def find_and_replace(directory):
    # 遍历目录及其子目录
    for root, dirs, files in os.walk(directory):
        # 检查当前目录名是否以drawable、mipmap开头或其是drawable-xxx、mipmap-xxx类型的目录
        if any(dir_name.startswith(('drawable', 'mipmap')) or dir_name.startswith(('drawable-', 'mipmap-')) for dir_name
               in dirs):
            # 检查ic_launcher_foreground.png文件是否存在

            # 如果存在，则复制同目录下的ic_launcher.png文件，覆盖ic_launcher_foreground.png文件
            source_file = os.path.join(dirs, 'ic_launcher.png')
            destination_file = os.path.join(dirs, 'ic_launcher_foreground.png')

            # 检查ic_launcher.png文件是否存在
            if os.path.exists(source_file) and os.path.exists(destination_file):
                shutil.copy(source_file, destination_file)
                print("Copied ic_launcher.png to ic_launcher_foreground.png in", root)
            else:
                print("ic_launcher.png not found in", root)

            # 使用示例：将'res'替换为你的目标目录路径


def copy_ic_launcher(directory):
    for root, dirs, files in os.walk(directory):
        for dirname in dirs:
            if dirname.startswith('drawable') or dirname.startswith('mipmap'):
                folder_path = os.path.join(root, dirname)
                foreground_path = os.path.join(folder_path, 'ic_launcher_foreground.png')
                launcher_path = os.path.join(folder_path, 'ic_launcher.png')
                if os.path.isfile(foreground_path):
                    shutil.copy(launcher_path, foreground_path)
                    print("Copied '{}' to '{}'".format(launcher_path, foreground_path))


# 调用函数并传入需要递归遍历的目录路径

if __name__ == '__main__':
    # replace_super_application("D:\work\wzdq")
    # modify_manifest("E:\\apk\\youhua\\youhua.zip.out", "requestLegacyExternalStorage")
    ""
