import package_utils
import file_utils
import xml_utils
import config_utils
import os
import os.path
import json


def packTest():
    '''
    测试代码
    '''
    game = 'qyj2'
    sdk = 'jm'
    subChannel = 'jm'

    channelPath = file_utils.getChannelPath('', game, sdk)
    print('channel_path :%s', channelPath)
    configPath = os.path.join(channelPath, 'config.json')
    jsonText = file_utils.read_file(configPath)
    config = json.loads(jsonText)
    config_utils.replaceArgs(config)

    config = getItemConfig(config, subChannel)
    if config is None:
        return 1

    package_utils.decomplie(game, sdk, subChannel, config)
    package_utils.remove_no_support_attr(game, sdk, subChannel, config)
    package_utils.merge_drawable_res(game, sdk, subChannel, config)
    package_utils.remove_same_values_res(game, sdk, subChannel, config)
    package_utils.copy_res(game, sdk, subChannel, config)
    package_utils.merge_manifest_res(game, sdk, subChannel, config)
    package_utils.change_placeholders(game, sdk, subChannel, config)
    package_utils.add_meta_data(game, sdk, subChannel, config)
    package_utils.copy_app_res(game, sdk, subChannel, config)
    package_utils.change_package_name(game, sdk, subChannel, config)
    package_utils.change_app_name(game, sdk, subChannel, config)
    package_utils.change_app_icon(game, sdk, subChannel, config)
    package_utils.add_launcher(game, sdk, subChannel, config)
    package_utils.pack_jar(game, sdk, subChannel, config)
    package_utils.do_sdk_post_script(game, sdk, config)
    package_utils.do_game_post_script(game, sdk, config)
    package_utils.generateNewRFile(game, sdk, subChannel, config)
    package_utils.split_dex(game, sdk, subChannel, config)
    package_utils.recomplie(game, sdk, subChannel, config)
    package_utils.alignApk(game, sdk, subChannel, config)
    package_utils.apk_signer_apk(game, sdk, subChannel, config)
    package_utils.clearTemp(game, sdk, subChannel, config)


def getCmd(cmd):
    p = os.popen(cmd)
    content = p.read()
    # print(content)
    return content


def getItemConfig(config, subChannel):
    if type(config) == dict:
        if subChannel is None or config['subChannel'] == subChannel:
            return config
    elif type(config) == list:
        for itemConfig in config:
            if subChannel is None or itemConfig['subChannel'] == subChannel:
                return subChannel
    return None


packTest()