"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const runner_1 = require("../base/runner");
const build_1 = require("@weex-cli/build");
const device_1 = require("@weex-cli/device");
const config_1 = require("../common/config");
const const_1 = require("../common/const");
const DEBUG = require("debug");
const debug = DEBUG('run');
class AndroidRunner extends runner_1.default {
    constructor(options) {
        super(options, const_1.PLATFORM_TYPES.android);
    }
    async setNativeConfig() {
        const config = this.config;
        config_1.default[this.type].resolve(Object.assign({
            platform: const_1.PLATFORM_TYPES.android,
        }, config.nativeConfig), config.projectPath);
    }
    async buildNative(options = {}) {
        const config = this.config;
        const androidBuilder = new build_1.AndroidBuilder({
            projectPath: config.projectPath,
        });
        this.transmitEvent(androidBuilder);
        const { appPath } = await androidBuilder.run(Object.assign({
            onOutCallback: outString => {
                debug('BUILD OUTPUT:', outString);
            },
            onErrorCallback: outString => {
                debug('BUILD ERROR:', outString);
            },
        }, options));
        return appPath;
    }
    async installAndLaunchApp(appPath) {
        const config = this.config;
        const wsServer = this.wsServer;
        const serverInfo = wsServer.getServerInfo();
        const androidDevice = new device_1.AndroidDevices();
        this.transmitEvent(androidDevice);
        const ws = this.stringifyConfigs({ Ws: `ws://${serverInfo.hostname}:${serverInfo.port}` });
        await androidDevice.run({
            id: config.deviceId,
            applicationId: config.applicationId,
            appPath,
            androidShellCmdString: `-d ${ws}`,
        });
    }
    stringifyConfigs(configs) {
        let str = "'{";
        for (const key in configs) {
            if (configs.hasOwnProperty(key)) {
                str += '\\"';
                str += key;
                str += '\\":';
                str += '\\"';
                str += configs[key];
                str += '\\",';
            }
        }
        str = str.slice(0, -1);
        str += "}'";
        return str;
    }
}
exports.default = AndroidRunner;
