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

            scope.instance = {};

            scope.config = ApplyMassConfigDialogService.config;

            scope.instance.show = function(afterStart) {
                ApplyMassConfigDialogService.loadSettings(function(response) {
                    if(scope.source=='file') {
                        scope.config.loadFrom.type = 'file';
                    } else {
                        scope.config.loadFrom.type = 'task';
                        scope.config.loadFrom.task = scope.source;                
                    }
            
                    try { scope.$digest(); } catch (err) {}
                    
                    showPopup('#applyMassConfigDialog-'+scope.id);
                });
            }

            scope.onApplyMassConfig = function() {
                ApplyMassConfigDialogService.applyMassConfig(function(response) {
                    $rootScope.showSuccess(L.tsq("Project config updated."));
                    hidePopup('#applyMassConfigDialog-'+scope.id);
                });
            }
            
        }
    }
});