angular.module("app").directive("applyMassConfig", function($rootScope, BackendService, SQWebSocketService, SQConstants, SQEvents, L, UniqueIdGenerator) {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: "../app/directives/applyMassConfig/applyMassConfig.html",
        scope: {
            instance: '=',
            config: '=',
            source: '@'
        },
        link: function (scope, element, attrs) {
            scope.id = UniqueIdGenerator.generateId();

            scope.getSourceTaskLabel = function(){
                var task = getItem(scope.config.availableTasks, "taskName", scope.config.loadFrom.task);
                return task ? task.taskTitle : '';
            }
        
            scope.openFilePicker = function () {
                $rootScope.showFilePicker(L.tsq('Select file'), "ApplyMassConfig", true, false, scope.config.loadFrom.file, null, null, function (paths) {
                    if (paths) {
                        scope.config.loadFrom.file = paths[0];
                        
                        try { scope.$digest(); } catch(er) {}
                    }
                });
            }
        
            scope.selectAllData = function(){
                scope.config.whatToApply.dataTradingEngine = true;
                scope.config.whatToApply.dataBacktestData = true;
                scope.config.whatToApply.dataTestParameters = true;
                scope.config.whatToApply.dataDataRangeParts = true;
            }
            
            scope.selectAllBlocks = function(){
                scope.config.whatToApply.blocksSignals = true;
                scope.config.whatToApply.blocksIndicators = true;
                scope.config.whatToApply.blocksStopLimitEntry = true;
                scope.config.whatToApply.blocksOrderTypes = true;
                scope.config.whatToApply.blocksExitTypes = true;
            }
        
            scope.selectAllTargetTasks = function() {
                for(var i=0; i<scope.config.availableTasks.length; i++) {
                    scope.config.availableTasks[i].apply = true;
                }
            }

            scope.deselectAllTargetTasks = function() {
                for(var i=0; i<scope.config.availableTasks.length; i++) {
                    scope.config.availableTasks[i].apply = false;
                }
            }
        }
    }
});