/*global CountlyHelpers, countlyDeviceDetails, countlyAppVersion, countlyCommon, _, jQuery*/
(function() {

    window.countlyDeviceDetails = window.countlyDeviceDetails || {};
    CountlyHelpers.createMetricModel(window.countlyDeviceDetails, {name: "device_details", estOverrideMetric: "platforms"}, jQuery);

    countlyDeviceDetails.os_mapping = {
        "webos": {short: "webos", name: "Webos"},
        "brew": {short: "brew", name: "Brew"},
        "unknown": {short: "unk", name: "Unknown"},
        "undefined": {short: "unk", name: "Unknown"},
        "tvos": {short: "atv", name: "Apple TV"},
        "apple tv": {short: "atv", name: "Apple TV"},
        "watchos": {short: "wos", name: "Apple Watch"},
        "unity editor": {short: "uty", name: "Unknown"},
        "qnx": {short: "qnx", name: "QNX"},
        "os/2": {short: "os2", name: "OS/2"},
        "amazon fire tv": {short: "aft", name: "Amazon Fire TV"},
        "amazon": {short: "amz", name: "Amazon"},
        "web": {short: "web", name: "Web"},
        "windows": {short: "mw", name: "Windows"},
        "open bsd": {short: "ob", name: "Open BSD"},
        "searchbot": {short: "sb", name: "SearchBot"},
        "sun os": {short: "so", name: "Sun OS"},
        "solaris": {short: "so", name: "Sun OS"},
        "beos": {short: "bo", name: "BeOS"},
        "mac osx": {short: "o", name: "Mac"},
        "macos": {short: "o", name: "Mac"},
        "mac": {short: "o", name: "Mac"},
        "osx": {short: "o", name: "Mac"},
        "linux": {short: "l", name: "Linux"},
        "unix": {short: "u", name: "UNIX"},
        "ios": {short: "i", name: "iOS"},
        "android": {short: "a", name: "Android"},
        "blackberry": {short: "b", name: "BlackBerry"},
        "windows phone": {short: "w", name: "Windows Phone"},
        "wp": {short: "w", name: "Windows Phone"},
        "roku": {short: "r", name: "Roku"},
        "symbian": {short: "s", name: "Symbian"},
        "chrome": {short: "c", name: "Chrome OS"},
        "debian": {short: "d", name: "Debian"},
        "nokia": {short: "n", name: "Nokia"},
        "firefox": {short: "f", name: "Firefox OS"},
        "tizen": {short: "t", name: "Tizen"}
    };

    countlyDeviceDetails.getCleanVersion = function(version) {
        for (var i in countlyDeviceDetails.os_mapping) {
            version = version.replace(new RegExp("^" + countlyDeviceDetails.os_mapping[i].short, "g"), "");
        }
        return version;
    };

    countlyDeviceDetails.callback = function(isRefresh, data) {
        if (isRefresh) {
            countlyAppVersion.refresh(data);
        }
        else {
            countlyAppVersion.initialize();
        }
    };

    countlyDeviceDetails.getPlatforms = function() {
        return countlyDeviceDetails.getMeta("os");
    };

    countlyDeviceDetails.checkOS = function(os, data, osName) {
        return new RegExp("^" + osName + "([0-9]+|unknown)").test(data);
    };

    countlyDeviceDetails.getPlatformData = function() {

        var chartData = countlyCommon.extractTwoLevelData(countlyDeviceDetails.getDb(), countlyDeviceDetails.getMeta("os"), countlyDeviceDetails.clearObject, [
            {
                name: "os_",
                func: function(rangeArr) {
                    if (countlyDeviceDetails.os_mapping[rangeArr.toLowerCase()]) {
                        return countlyDeviceDetails.os_mapping[rangeArr.toLowerCase()].name;
                    }
                    return rangeArr;
                }
            },
            {
                name: "origos_",
                func: function(rangeArr) {
                    return rangeArr;
                }
            },
            { "name": "t" },
            { "name": "u" },
            { "name": "n" }
        ], "platforms");
        chartData.chartData = countlyCommon.mergeMetricsByName(chartData.chartData, "os_");
        var platformNames = _.pluck(chartData.chartData, 'os_'),
            platformTotal = _.pluck(chartData.chartData, 'u'),
            chartData2 = [];

        /*var sum = _.reduce(platformTotal, function(memo, num) {
            return memo + num;
        }, 0);*/

        for (var i = 0; i < platformNames.length; i++) {
            chartData2[i] = {
                data: [
                    [0, platformTotal[i]]
                ],
                label: platformNames[i]
            };
        }

        chartData.chartDP = {};
        chartData.chartDP.dp = chartData2;

        return chartData;
    };

    countlyDeviceDetails.getResolutionData = function() {
        var chartData = countlyCommon.extractTwoLevelData(countlyDeviceDetails.getDb(), countlyDeviceDetails.getMeta("resolutions"), countlyDeviceDetails.clearObject, [
            {
                name: "resolution",
                func: function(rangeArr) {
                    return rangeArr;
                }
            },
            {
                name: "width",
                func: function(rangeArr) {
                    return "<a>" + rangeArr.split("x")[0] + "</a>";
                }
            },
            {
                name: "height",
                func: function(rangeArr) {
                    return "<a>" + rangeArr.split("x")[1] + "</a>";
                }
            },
            { "name": "t" },
            { "name": "u" },
            { "name": "n" }
        ], "resolutions");

        var resolutions = _.pluck(chartData.chartData, 'resolution'),
            resolutionTotal = _.pluck(chartData.chartData, 'u'),
            resolutionNew = _.pluck(chartData.chartData, 'n'),
            chartData2 = [],
            chartData3 = [];

        /*var sum = _.reduce(resolutionTotal, function(memo, num) {
            return memo + num;
        }, 0);*/
        var i = 0;
        for (i = 0; i < resolutions.length; i++) {
            //var percent = (resolutionTotal[i] / sum) * 100;
            chartData2[i] = {
                data: [
                    [0, resolutionTotal[i]]
                ],
                label: resolutions[i]
            };
        }

        /*var sum2 = _.reduce(resolutionNew, function(memo, num) {
            return memo + num;
        }, 0);*/

        for (i = 0; i < resolutions.length; i++) {
            //var percent = (resolutionNew[i] / sum) * 100;
            chartData3[i] = {
                data: [
                    [0, resolutionNew[i]]
                ],
                label: resolutions[i]
            };
        }

        chartData.chartDPTotal = {};
        chartData.chartDPTotal.dp = chartData2;

        chartData.chartDPNew = {};
        chartData.chartDPNew.dp = chartData3;

        return chartData;
    };

    countlyDeviceDetails.__getBars = countlyDeviceDetails.getBars;
    countlyDeviceDetails.getBars = function(metric) {
        var data = countlyDeviceDetails.__getBars(metric);

        if (metric === "os_versions") {
            for (var i = 0; i < data.length; i++) {
                data[i].name = countlyDeviceDetails.fixOSVersion(data[i].name);
            }
        }

        return data;
    };

    countlyDeviceDetails.fixOSVersion = function(osName) {
        osName = (osName + "").replace(/:/g, ".");

        for (var i in countlyDeviceDetails.os_mapping) {
            osName = osName.replace(new RegExp("^" + countlyDeviceDetails.os_mapping[i].short, "g"), countlyDeviceDetails.os_mapping[i].name + " ");
        }
        return osName;
    };

    countlyDeviceDetails.getOSVersionList = function(name) {
        var codes = {};
        var lowerCase = name.toLowerCase();
        for (var i in countlyDeviceDetails.os_mapping) {
            if (countlyDeviceDetails.os_mapping[i].name.toLowerCase().startsWith(lowerCase)) {
                codes[countlyDeviceDetails.os_mapping[i].short] = true;
            }
            else {
                var changed = lowerCase.replace(new RegExp("^" + countlyDeviceDetails.os_mapping[i].name.toLowerCase(), "g"), countlyDeviceDetails.os_mapping[i].short);
                changed = changed.replace(/ /g, ""); //removes spaces
                changed = (changed + "").replace(/\./g, ":");
                codes[changed] = true;
            }

        }
        return Object.keys(codes);
    };

}());