Fingerprint2.excludePixelRatio = false;
Fingerprint2.excludeDoNotTrack = false;

Fingerprint2.get({
    excludes: {
        enumerateDevices: true,
        pixelRatio: false,
        doNotTrack: false,
        fontsFlash: true,
    },
}, function (components) {

    var device = {};
    device.hash = Fingerprint2.x64hash128(components.map(function (pair) {
        return pair.value;
    }).join(), 31);

    var params = {};

    var pVSg = {
        'availableScreenResolution': 'available_resolution',
        'colorDepth': 'color_depth',
        'cpuClass': 'cpu_class',
        // '': 'do_not_track', // ???
        'hardwareConcurrency': 'hardware_concurrency',
        'language': 'language',
        'platform': 'navigator_platform',
        // '': 'pixel_ratio', // ???
        'plugins': 'regular_plugins',
        'screenResolution': 'resolution',
        'timezoneOffset': 'timezone_offset',
        'userAgent': 'user_agent',
    };

    for (var index in components) {
        var obj = components[index];
        var paramItem = obj.key;

        if (pVSg[paramItem]) {
            var paramName = pVSg[paramItem];

            if (paramItem === 'colorDepth' || paramItem === 'hardwareConcurrency') {
                obj.value = obj.value.toString();
            }

            if (paramItem === 'plugins') {
                var newValue = [];
                for (var plugin in obj.value) {
                    if (obj.value[plugin][0]) {
                        newValue.push(obj.value[plugin][0]);
                    }
                }
                obj.value = newValue;
            }

            params[paramName] = Array.isArray(obj.value) ? obj.value : [obj.value];
        }

        params.do_not_track = [];
        params.pixel_ratio = [];
    }

    params.acceptHeader = '*/*';
    params.javaEnabled = true;

    device.params = params;
    if (window._env) {
        window._env.device = JSON.stringify(device);
        window._env.deviceHash = device.hash;
    } else {
        throw new Error('no _env');
    }
});

$.fn.initDevice = function () {
    var $this = $(this);
    if ($this.length !== 1) {
        $this.each(function () {
            $(this).initDevice();
        });

        return this;
    }

    $this.val(window._env.device);
};
