"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var application_manager_base_1 = require("../../application-manager-base");
var Future = require("fibers/future");
var path = require("path");
var temp = require("temp");
var IOSSimulatorApplicationManager = (function (_super) {
    __extends(IOSSimulatorApplicationManager, _super);
    function IOSSimulatorApplicationManager(iosSim, identifier, $options, $fs, $bplistParser, $processService, $logger) {
        _super.call(this, $logger);
        this.iosSim = iosSim;
        this.identifier = identifier;
        this.$options = $options;
        this.$fs = $fs;
        this.$bplistParser = $bplistParser;
        this.$processService = $processService;
    }
    IOSSimulatorApplicationManager.prototype.getInstalledApplications = function () {
        return Future.fromResult(this.iosSim.getInstalledApplications(this.identifier));
    };
    IOSSimulatorApplicationManager.prototype.installApplication = function (packageFilePath) {
        var _this = this;
        return (function () {
            if (_this.$fs.exists(packageFilePath).wait() && path.extname(packageFilePath) === ".zip") {
                temp.track();
                var dir = temp.mkdirSync("simulatorPackage");
                _this.$fs.unzip(packageFilePath, dir).wait();
                var app = _.find(_this.$fs.readDirectory(dir).wait(), function (directory) { return path.extname(directory) === ".app"; });
                if (app) {
                    packageFilePath = path.join(dir, app);
                }
            }
            _this.iosSim.installApplication(_this.identifier, packageFilePath).wait();
        }).future()();
    };
    IOSSimulatorApplicationManager.prototype.uninstallApplication = function (appIdentifier) {
        return this.iosSim.uninstallApplication(this.identifier, appIdentifier);
    };
    IOSSimulatorApplicationManager.prototype.startApplication = function (appIdentifier) {
        var _this = this;
        return (function () {
            var launchResult = _this.iosSim.startApplication(_this.identifier, appIdentifier).wait();
            if (!_this.$options.justlaunch) {
                var childProcess = _this.iosSim.printDeviceLog(_this.identifier, launchResult);
                _this.$processService.attachToProcessExitSignals(_this, childProcess.kill);
            }
        }).future()();
    };
    IOSSimulatorApplicationManager.prototype.stopApplication = function (cfBundleExecutable) {
        return this.iosSim.stopApplication(this.identifier, cfBundleExecutable);
    };
    IOSSimulatorApplicationManager.prototype.canStartApplication = function () {
        return true;
    };
    IOSSimulatorApplicationManager.prototype.getApplicationInfo = function (applicationIdentifier) {
        var _this = this;
        return (function () {
            var result = null, plistContent = _this.getParsedPlistContent(applicationIdentifier).wait();
            if (plistContent) {
                result = {
                    applicationIdentifier: applicationIdentifier,
                    deviceIdentifier: _this.identifier,
                    configuration: plistContent && plistContent.configuration
                };
            }
            return result;
        }).future()();
    };
    IOSSimulatorApplicationManager.prototype.isLiveSyncSupported = function (appIdentifier) {
        var _this = this;
        return (function () {
            var plistContent = _this.getParsedPlistContent(appIdentifier).wait();
            if (plistContent) {
                return !!plistContent && !!plistContent.IceniumLiveSyncEnabled;
            }
            return false;
        }).future()();
    };
    IOSSimulatorApplicationManager.prototype.getParsedPlistContent = function (appIdentifier) {
        var _this = this;
        return (function () {
            if (!_this.isApplicationInstalled(appIdentifier).wait()) {
                return null;
            }
            var applicationPath = _this.iosSim.getApplicationPath(_this.identifier, appIdentifier), pathToInfoPlist = path.join(applicationPath, "Info.plist");
            return _this.$fs.exists(pathToInfoPlist).wait() ? _this.$bplistParser.parseFile(pathToInfoPlist).wait()[0] : null;
        }).future()();
    };
    IOSSimulatorApplicationManager.prototype.getDebuggableApps = function () {
        return Future.fromResult([]);
    };
    IOSSimulatorApplicationManager.prototype.getDebuggableAppViews = function (appIdentifiers) {
        return Future.fromResult(null);
    };
    return IOSSimulatorApplicationManager;
}(application_manager_base_1.ApplicationManagerBase));
exports.IOSSimulatorApplicationManager = IOSSimulatorApplicationManager;
