"use strict";

apingApp
    .service('apingTimeHelper', function () {
        this.getTimestampFromDateString = function (_string, _multiplier, _add) {
            if (!_multiplier || isNaN(_multiplier)) {
                _multiplier = 1;
            }

            if (!_add || isNaN(_add)) {
                _add = 0;
            }

            if (_string) {
                var a = _string.split(/[^0-9]/);
                try {
                    return parseInt(Math.round(new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]) / 1000 * _multiplier) + _add);
                } catch (e) {
                }
            }
            return false;
        };
    })
    .service('apingUtilityHelper', ['apingInputObjects', 'apingApiKeys', function (apingInputObjects, apingApiKeys) {
        this.getApiCredentials = function (_platform, _keyName) {

            if(apingApiKeys) {
                if(apingApiKeys[_platform]) {
                    return apingApiKeys[_platform][Math.floor(Math.random()*apingApiKeys[_platform].length)][_keyName];
                }
            }
            return false;
        };


        this.parseJsonFromAttributes = function (_string, _platform, _appSettings) {

            if (!(typeof _string === "string" && _string)) {
                return [];
            }

            var requests = [];

            var tempArray = this.replaceSingleQuotesAndParseJson(_string);

            angular.forEach(tempArray, function (value, key) {

                value.platform = _platform;

                if (_appSettings) {
                    if (typeof value.items == "undefined" && typeof _appSettings.items != "undefined") {
                        value.items = _appSettings.items;
                    }
                    if (typeof value.model == "undefined" && typeof _appSettings.model != "undefined") {
                        value.model = _appSettings.model;
                    }
                }

                var request = apingInputObjects.getNew("request", value);
                requests.push(request);

            });

            return requests;
        };

        this.replaceSingleQuotesAndParseJson = function (_string) {
            return $.parseJSON(_string.replace(/'/g, '"'));
        };

        this.sortArrayByProperty = function (_property) {
            var sortOrder = 1;
            if (_property[0] === "-") {
                sortOrder = -1;
                _property = _property.substr(1);
            }
            return function (a, b) {
                var result = (a[_property] < b[_property]) ? -1 : (a[_property] > b[_property]) ? 1 : 0;
                return result * sortOrder;
            }
        };

        this.shuffleArray = function(_array) {
            for (var i = _array.length - 1; i > 0; i--) {
                var j = Math.floor(Math.random() * (i + 1));
                var temp = _array[i];
                _array[i] = _array[j];
                _array[j] = temp;
            }
            return _array;
        }

    }]);
@@ -62,16 +62,248 @@
 ion () %7B
+%0A%0A        /**%0A         * Parse Timestamp from DateString and do some math%0A         *%0A         * @param _string %7BString%7D%0A         * @param _multiplier %7BInt%7D%0A         * @param _add %7BInt%7D%0A         * @returns timestamp %7BInt%7D%0A         */
 %0A       
@@ -1000,32 +1000,311 @@
  apingApiKeys) %7B
+%0A%0A        /**%0A         * Return random matching API Key from Constant %22apingApiKeys%22. If there is no matching API Key, the function returns 'false'%0A         *%0A         * @param _platform %7BString%7D%0A         * @param _keyName %7BString%7D%0A         * @returns apiKey %7BString%7D%0A         */
 %0A        this.ge
@@ -1626,31 +1626,692 @@
 %7D;%0A%0A
-%0A        this.parseJson
+        /**%0A         * legagy function for this.parseRequestsFromAttributes()%0A         *%0A         * @param _string %7BString%7D%0A         * @param _platform %7BString%7D%0A         * @param _appSettings %7BObject%7D%0A         * @returns %7BArray%7D%0A         */%0A        this.parseJsonFromAttributes = function (_string, _platform, _appSettings) %7B%0A            return this.parseRequestsFromAttributes(_string, _platform, _appSettings);%0A        %7D;%0A%0A        /**%0A         * Parse JSON from Attributes and create requests%0A         *%0A         * @param _string %7BString%7D%0A         * @param _platform %7BString%7D%0A         * @param _appSettings %7BObject%7D%0A         * @returns %7BArray%7D%0A         */%0A        this.parseRequests
 From
@@ -3300,139 +3300,465 @@
     
-this.replaceSingleQuotesAndParseJson = function (_string) %7B%0A            return $.parseJSON(_string.replace(/'/g, '%22'));%0A        %7D;%0A
+/**%0A         * replace single quotes in string and parse JSON%0A         *%0A         * @param _string %7BString%7D%0A         * @returns %7BArray/Object%7D%0A         */%0A        this.replaceSingleQuotesAndParseJson = function (_string) %7B%0A            return $.parseJSON(_string.replace(/'/g, '%22'));%0A        %7D;%0A%0A        /**%0A         * filter function to sort an array by a property%0A         *%0A         * @param _property %7BFunction%7D%0A         * @returns %7BFunction%7D%0A         */
 %0A   
@@ -4181,32 +4181,153 @@
   %7D%0A        %7D;%0A%0A
+        /**%0A         * shuffle array%0A         *%0A         * @param _array %7BArray%7D%0A         * @returns %7BArray%7D%0A         */%0A
         this.shu
@@ -4633,22 +4633,23 @@
 y;%0A        %7D
+;
 %0A%0A    %7D%5D);

create comments for some function as inline documentations (#55)