(function () {
    'use strict';
    angular.module('uniflash').directive('ufConnectionState', connectionState);
    function connectionState() {
        var directive = {
            restrict: 'E',
            controller: ConnectionState,
            controllerAs: 'vm',
            templateUrl: 'ds/connection_state.template.html',
            scope: {},
        };
        return directive;
    }
    var ConnectionState = (function () {
        function ConnectionState($scope, $rootScope, $log, ufPreferencesSrv, ufSession, ufDS, ufDsProgressDialog) {
            var _this = this;
            this.$scope = $scope;
            this.$rootScope = $rootScope;
            this.$log = $log;
            this.ufPreferencesSrv = ufPreferencesSrv;
            this.ufSession = ufSession;
            this.ufDS = ufDS;
            this.ufDsProgressDialog = ufDsProgressDialog;
            this.connectionStatus = {};
            this.cores = this.ufSession.queryConfig().cores;
            //init value
            this.remainConnected = !ufPreferencesSrv.get('Auto Disconnect');
            //update gui value back to service
            $scope.$watch('vm.remainConnected', function (newVal, oldVal) {
                if (newVal !== oldVal) {
                    ufPreferencesSrv.set('Auto Disconnect', !newVal);
                }
            });
            var listeners = {};
            angular.forEach(this.cores, function (core) {
                var coreName = core.name;
                var listener = _this.getTargetStateListener(coreName);
                _this.ufDS.addTargetStateListener((coreName), listener);
                listeners[coreName] = listener;
            });
            $scope.$on('$destroy', function () {
                angular.forEach(listeners, function (listener, coreName) {
                    _this.ufDS.removeTargetStateListener((coreName), listener);
                });
            });
            $scope.$on('$destroy', $rootScope.$on('ds.opStarted', function () {
                _this.dsOpInProgress = true;
            }));
            $scope.$on('$destroy', $rootScope.$on('ds.opFinished', function () {
                _this.dsOpInProgress = false;
            }));
        }
        ConnectionState.prototype.isConfigured = function () {
            return this.ufDS.isConfigured();
        };
        ConnectionState.prototype.isConnected = function (coreName) {
            return (this.ufDS.getCachedConnectedCores()[coreName]) ? true : false;
        };
        ConnectionState.prototype.isAnyCoreConnected = function () {
            return (Object.keys(this.ufDS.getCachedConnectedCores()).length > 0);
        };
        ConnectionState.prototype.getTargetStateListener = function (coreName) {
            var _this = this;
            return function (status) {
                if (_this.isConnected(coreName)) {
                    _this.connectionStatus[coreName] = status.description;
                }
                else {
                    _this.connectionStatus[coreName] = status.description ? status.description : 'Disconnected';
                }
            };
        };
        ConnectionState.prototype.disconnectAllCores = function () {
            this.$log.debug('ConnectionState: disconnectAllCores()');
            this.ufDsProgressDialog.disconnectAllCores();
        };
        ConnectionState.prototype.configure = function () {
            this.ufDsProgressDialog.configureDS();
        };
        ConnectionState.prototype.deconfigure = function () {
            this.ufDsProgressDialog.deconfigureDS();
        };
        ConnectionState.$inject = ['$scope', '$rootScope', '$log', 'ufPreferencesSrv', 'ufSession', 'ufDS', 'ufDsProgressDialog'];
        return ConnectionState;
    }());
})();
