# coding:utf8

__author__ = '极无双'

import os
import logging
import xml.dom.minidom
import re
import shutil
import glob

ANDROID_NS = 'http://schemas.android.com/apk/res/android'
from xml.etree import ElementTree as ET


def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    # fix_coolyun_application_getinstance_null_bug(decompileDir)
    # replace_application_verify_rose_bug(decompileDir)
    is_version_3_sdk = fix_coolpad_version_3_method_name_change_bug(decompileDir)
    print('is_version_3_sdk: ', is_version_3_sdk)
    # if is_version_3_sdk:
    #     fix_coolpad_version_3_login_fail_bug(decompileDir)
    return


def fix_coolyun_application_getinstance_null_bug(decompile_dir):
    insert_str_list = [
        '\n\t.locals 1\n\n',
        '\tsget-object v0, Lcom/coolyun/framework/CoolYunApplication;->application:Landroid/content/Context;\n\n',
        '\tcheck-cast v0, Lcom/coolyun/framework/CoolYunApplication;\n\n',
        '\treturn-object v0\n\n']
    for dirpath, dirnames, filenames in os.walk(decompile_dir):
        if r'com\coolyun\framework' in dirpath and 'CoolYunApplication.smali' in filenames:
            smali_path = os.path.join(dirpath, 'CoolYunApplication.smali')
            with open(smali_path, 'r') as f:
                smali_str_arr = f.readlines()
                index_str = '.method public static getInstance()Lcom/coolyun/framework/CoolYunApplication;'
                end_method_str = '.end method'
                insert_str = ''.join(insert_str_list)
                on_create_index = 0
                insert_index = 0
                for line in smali_str_arr:
                    if index_str in line:
                        on_create_index = smali_str_arr.index(line)
                        print('on_create_index', on_create_index, line)
                        break
                for index, value in enumerate(smali_str_arr):
                    if index > on_create_index and end_method_str in value:
                        insert_index = index
                        print('insert_index', index)
                        break
                print(on_create_index, insert_index)
                del smali_str_arr[on_create_index + 1:insert_index]
                smali_str_arr.insert(on_create_index + 1, insert_str)
            with open(smali_path, 'w') as f:
                f.write(''.join(smali_str_arr))
                break


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 'com\\wrapper\\proxyapplication' in dirpath and 'WrapperProxyApplication.smali' in filenames:
            application_path = os.path.join(dirpath, "WrapperProxyApplication.smali")
            print(application_path)
            f = open(application_path, "r")
            smali_str_arr = f.readlines()
            f.close()
            for line in smali_str_arr:
                if not is_rose_replace_application(line) and 'Lcom/coolyun/framework/CoolYunApplication' in line:
                    smali_str_arr[smali_str_arr.index(line)] = line.replace('Lcom/coolyun/framework/CoolYunApplication',
                                                                            'Landroid/app/Application')
                if isNeedReplaceApplicationLine(line):
                    smali_str_arr[smali_str_arr.index(line)] = line.replace('Landroid/app/Application',
                                                                            'Lcom/coolyun/framework/CoolYunApplication')
            f = open(application_path, "w")
            f.write(''.join(smali_str_arr))
            f.close()


def fix_coolpad_version_3_method_name_change_bug(decompile_dir):
    # is_version_3_sdk = False
    is_new_sdk_and_version_lt_version_3 = False
    #
    # for dirpath, dirnames, filenames in os.walk(decompile_dir):
    #     if 'com\\yulong\\sdk\\promoter' in dirpath and 'OnGameAuthListener.smali' in filenames:
    #         auth_listener_path = os.path.join(dirpath, 'OnGameAuthListener.smali')
    #         f = open(auth_listener_path, 'r')
    #         smali_str_arr = f.read()
    #         if 'onSuccess' in smali_str_arr:
    #             is_version_3_sdk = True
    #         if 'onResult' in smali_str_arr:
    #             is_new_sdk_and_version_lt_version_3 = True
    #         f.close()
    #         break

    is_version_3_sdk=True

    for dirpath, dirnames, filenames in os.walk(decompile_dir):
        if 'com\\yulong\\sdk\\promoter' in dirpath and 'PromoterGameAuthApi$1.smali' in filenames:
            smali_path = os.path.join(dirpath, 'PromoterGameAuthApi$1.smali')
            print(smali_path)
            f = open(smali_path, 'r')
            smali_str_arr = f.readlines()
            index_str = 'onSuccess'
            if is_new_sdk_and_version_lt_version_3:
                index_str = 'onSuccess'
            elif is_version_3_sdk:
                index_str = 'onResult'
            for line in smali_str_arr:
                if index_str in line:
                    print(line)
                    index = smali_str_arr.index(line)
                    if is_new_sdk_and_version_lt_version_3:
                        smali_str_arr[index] = line.replace(index_str, 'onResult')
                    elif is_version_3_sdk:
                        smali_str_arr[index] = line.replace(index_str, 'onSuccess')
                    print(smali_str_arr[index])
                    break
            f.close()
            f = open(smali_path, 'w')
            f.write(''.join(smali_str_arr))
            f.close()
            break
    return is_version_3_sdk

def fix_coolpad_version_3_login_fail_bug(decompile_dir_path):
    insert_str_list = [
        '\n\t.locals 0\n\n',
        '\treturn-void\n\n']
    for dirpath, dirnames, filenames in os.walk(decompile_dir_path):
        if 'com\\yulong\\account\\auth' in dirpath and 'AuthCodeApiImpl.smali' in filenames:
            smali_path = os.path.join(dirpath, 'AuthCodeApiImpl.smali')
            f = open(smali_path, 'r')
            smali_str_arr = f.readlines()
            index_str = '.method private returnAuthError(Lcom/yulong/account/common/info/ErrorInfo;)V'
            end_method_str = '.end method'
            insert_str = ''.join(insert_str_list)
            on_create_index = 0
            insert_index = 0
            for line in smali_str_arr:
                if index_str in line:
                    on_create_index = smali_str_arr.index(line)
                    print('on_create_index', on_create_index, line)
                    break
            for index, value in enumerate(smali_str_arr):
                if index > on_create_index and end_method_str in value:
                    insert_index = index
                    print('insert_index', index)
                    break
            print(on_create_index, insert_index)
            del smali_str_arr[on_create_index + 1:insert_index]
            smali_str_arr.insert(on_create_index + 1, insert_str)
            f.close()
            f = open(smali_path, 'w')
            f.write(''.join(smali_str_arr))
            f.close()
            break


if __name__ == '__main__':
    ""
