/*jslint single: true, long:true, for:true, unordered:true*/
/*global ScenesCommon*/

const KaleidoscopeScene = (function () {
    const AVERAGE_LENGTH = 20; // leds
    const LENGTH_VARIATION = 15; // +/- len leds
    const NUMBER_OF_SETS = 20;

    function getName() {
        return "Kaleidoscope";
    }

    function getIdentifier() {
        return "com.corsair.cc.sc.kaleidoscope";
    }

    function getParameters() {
        return [
            {
                name: "color_hint",
                title: "This scene uses random colors.",
                type: "hint"
            },
            {
                name: "speed",
                title: "Speed",
                type: "int_enum",
/*jslint-disable*/
                values: {2000: "Very Slow", 500: "Slow", 100: "Medium", 50: "Fast"},
/*jslint-enable*/
                default: 100
            }
        ];
    }

    function generateScene(sceneID, sceneName, numLeds, parameters) {
        const config = getParameters();
        const speed = ScenesCommon.valueOrDefaultFromParameters(parameters, config, 'speed');
        let sceneData = ScenesCommon.createSceneData(sceneID, numLeds, sceneName);
        let i = 0;

        for (i = 0; i < NUMBER_OF_SETS; i += 1) {
            let random = Math.random() - 0.5;
            let duration = parseFloat(speed) + Math.floor(parseFloat(speed) * random);

            let sceneDataSet = ScenesCommon.createSceneDataSet(numLeds, duration);
            addRandomColorsToDataSet(sceneDataSet, numLeds);
            sceneData.sceneDatas.push(sceneDataSet);
        }
        return sceneData;
    }

    function addRandomColorsToDataSet(sceneDataSet, numLeds) {
        let end = 0;
        while (end < numLeds) {
            let len = AVERAGE_LENGTH + Math.round((Math.random() * LENGTH_VARIATION * 2) - LENGTH_VARIATION);
            let color = ScenesCommon.randomColor();
            let i = 0;
            for (i = 0; i < len; i += 1) {
                ScenesCommon.pushColor(sceneDataSet.rgbArray, color);
                end += 1;

                if (end >= numLeds) {
                    break;
                }
            }
        }
    }

    function getIcon() {
        return `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 16 16">
<path id="icon" fill="#d8d8d8" fill-rule="evenodd" d="M6.022,9.095c-0.015,-0.071 -0.022,-0.145 -0.022,-0.22l0,-2.25c0,-0.621 0.504,-1.125 1.125,-1.125l2.25,0c0.621,0 1.125,0.504 1.125,1.125l0,2.25c0,0.621 -0.504,1.125 -1.125,1.125l-2.25,-0c-0.232,-0 -0.448,-0.071 -0.627,-0.191c0.001,0.022 0.002,0.044 0.002,0.066l0,1.75c0,0.483 -0.392,0.875 -0.875,0.875l-1.75,-0c-0.483,-0 -0.875,-0.392 -0.875,-0.875l0,-1.75c0,-0.483 0.392,-0.875 0.875,-0.875l1.75,0c0.143,0 0.277,0.034 0.397,0.095Zm6.478,-6.47l0,1.25c0,0.345 -0.28,0.625 -0.625,0.625l-1.25,-0c-0.345,-0 -0.625,-0.28 -0.625,-0.625l0,-1.25c0,-0.345 0.28,-0.625 0.625,-0.625l1.25,0c0.345,0 0.625,0.28 0.625,0.625Zm1,8.625l0,1.5c0,0.414 -0.336,0.75 -0.75,0.75l-1.5,-0c-0.414,-0 -0.75,-0.336 -0.75,-0.75l0,-1.5c0,-0.414 0.336,-0.75 0.75,-0.75l1.5,0c0.414,0 0.75,0.336 0.75,0.75Zm-1,0.25l-1,-0l0,1l1,-0l0,-1Zm-7,-7.25l0,1.5c0,0.414 -0.336,0.75 -0.75,0.75l-1.5,-0c-0.414,-0 -0.75,-0.336 -0.75,-0.75l0,-1.5c0,-0.414 0.336,-0.75 0.75,-0.75l1.5,0c0.414,0 0.75,0.336 0.75,0.75Zm-1,0.25l-1,-0l0,1l1,-0l0,-1Z"/>
</svg>`;
    }

    function getColors() {
        return ScenesCommon.hexColors(ScenesCommon.RAINBOW_COLORS);
    }

    return {
        name: getName,
        identifier: getIdentifier,
        parameters: getParameters,
        generate_scene: generateScene,
        icon: getIcon,
        colors: getColors
    };
}());


function getName() {
    return KaleidoscopeScene.name();
}

function getIdentifier() {
    return KaleidoscopeScene.identifier();
}

function getParameters() {
    return KaleidoscopeScene.parameters();
}

function generateScene(sceneID, sceneName, numLeds, parameters) {
    return KaleidoscopeScene.generate_scene(sceneID, sceneName, numLeds, parameters);
}

function getIcon() {
    return KaleidoscopeScene.icon();
}

function getColors() {
    return KaleidoscopeScene.colors();
}
