# -*- coding:utf-8 -*-
import os
import logging
def script_last(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    print "script last snow"
    # 修复酷派新sdk 3.0接口方法名改变登录后闪退bug
    is_version_3_sdk = fix_coolpad_version_3_method_name_change_bug(extract_dir)
    print u'is_version_3_sdk: ', is_version_3_sdk
    if is_version_3_sdk:
        fix_coolpad_version_3_login_fail_bug(extract_dir)


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 ur'com\yulong\sdk\promoter' in dirpath and u'OnGameAuthListener.smali' in filenames:
            auth_listener_path = os.path.join(dirpath, u'OnGameAuthListener.smali')
            with open(auth_listener_path, u'r') as f:
                smali_str_arr = f.read()
                if u'onSuccess' in smali_str_arr:
                    is_version_3_sdk = True
                if u'onResult' in smali_str_arr:
                    is_new_sdk_and_version_lt_version_3 = True
                break
    for dirpath, dirnames, filenames in os.walk(decompile_dir):
        if ur'com\yulong\sdk\promoter' in dirpath and u'PromoterGameAuthApi$1.smali' in filenames:
            smali_path = os.path.join(dirpath, u'PromoterGameAuthApi$1.smali')
            print smali_path
            with open(smali_path, u'r') as f:
                smali_str_arr = f.readlines()
                index_str = ur'onSuccess'
                if is_new_sdk_and_version_lt_version_3:
                    index_str = ur'onSuccess'
                elif is_version_3_sdk:
                    index_str = ur'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, u'onResult')
                        elif is_version_3_sdk:
                            smali_str_arr[index] = line.replace(index_str, u'onSuccess')
                        print smali_str_arr[index]
                        break
            with open(smali_path, u'w') as f:
                f.write(u''.join(smali_str_arr))
                break
    return is_version_3_sdk


def fix_coolpad_version_3_login_fail_bug(decompile_dir_path):
    insert_str_list = [
        u'\n\t.locals 0\n\n',
        u'\treturn-void\n\n']
    for dirpath, dirnames, filenames in os.walk(decompile_dir_path):
        if ur'com\yulong\account\auth' in dirpath and u'AuthCodeApiImpl.smali' in filenames:
            smali_path = os.path.join(dirpath, u'AuthCodeApiImpl.smali')
            with open(smali_path, u'r') as f:
                smali_str_arr = f.readlines()
                index_str = u'.method private returnAuthError(Lcom/yulong/account/common/info/ErrorInfo;)V'
                end_method_str = u'.end method'
                insert_str = u''.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 u'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 u'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, u'w') as f:
                f.write(u''.join(smali_str_arr))
                break

