(function () {
    'use strict';
    angular.module('uniflash').directive('ufProgramActions', programActions);
    function programActions() {
        var directive = {
            templateUrl: 'ds/program_actions.html',
            restrict: 'E',
            controller: ProgramActionsController,
            controllerAs: 'vm',
            scope: {
                program: '='
            },
            bindToController: true
        };
        return directive;
    }
    ProgramActionsController.$inject = ['ufDsProgressDialog', '$scope', '$rootScope', 'ufContext'];
    function ProgramActionsController(ufDsProgressDialog, $scope, $rootScope, ufContext) {
        var vm = this;
        var coreName = ufContext.getCurrentCore().name;
        vm.primaryAction = createAction('Load', 'program', [vm.program]);
        vm.programActions = [];
        vm.programActions.push(createAction('Verify', 'verify', [vm.program]));
        vm.dsOpInProgress = false;
        $scope.$on('$destroy', $rootScope.$on('ds.opStarted', function () {
            vm.dsOpInProgress = true;
        }));
        $scope.$on('$destroy', $rootScope.$on('ds.opFinished', function () {
            vm.dsOpInProgress = false;
        }));
        function createAction(displayName, dsOperation, dsOpParams) {
            var programName = dsOpParams[0] ? dsOpParams[0].file.name : '';
            var action = {
                displayName: displayName,
                handler: handler,
                toolTip: displayName + ' ' + programName
            };
            return action;
            function handler() {
                var dsCore = ufDsProgressDialog.getDSCore(coreName);
                return dsCore.run(dsOperation, dsOpParams);
            }
        }
    }
}());
