/**
 * ©2015 Web Performance, Inc
 */
var module = angular.module('metrics.module', ['events.module']);

module.service('initialMetricsService', ['$http', function ($http)
    {
    var service = {};

    service.getInitialSamples = function(number, listener)
        {
        $http.get('/controller/metrics/set/2/recent/' + number).then(function(response)
            {
            if (typeof response === 'undefined')
                {
                console.log("no initial samples are available");
                listener([]);
                }
            else
                listener(response.data);
            }, function(response)
            {
            console.log("Failed to get initial samples. Response code is " + response.status);
            listener([]);
            });
        };

    return service;
    }]);


module.service('fakeMetricsService', ['eventSubscriptionService', function(EventService)
    {
    var last_time = 0;
    var last_sample = [0,0,0,50,,0,,,,,,,,,5];
    var VU_ramp_counter = 1;

    var service = {};
    service.createASample = function()
        {
        var sample = [];
        sample[METRIC_IDS.ELAPSED] = last_time;
        last_time = last_time + 2000;

        sample[METRIC_IDS.HITS_PER_SEC] = (((Math.random() * 2) - 1) * 10) + last_sample[METRIC_IDS.HITS_PER_SEC];
        if (sample[METRIC_IDS.HITS_PER_SEC] < 0)
            sample[METRIC_IDS.HITS_PER_SEC] = 0;
        sample[METRIC_IDS.HITS] = last_sample[METRIC_IDS.HITS] + Math.round(sample[METRIC_IDS.HITS_PER_SEC] / 2);

        sample[METRIC_IDS.PAGES_PER_SEC] = ((Math.random() * 2) - 1) + last_sample[METRIC_IDS.PAGES_PER_SEC];
        if (sample[METRIC_IDS.PAGES_PER_SEC] < 0)
            sample[METRIC_IDS.PAGES_PER_SEC] = 0;

        var errors = Math.random() * 10;
        if (errors > 9)
            sample[METRIC_IDS.PAGES_FAIL] = 2;
        else if (errors > 8)
            sample[METRIC_IDS.PAGES_FAIL] = 1;
        else
            sample[METRIC_IDS.PAGES_FAIL] = 0;

        if (VU_ramp_counter == 5)
            {
            sample[METRIC_IDS.VUS] = last_sample[METRIC_IDS.VUS] + 5;
            VU_ramp_counter = 0;
            }
        else
            sample[METRIC_IDS.VUS] = last_sample[METRIC_IDS.VUS];
        VU_ramp_counter++;

        var avg_duration = Math.random() * 10;
        if (avg_duration > 3)
            sample[METRIC_IDS.PAGES_DUR_AVG] = avg_duration;

        last_sample = sample;

        return sample;
        };

    service.injectMetrics = function()
        {
        var sample = service.createASample();
        var data = {};
        data.metrics = sample;
        var message = {};
        message.data = JSON.stringify(data);
        EventService.injectSummaryMetrics(message);
        };

    return service;
    }]);

module.controller("FakeMetricsController", ["$scope", 'fakeMetricsService', function ($scope, fakeMetricsService)
    {
    $scope.injectMetrics = function()
        {
        fakeMetricsService.injectMetrics();
        };
    }]);

const METRIC_IDS =
    {
    "ELAPSED": 0,
    "HITS": 1,
    "VUS": 2,
    "HITS_PER_SEC": 3,
    "PAGES_COMPLETE": 15,
    "PAGES_FAIL": 6,
    "PAGES_DUR_AVG": 11,
    "PAGES_PER_SEC": 14,
    "WAITING_USERS": 17,
    "WAIT_TIME_AVG": 18,
    "PAGES_FAIL_RATE": 19,
    "BANDWIDTH_OUT": 8,
    "BANDWIDTH_IN": 9
    };

const METRIC_NAMES =
    {
    "ELAPSED": 0,
    "HITS": "Total Hits",
    "VUS": 'Virtual Users',
    "HITS_PER_SEC": 'Hits/sec',
    "PAGES_COMPLETE": 'Pages Completed',
    "PAGES_FAIL": 'Pages failed',
    "PAGES_DUR_AVG": "Avg Page Duration",
    "PAGES_PER_SEC": 'Pages/sec',
    "WAITING_USERS": "Waiting Users",
    "WAIT_TIME_AVG": "Avg Wait Time",
    "PAGES_FAIL_RATE": "Page Failure Rate",
    "BANDWIDTH_OUT": "Bandwidth (out)",
    "BANDWIDTH_IN": "Bandwidth (in)"
    };