angular.module("app").directive("benchmarkDialog", function($rootScope, BackendService, SQWebSocketService, SQConstants, SQEvents, L) {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: "../app/directives/benchmarkDialog/benchmarkDialog.html",
        scope: {
            instance: '=',
        },
        link: function (scope, element, attrs) {
            scope.status = "beforeStart"; //"beforeStart","running","showResults"
            scope.progress = 0;
            scope.results = {};
            scope.performance = "NA";
            scope.hideClose = false;
            scope.afterStart = false;

            var performance = SQConstants.getConstants().performance;
            var performanceOptions = performance.options;

            scope.instance.show = function(afterStart) {
                scope.afterStart = afterStart;
                scope.hideClose = afterStart;
                scope.progress = 0;
                scope.status = "beforeStart";
                scope.performance = "N/A";

                if(performance) {
                    switch (performance.coreUsage) {
                        case performanceOptions.singleCore:
                            scope.performance = L.tsq("Single threaded.");
                            break;
                        case performanceOptions.reserve1Core:
                            scope.performance = L.tsq("Reserve 1 core for UI, compute tasks in parallel on all remaining cores.");
                            break;
                        case performanceOptions.customCores:
                            scope.performance = L.tsq("Custom number of cores - %d.", [performance.customCores]);
                            break;
                        case performanceOptions.maxPerformance:
                            scope.performance = L.tsq("Max performance - running multiple tasks in parallel, using all processor cores.");
                            break;
                    }    
                }

                try { scope.$digest(); } catch (err) {}
                
                showPopup('#benchmarkDialog');
            }

            scope.start = function(performance) {
                BackendService.sendRequest('/main/startBenchmark', null, function(result) {
                    scope.status = "running";
                });
            }

            function onWebSocketData(data) {
                if(data.benchmark) {
                    if(data.benchmark.progress) {
                        scope.progress = data.benchmark.progress;
                    } 
                    
                    if(data.benchmark.results) {
                        scope.status = "showResults";

                        SQConstants.getConstants().benchmarkTimePerTick = data.benchmark.results.timePerTick;

                        scope.results = data.benchmark.results;
                        scope.results.avgStrategiesPerHour = formatIntNumber(scope.results.avgStrategiesPerHour);
                        scope.results.totalTicks = formatIntNumber(scope.results.totalTicks);
                        scope.results.strategyAverageTime = scope.results.strategyAverageTime+" s.";
                        scope.results.timePerTick = scope.results.timePerTick+" ms.";
                        scope.results.duration = scope.results.dataLoadTime+" / "+scope.results.testTime;
                        
                        SQEvents.notifyListeners(SQEvents.get('REFRESH_CC_TIMES'));
                        
                        scope.hideClose = false;
                    }
                   
                    try { scope.$digest(); } catch (err) {}
                }
            }

            SQWebSocketService.subscribeGeneral("benchmarkDialog", onWebSocketData);
        }
    }
});