(function () {
    'use strict';
    var ProgressService = (function () {
        function ProgressService($uibModal, ufSession) {
            this.$uibModal = $uibModal;
            this.ufSession = ufSession;
        }
        ProgressService.prototype.createProgress = function (title) {
            var progress = new Progress(this.$uibModal, title, this.ufSession);
            return this.$uibModal.open({
                templateUrl: 'ds/progress.html',
                controllerAs: 'vm',
                controller: ['$scope', progress.createModalControllerInstance],
                backdrop: 'static'
            }).opened
                .then(function () {
                return progress;
            });
        };
        ProgressService.$inject = ['$uibModal', 'ufSession'];
        return ProgressService;
    }());
    var Progress = (function () {
        function Progress($uibModal, title, ufSession) {
            var _this = this;
            this.$uibModal = $uibModal;
            this.title = title;
            this.ufSession = ufSession;
            this.dialogNames = [];
            this.createModalControllerInstance = function ($scope) {
                _this.modalController = new ModalController($scope, _this.title, _this.ufSession);
                return _this.modalController;
            };
        }
        Progress.prototype.handleEvent = function (event, cancel) {
            this.modalController.update(event, cancel);
            if (event.isFirstUpdate) {
                this.dialogNames.push(event.name);
                this.modalController.updateDescription(event.name);
            }
            else if (event.isComplete) {
                this.dialogNames.pop();
                this.modalController.updateDescription(this.dialogNames[this.dialogNames.length - 1]);
            }
        };
        Progress.prototype.close = function () {
            this.modalController.closeModal();
        };
        return Progress;
    }());
    var ModalController = (function () {
        function ModalController($scope, title, ufSession) {
            this.$scope = $scope;
            this.title = title;
            this.ufSession = ufSession;
            this.event = {};
            this.description = '';
        }
        ModalController.prototype.updateDescription = function (value) {
            this.description = value;
        };
        ModalController.prototype.cancelClicked = function () {
            if (this.cancel) {
                this.cancel().finally(this.$scope.$dismiss);
            }
        };
        ModalController.prototype.update = function (event, newCancelHandler) {
            this.cancel = newCancelHandler;
            // update the even object
            this.event = event;
            // this is may be an external event, so we need this to have the changes take effect
            if (!this.$scope.$$phase) {
                this.$scope.$apply();
            }
        };
        ModalController.prototype.closeModal = function () {
            this.$scope.$dismiss();
        };
        ModalController.prototype.isPercentType = function () {
            return this.event.percent || this.event.percent === 0;
        };
        ModalController.prototype.isSerial = function () {
            return this.ufSession.isSerial();
        };
        return ModalController;
    }());
    angular.module('uniflash').service('ufProgess', ProgressService);
})();
