angular.module("app").directive("dashboardHeader", function ($rootScope, $timeout, AppService, BackendService, SQConstants, SQEvents, SettingsService, L) {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: "../app/directives/dashboardHeader/dashboardHeader.html",
        scope: {
            taskmanager: '@', // whether the header is used in task-manager app or not
            instance: '='
        },
        link: function (scope, element, attrs) {
            scope.switchToPanel = function (panel, saveConfig) {
                if(saveConfig && $rootScope.project.state != scope.projectStates.running){
                    AppService.updateTaskXML().then(function () {
                        AppService.switchToPanel(panel);
                    });
                }
                else {
                    AppService.switchToPanel(panel);
                }
            }

            /*
            scope.toggleSettingsView = function () {
                scope.settingsOverviewShown = !scope.settingsOverviewShown;

                //save selected settings view option 
                SettingsService.setShowSettingsOverview(scope.settingsOverviewShown);

                SQEvents.notifyListeners(SQEvents.get("SETTINGS_VIEW_TOGGLE"), {
                    showOverview: scope.settingsOverviewShown
                });
            }
            */
            function getEngineTitle() {
                switch (window.appConfig.product) {
                    case 'RETESTER':
                        return L.tsq('Retester');
                    case 'BUILDER':
                        return L.tsq('Builder');
                    case 'OPTIMIZER':
                        return L.tsq('Optimizer');
                    case 'PORTFOLIOMASTER':
                        return L.tsq('Portfolio Master');
                    default:
                        return getCustomProjectName();
                }
            }

            function getCustomProjectName(){
                return limitString2(AppService.getProject(), 40);
            }

            function onEvent(event, data) {
                if (event == SQEvents.get('SETTINGS_TAB_RELOAD')) {
                    if (data == 'all') {
                        scope.template = AppService.getTaskTemplateName();
                    }
                } 
                else if (event == SQEvents.get('APP_PANEL_CHANGED')) {
                    if (scope.activePanel == 'settings' && data != 'settings') {
                        SQEvents.notifyListeners(SQEvents.get('SETTINGS_CLOSE'));
                    }

                    scope.activePanel = data;

                    if (data == 'results' || data == 'settings') {
                        $timeout(function () {
                            window.dispatchEvent(new Event('resize'));
                        }, 0, false);
                    }
                    if (data == 'tmprojects') {
                        $('.sqn-container').addClass('sqn-hide-header');
                    } else {
                        $('.sqn-container').removeClass('sqn-hide-header');
                    }
                }
                else if(event == SQEvents.get('APP_PROJECT_CHANGED') && scope.taskmanager === 'true'){
                    scope.engineTitle = getCustomProjectName();
                }
                else return;

                try { scope.$digest(); } catch (er) { }
            }

            scope.showSettingsTab = function() {
                return true;
            }

            scope.$on('$destroy', function () {
                SQEvents.removeListener(listenerId);
            });

            scope.projectStates = SQConstants.getConstants().runningStatuses;
            scope.template = AppService.getTaskTemplateName();

            scope.activePanel = 'dashboard';
            scope.engineTitle = getEngineTitle();
            //scope.settingsOverviewShown = SettingsService.shouldShowSettingsOverview();

            scope.isTaskManager = AppService.isTaskManager();

            scope.projectInProgressTitle = L.tsq("Project is in progress");

            var listenerId = "DashboardHeader";
            SQEvents.addListener(listenerId, [
                SQEvents.get('SETTINGS_TAB_RELOAD'),
                SQEvents.get('APP_PANEL_CHANGED'),
                SQEvents.get('APP_PROJECT_CHANGED'),
                SQEvents.get('SETTINGS_CLOSE')
            ], onEvent);
        }
    };
});