# coding:utf8
import os
import logging
import xml.etree.ElementTree as ET

ANDROID_NAMESPACE = 'http://schemas.android.com/apk/res/android'
logger = logging.getLogger('script.py')


def script(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    # replace_application_verify_rose_bug(extract_dir)
    fix_install_fail_bug(extract_dir)
    fix_android8_bug_rose(extract_dir)


def fix_android8_bug_rose(decompile_dir):
    style_xml_path = os.path.join(decompile_dir, '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') == 'coolyun_splash_translucent':
            items = style_root.findall('./item')
            for item in items:
                print 'item name', item.get('name')
                print item.findtext('.')
                if item.get('name') == 'android:windowIsTranslucent':
                    item.text = 'false'
            xml_tree.write(style_xml_path, encoding='utf-8', xml_declaration=True)
            break


def fix_install_fail_bug(decompile_dir_path):
    namespace = '{http://schemas.android.com/apk/res/android}'
    ET.register_namespace('android', 'http://schemas.android.com/apk/res/android')
    manifest_path = os.path.join(decompile_dir_path, 'AndroidManifest.xml')
    xml_tree = ET.parse(manifest_path)
    xml_root = xml_tree.getroot()
    application_node = xml_root.find('./application')
    print application_node
    application_name = application_node.get(namespace + 'name')
    print application_name
    # 修改 extractNativeLib
    application_node.set(namespace + 'extractNativeLibs', 'true')
    xml_tree.write(manifest_path, encoding='utf-8', xml_declaration=True)


def isNeedReplaceApplicationLine(line):
    return '.super Landroid/app/Application;' in line \
           or 'invoke-direct {p0}, Landroid/app/Application;-><init>()V' in line \
           or 'invoke-super {p0, p1}, Landroid/app/Application;->attachBaseContext(Landroid/content/Context;)V' in line \
           or 'invoke-super {p0, p1}, Landroid/app/Application;->onConfigurationChanged(Landroid/content/res/Configuration;)V' in line \
           or 'invoke-super {p0}, Landroid/app/Application;->onLowMemory()V' in line \
           or 'invoke-super {p0}, Landroid/app/Application;->onTerminate()V' in line \
           or 'invoke-super {p0, p1}, Landroid/app/Application;->onTrimMemory(I)V' in line \
           or 'invoke-super {p0}, Landroid/app/Application;->onCreate()V' in line


def is_rose_replace_application(line):
    return '.super Lcom/coolyun/framework/CoolYunApplication;' in line \
           or 'invoke-direct {p0}, Lcom/coolyun/framework/CoolYunApplication;-><init>()V' in line \
           or 'invoke-super {p0, p1}, Lcom/coolyun/framework/CoolYunApplication;->attachBaseContext(Landroid/content/Context;)V' in line \
           or 'invoke-super {p0, p1}, Lcom/coolyun/framework/CoolYunApplication;->onConfigurationChanged(Landroid/content/res/Configuration;)V' in line \
           or 'invoke-super {p0}, Lcom/coolyun/framework/CoolYunApplication;->onLowMemory()V' in line \
           or 'invoke-super {p0}, Lcom/coolyun/framework/CoolYunApplication;->onTerminate()V' in line \
           or 'invoke-super {p0, p1}, Lcom/coolyun/framework/CoolYunApplication;->onTrimMemory(I)V' in line \
           or 'invoke-super {p0}, Lcom/coolyun/framework/CoolYunApplication;->onCreate()V' in line


def replace_application_verify_rose_bug(decompile_dir):
    for dirpath, dirnames, filenames in os.walk(decompile_dir):
        if r'com\shenghe\wzcq\coolpad' in dirpath and 'HTApplication.smali' in filenames:
            application_path = os.path.join(dirpath, "HTApplication.smali")
            print application_path
            with open(application_path, "r") as f:
                smali_str_arr = f.readlines()
                for line in smali_str_arr:
                    if not is_rose_replace_application(line) and 'Lcom/coolyun/framework/CoolYunApplication' in line:
                        index = smali_str_arr.index(line)
                        smali_str_arr[index] = line.replace('Lcom/coolyun/framework/CoolYunApplication',
                                                            'Landroid/app/Application')
                    if isNeedReplaceApplicationLine(line):
                        index = smali_str_arr.index(line)
                        smali_str_arr[index] = line.replace('Landroid/app/Application',
                                                            'Lcom/coolyun/framework/CoolYunApplication')
            with open(application_path, "w") as f:
                f.write(''.join(smali_str_arr))


if __name__ == '__main__':
    script('', 'E:/script_test', {}, {}, {})
