# coding:utf8

__author__ = 'tianshuqitan'

import os
import logging
import shutil
from xml.etree import ElementTree as ET

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

def script_second(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    modify_manifest_common(decompileDir, "allowNativeHeapPointerTagging")
    if channelSdkInfo['id']=='587':
        progress_png = os.path.join(decompileDir, "res/drawable/progress.png")
        if os.path.exists(progress_png):
            os.remove(progress_png)
    return


def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    logging.info('------------channelSdkInfo:' + str(channelSdkInfo))
    if channelSdkInfo['id'] == '549':
        del_coolcloud(decompileDir)
    modify_manifest_common(decompileDir, "allowNativeHeapPointerTagging")
    app_theme(decompileDir)
    handle_style(decompileDir)


def del_coolcloud(decompileDir):
    coolcloud = os.path.join(decompileDir, 'smali_classes3/com/coolcloud')
    if os.path.exists(coolcloud):
        shutil.rmtree(coolcloud)


def replace_Theme(extract_dir):
    goal_file = os.path.join(extract_dir, 'res/values/styles.xml')
    print(os.path.isfile(goal_file))
    if os.path.isfile(goal_file):
        logging.info('method_proceed: ' + 'goal_file')
        print(goal_file)
        with open(goal_file, 'r+') as f:
            cont = f.read()
            change_str = '@android:style/Theme.Light'
            str = '@android:style/Theme.Light.NoTitleBar'
            cont = cont.replace(change_str, str)
            logging.info('replace_str: ' + cont)
        with open(goal_file, 'w+') as tf:
            tf.write(cont)


def app_theme(decompile_dir_path):
    style_xml_path = os.path.join(decompile_dir_path, 'res', 'values', 'styles.xml')
    xml_tree = ET.parse(style_xml_path)
    xml_root = xml_tree.getroot()
    style_roots = xml_root.findall('./style')
    for style_root in style_roots:
        if style_root.get('name') == 'AppTheme':
            print style_root.get('parent')
            style_root.set('parent', '@android:style/Theme.NoTitleBar.Fullscreen')
            # 在 Python 2 中，需要手动处理编码
            with open(style_xml_path, 'w') as f:
                f.write('<?xml version="1.0" encoding="utf-8"?>\n')
                f.write(ET.tostring(xml_root, encoding='utf-8'))
            break


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 modify_manifest_common(decompileDir, removeKey):
    ET.register_namespace('android', ANDROID_NS)
    xmlparse = os.path.join(decompileDir, 'AndroidManifest.xml')
    logging.info(xmlparse)
    root_node = ET.parse(xmlparse)
    root = root_node.getroot()
    name = '{' + ANDROID_NS + '}name'
    authorities = '{' + ANDROID_NS + '}' + removeKey
    package_name = root.attrib.get('package')
    if package_name == None:
        return
    providers = root.findall('./application')
    if providers != None:
        for provider in providers:
            try:
                del provider.attrib[authorities]
            except:
                ""

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

def handle_style(decompile_dir_path):
    style_xml_path = os.path.join(decompile_dir_path, 'res', 'values', 'styles.xml')
    xml_tree = ET.parse(style_xml_path)
    xml_root = xml_tree.getroot()
    style_roots = xml_root.findall('./style')
    for style_root in style_roots:
        if style_root.get('name') == 'AppTheme':
            print(style_root.get('parent'))
            style_root.set('parent', '@android:style/Theme.NoTitleBar.Fullscreen')
            xml_tree.write(style_xml_path, encoding='utf-8', xml_declaration=True)
            break

if __name__ == '__main__':
    d="D://work//autopack3//tool//outputGame//admin@163.com//xinxiaaojianghukp//1.0.260//xinxiaaojianghukp_39"
    app_theme(d)
