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

const GlitchScene = (function () {
    const MIN_RADIUS = 8;
    const MAX_RADIUS = 24;

    const EXPLOSIONS_COUNT = 12;
    const SPEED_VARIATION = 10; // +/- ms

    function getName() {
        return "Glitch";
    }

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

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

    function generateScene(sceneID, sceneName, numLeds, parameters) {
        const config = getParameters();
        const background = ScenesCommon.colorFromHexString(ScenesCommon.valueOrDefaultFromParameters(parameters, config, 'background'));
        const speed = ScenesCommon.valueOrDefaultFromParameters(parameters, config, 'speed');
        const explosions = [];
        let i = 0;

        for (i = 0; i < EXPLOSIONS_COUNT; i += 1) {
            explosions.push({
                i: 0,
                speed: speed - Math.round((Math.random() * SPEED_VARIATION * 2) - SPEED_VARIATION),
                color: ScenesCommon.randomColor(),
                location: Math.round(Math.random() * numLeds),
                size: Math.round(Math.random() * (MAX_RADIUS - MIN_RADIUS)) + MIN_RADIUS
            });
        }

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

        explosions.forEach(function (explosion) {
            while (explosion.i <= explosion.size) {
                let sceneDataSet = ScenesCommon.createSceneDataSet(numLeds, explosion.speed);

                // Set colors
                for (i = 0; i < numLeds; i += 1) {
                    if ((i === explosion.location - explosion.i) || (i === explosion.location + explosion.i)) {
                        ScenesCommon.pushColor(sceneDataSet.rgbArray, explosion.color);
                    } else {
                        ScenesCommon.pushColor(sceneDataSet.rgbArray, background);
                    }
                }

                // Add set
                sceneData.sceneDatas.push(sceneDataSet);
                explosion.i += 1;
            }
        });

        return sceneData;
    }

    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="M8,6c1.104,-0 2,0.896 2,2c0,1.104 -0.896,2 -2,2c-1.104,-0 -2,-0.896 -2,-2c0,-1.104 0.896,-2 2,-2Zm-4.825,5.565l-1.47,0.512c-0.139,-0.214 -0.267,-0.435 -0.384,-0.663l1.178,-1.017c0.181,0.416 0.409,0.808 0.676,1.168Zm-0.663,-5.992l-0.775,-0.447l0.642,-1.113l0.777,0.448c-0.253,0.345 -0.469,0.717 -0.644,1.112Zm4.813,-3.535l0.292,-1.528c0.127,-0.007 0.254,-0.01 0.383,-0.01c0.129,-0 0.256,0.003 0.383,0.01l0.292,1.528c-0.222,-0.025 -0.447,-0.038 -0.675,-0.038c-0.228,-0 -0.453,0.013 -0.675,0.038Zm5.519,2.423l0.777,-0.448l0.642,1.113l-0.775,0.447c-0.175,-0.395 -0.391,-0.767 -0.644,-1.112Zm0.657,5.936l1.178,1.017c-0.117,0.228 -0.245,0.449 -0.384,0.663l-1.47,-0.512c0.267,-0.36 0.495,-0.752 0.676,-1.168Zm-4.858,3.569l-0,0.895l-1.286,0l0,-0.895c0.212,0.022 0.426,0.034 0.643,0.034c0.217,-0 0.431,-0.012 0.643,-0.034Zm-4.935,-4.612l0.847,-0.732c0.147,0.817 0.577,1.535 1.185,2.05l-1.057,0.368c-0.439,-0.479 -0.775,-1.052 -0.975,-1.686Zm0.757,-4.137l0.877,0.507c-0.278,0.324 -0.498,0.7 -0.643,1.112l-0.878,-0.507c0.161,-0.403 0.379,-0.777 0.644,-1.112Zm2.892,7.237l0,-1.013c0.209,0.039 0.424,0.059 0.643,0.059c0.219,-0 0.434,-0.02 0.643,-0.059l-0,1.013c-0.21,0.03 -0.425,0.046 -0.643,0.046c-0.218,-0 -0.433,-0.016 -0.643,-0.046Zm3.96,-1.414l-1.057,-0.368c0.608,-0.515 1.038,-1.233 1.185,-2.05l0.847,0.732c-0.2,0.634 -0.536,1.207 -0.975,1.686Zm0.862,-4.711l-0.878,0.507c-0.145,-0.412 -0.365,-0.788 -0.643,-1.112l0.877,-0.507c0.265,0.335 0.483,0.709 0.644,1.112Zm-3.205,-2.723l0.209,1.099c-0.369,-0.133 -0.768,-0.205 -1.183,-0.205c-0.415,-0 -0.814,0.072 -1.183,0.205l0.209,-1.099c0.314,-0.069 0.64,-0.106 0.974,-0.106c0.334,-0 0.66,0.037 0.974,0.106Z"/>
</svg>`;
    }

    function getColors() {
        let i = 0;
        let colors = [];

        for (i = 0; i < EXPLOSIONS_COUNT; i += 1) {
            colors.push(ScenesCommon.randomColor());
        }

        return ScenesCommon.hexColors(colors);
    }

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


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

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

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

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

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

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