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

const PendulumScene = (function () {
    function getName() {
        return "Pendulum";
    }

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

    function getParameters() {
        return [
            {
                name: "color_hint",
                title: "Define 2 colors for your scene.",
                type: "hint"
            },
            {
                name: "foreground",
                title: "Foreground",
                type: "color",
                default: "#ff0000"
            },
            {
                name: "background",
                title: "Background",
                type: "color",
                default: "#000000"
            },
            {
                name: "speed",
                title: "Speed",
                type: "int_enum",
/*jslint-disable*/
                values: {200: "Very Slow", 10: "Slow", 6: "Medium", 4: "Fast"},
/*jslint-enable*/
                default: 6
            }
        ];
    }

    function generateScene(sceneID, sceneName, numLeds, parameters) {

        const config = getParameters();
        const background = ScenesCommon.colorFromHexString(ScenesCommon.valueOrDefaultFromParameters(parameters, config, 'background'));
        const foreground = ScenesCommon.colorFromHexString(ScenesCommon.valueOrDefaultFromParameters(parameters, config, 'foreground'));
        const speed = ScenesCommon.valueOrDefaultFromParameters(parameters, config, 'speed');
        const gradientLength = Math.floor(parseFloat(numLeds) / 3.0);

        const gradient = ScenesCommon.gradientColors(foreground, background, gradientLength);

        let sceneData = ScenesCommon.createSceneData(sceneID, numLeds, sceneName);

        let pos;
        for (pos = 0; pos < numLeds; pos += 1) {
            addLedsSceneData(sceneData, numLeds, pos, speed, gradient, background, foreground);
        }
        while (pos > 0) {
            addLedsSceneData(sceneData, numLeds, pos, speed, gradient, background, foreground);
            pos -= 1;
        }

        return sceneData;
    }

    function addLedsSceneData(sceneData, numLeds, pos, speed, gradient, background, foreground) {

        // Create scene data set
        let sceneDataSet = ScenesCommon.createSceneDataSet(numLeds, speed);
        let led = 0;
        let color;

        // Set colors
        for (led = 0; led < numLeds; led += 1) {
            if (led === pos) {
                ScenesCommon.pushColor(sceneDataSet.rgbArray, foreground);
            } else if ((led < pos) && (led > (pos - gradient.length))) {
                color = gradient[pos - led];
                ScenesCommon.pushColor(sceneDataSet.rgbArray, color);
            } else if ((led > pos) && (led < (pos + gradient.length))) {
                color = gradient[(led - pos)];
                ScenesCommon.pushColor(sceneDataSet.rgbArray, color);
            } else {
                ScenesCommon.pushColor(sceneDataSet.rgbArray, background);
            }
        }

        // Add set
        sceneData.sceneDatas.push(sceneDataSet);
    }

    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="M10,5.5l0,5c0,0.276 -0.224,0.5 -0.5,0.5l-3,0c-0.276,0 -0.5,-0.224 -0.5,-0.5l0,-5c0,-0.276 0.224,-0.5 0.5,-0.5l3,0c0.276,0 0.5,0.224 0.5,0.5Zm3,0.5l0,4c0,0.276 -0.224,0.5 -0.5,0.5l-1,0c-0.276,0 -0.5,-0.224 -0.5,-0.5l0,-4c0,-0.276 0.224,-0.5 0.5,-0.5l1,0c0.276,0 0.5,0.224 0.5,0.5Zm-8,0l0,4c0,0.276 -0.224,0.5 -0.5,0.5l-1,0c-0.276,0 -0.5,-0.224 -0.5,-0.5l0,-4c0,-0.276 0.224,-0.5 0.5,-0.5l1,0c0.276,0 0.5,0.224 0.5,0.5Zm10.5,0.5l-0,3c-0,0.276 -0.224,0.5 -0.5,0.5l-0.5,-0c-0.276,-0 -0.5,-0.224 -0.5,-0.5l0,-3c0,-0.276 0.224,-0.5 0.5,-0.5l0.5,-0c0.276,-0 0.5,0.224 0.5,0.5Zm-13.5,-0l-0,3c-0,0.276 -0.224,0.5 -0.5,0.5l-0.5,-0c-0.276,-0 -0.5,-0.224 -0.5,-0.5l0,-3c0,-0.276 0.224,-0.5 0.5,-0.5l0.5,-0c0.276,-0 0.5,0.224 0.5,0.5Z"/>
</svg>`;
    }

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


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

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

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

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

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