"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 platform_livesync_service_base_1 = require("./platform-livesync-service-base");
var AndroidPlatformLiveSyncService = (function (_super) {
    __extends(AndroidPlatformLiveSyncService, _super);
    function AndroidPlatformLiveSyncService(_liveSyncData, $devicesService, $mobileHelper, $logger, $options, $deviceAppDataFactory, $fs, $injector, $projectFilesManager, $projectFilesProvider, $liveSyncProvider) {
        _super.call(this, _liveSyncData, $devicesService, $mobileHelper, $logger, $options, $deviceAppDataFactory, $fs, $injector, $projectFilesManager, $projectFilesProvider, $liveSyncProvider);
        this.$devicesService = $devicesService;
        this.$mobileHelper = $mobileHelper;
        this.$logger = $logger;
        this.$options = $options;
        this.$deviceAppDataFactory = $deviceAppDataFactory;
        this.$fs = $fs;
        this.$injector = $injector;
        this.$projectFilesManager = $projectFilesManager;
        this.$projectFilesProvider = $projectFilesProvider;
        this.$liveSyncProvider = $liveSyncProvider;
    }
    AndroidPlatformLiveSyncService.prototype.fullSync = function (postAction) {
        var _this = this;
        return (function () {
            var appIdentifier = _this.liveSyncData.appIdentifier;
            var platform = _this.liveSyncData.platform;
            var projectFilesPath = _this.liveSyncData.projectFilesPath;
            var canExecute = _this.getCanExecuteAction(platform, appIdentifier);
            var action = function (device) {
                return (function () {
                    var deviceLiveSyncService = _this.resolveDeviceSpecificLiveSyncService(platform, device);
                    var deviceAppData = _this.$deviceAppDataFactory.create(appIdentifier, _this.$mobileHelper.normalizePlatformName(platform), device);
                    deviceLiveSyncService.beforeLiveSyncAction(deviceAppData).wait();
                    ;
                    var installed = _this.tryInstallApplication(device, deviceAppData).wait();
                    var localToDevicePaths = _this.$projectFilesManager.createLocalToDevicePaths(deviceAppData, projectFilesPath, null, _this.liveSyncData.excludedProjectDirsAndFiles);
                    var afterSyncAction;
                    if (installed) {
                        deviceLiveSyncService.afterInstallApplicationAction(deviceAppData, localToDevicePaths).wait();
                        afterSyncAction = function () { return device.applicationManager.tryStartApplication(deviceAppData.appIdentifier); };
                    }
                    else {
                        _this.transferFiles(deviceAppData, localToDevicePaths, _this.liveSyncData.projectFilesPath, true).wait();
                        afterSyncAction = function () { return _this.refreshApplication(deviceAppData, localToDevicePaths); };
                    }
                    if (postAction) {
                        _this.finishLivesync(deviceAppData).wait();
                        return postAction(deviceAppData, localToDevicePaths).wait();
                    }
                    afterSyncAction().wait();
                    _this.finishLivesync(deviceAppData).wait();
                }).future()();
            };
            _this.$devicesService.execute(action, canExecute).wait();
        }).future()();
    };
    return AndroidPlatformLiveSyncService;
}(platform_livesync_service_base_1.PlatformLiveSyncServiceBase));
$injector.register("androidPlatformLiveSyncServiceLocator", { factory: AndroidPlatformLiveSyncService });
