(function() {

    'use strict';

    angular.module('uniflash').directive('ufProgramActions', programActions);

    function programActions() {

        let 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: uniflash.Context) {

        let vm = this;
        let 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) {

            let programName = dsOpParams[0] ? dsOpParams[0].file.name : '';

            let action = {
                displayName: displayName,
                handler: handler,
                toolTip: displayName + ' ' + programName
            };

            return action;

            function handler() {
                let dsCore = ufDsProgressDialog.getDSCore(coreName);
                return dsCore.run(dsOperation, dsOpParams);
            }
        }
    }

} ());