# -*- coding:utf-8 -*-
__author__ = 'ALIXie'

import logging
import shutil
import os
import re
import distutils.dir_util
import gw_file_system
import gw_data_center


def script_init(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    gw_data_center.pack_small = False


def script_pre(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    logging.info('method_proceed: ' + 'script_pre')


def script(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
    logging.info('method_proceed: ' + 'script')
    logging.info('channel_sdk_info: ' + str(channel_sdk_info))
    # 遥望
    if channel_sdk_info['id'] == "412":
        add_hjr_checkSelfPermission(extract_dir)

    if channel_sdk_info['id'] == "521" or channel_sdk_info['id'] == "535" or channel_sdk_info['id'] == "558":
        add_checkSelfPermission(extract_dir)


def script_last(workspace_sdk_dir, decompileDir, channel_sdk_info, new_game_channel_info, game_info):
    if channel_sdk_info['id'] == "549":
        s1 = os.path.join(decompileDir, "smali_classes3/com")
        smali_classes_new = create_mutil_smali(decompileDir)
        s3 = os.path.join(decompileDir, smali_classes_new)
        list_package = ["netease"]
        if not os.path.exists(s3):
            distutils.dir_util.mkpath(s3)
        move_package(s1, s3, list_package)
    return


def add_checkSelfPermission(extract_dir):
    goal_file = os.path.join(extract_dir, 'smali/android/support/v4/content/ContextCompat.smali')
    add_method(goal_file)


def add_hjr_checkSelfPermission(extract_dir):
    goal_file = os.path.join(extract_dir, 'smali/android/support/haojieru/v4/content/ContextCompat.smali')
    add_method(goal_file)


def add_method(goal_file):
    if os.path.isfile(goal_file):
        with open(goal_file, 'r+') as f:
            lines = f.readlines()
            method = ['.method public static checkSelfPermission(Landroid/content/Context;Ljava/lang/String;)I\n',
                      '    .locals 2\n',
                      '    .param p0, "context"    # Landroid/content/Context;\n',
                      '         .annotation build Landroid/support/annotation/NonNull;\n',
                      '         .end annotation\n',
                      '    .end param\n',
                      '    .param p1, "permission"    # Ljava/lang/String;\n',
                      '         .annotation build Landroid/support/annotation/NonNull;\n',
                      '         .end annotation\n',
                      '    .end param\n',
                      '    if-eqz p1, :cond_0\n',
                      '    invoke-static {}, Landroid/os/Process;->myPid()I\n',
                      '    move-result v0\n',
                      '    invoke-static {}, Landroid/os/Process;->myUid()I\n',
                      '    move-result v1\n',
                      '    invoke-virtual {p0, p1, v0, v1}, Landroid/content/Context;->checkPermission(Ljava/lang/String;II)I\n',
                      '    move-result v0\n',
                      '    return v0\n',
                      '    :cond_0\n',
                      '    new-instance v0, Ljava/lang/IllegalArgumentException;\n',
                      '    const-string v1, "permission is null"\n',
                      '    invoke-direct {v0, v1}, Ljava/lang/IllegalArgumentException;-><init>(Ljava/lang/String;)V\n',
                      '    throw v0\n',
                      '.end method']
            for i in method:
                lines.append(i)
        with open(goal_file, 'w') as fl:
            fl.write(''.join(lines))


def move_package(s1, s3, list_package):
    for d in list_package:
        src = os.path.join(s1, d)
        if os.path.exists(src):
            dst = os.path.join(s3, d)
            distutils.dir_util.copy_tree(src, dst)
            distutils.dir_util.remove_tree(src)


def create_mutil_smali(decompileDir):
    f_idx = 2
    while True:
        tmp = gw_file_system.get_full_path(os.path.join(decompileDir, 'smali_classes%d' % f_idx))
        tmp = tmp.replace('\\', '/')
        tmp = re.sub('/+', '/', tmp)
        if os.path.exists(tmp):
            f_idx += 1
        else:
            smali_classes2_dir = tmp
            break
    # endwhile
    os.mkdir(smali_classes2_dir)
    return smali_classes2_dir


if __name__ == "__main__":
    add_hjr_checkSelfPermission("C:\\Users\\Administrator\\Desktop\\dtws")
