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

const CometScene = (function () {
    const GRADIENT_LENGTH = 50;
    const ACTIVE_LEDS_COUNT = 6;

    function getName() {
        return "Comet";
    }

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

    function getParameters() {
        return [
            {
                name: "color_hint",
                title: "Define up to 6 colors for your scene.",
                type: "hint"
            },
            {
                name: "colors",
                title: "Colors",
                type: "color_list",
                min: 2,
                max: 6,
                default: ["#ff0000", "#ffff00", "#ffffff", "#00ff00", "#0000ff", "#000000"]
            },
            {
                name: "speed",
                title: "Speed",
                type: "int_enum",
/*jslint-disable*/
                values: {300: "Very Slow", 100: "Slow", 55: "Medium", 10: "Fast"},
/*jslint-enable*/
                default: 55
            },
            {
                name: "direction",
                title: "Direction",
                type: "int_enum",
/*jslint-disable*/
                values: {1: "Forward", 2: "Backwards"},
/*jslint-enable*/
                default: 1
            }
        ];
    }

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

        const config = getParameters();
        const speed = ScenesCommon.valueOrDefaultFromParameters(parameters, config, 'speed');
        const direction = ScenesCommon.valueOrDefaultFromParameters(parameters, config, 'direction');
        const parameterColors = ScenesCommon.valueOrDefaultFromParameters(parameters, config, 'colors');

        let i = 0;
        let colors = [];
        let color;

        for (i = 0; i < parameterColors.length; i += 1) {
            color = ScenesCommon.colorFromHexString(parameterColors[i]);
            if (!ScenesCommon.colorEqual(color, ScenesCommon.BLACK_COLOR)) {
                colors.push(color);
            }
        }


        // Create scene data
        let sceneData = ScenesCommon.createSceneData(sceneID, numLeds, sceneName);
        let c = 0;

        for (c = 0; c < colors.length; c += 1) {
            color = colors[c];
            let gradient = ScenesCommon.gradientColors(color, ScenesCommon.BLACK_COLOR, GRADIENT_LENGTH);
            let j = 0;

            // Cycle through all sets
            for (j = 0; j < numLeds; j += 1) {

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

                if (parseInt(direction) === 1) {
                    for (i = 0; i < numLeds; i += 1) {
                        addColorToSceneDataSet(sceneDataSet, i, j, color, gradient);
                    }
                } else {
                    for (i = numLeds - 1; i >= 0; i -= 1) {
                        addColorToSceneDataSet(sceneDataSet, i, j, color, gradient);
                    }
                }

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

        return sceneData;
    }

    function addColorToSceneDataSet(sceneDataSet, i, offset, color, gradient) {
        if (i >= offset && i < offset + ACTIVE_LEDS_COUNT) {
            ScenesCommon.pushColor(sceneDataSet.rgbArray, color);
        } else if (i < offset && i > offset - gradient.length) {
            ScenesCommon.pushColor(sceneDataSet.rgbArray, gradient[offset - i]);
        } else {
            ScenesCommon.pushColor(sceneDataSet.rgbArray, ScenesCommon.BLACK_COLOR);
        }
    }

    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.06,8.35c0.224,0 0.39,-0.125 0.553,-0.269c0.177,-0.157 0.345,-0.343 0.522,-0.529c0.59,-0.619 1.27,-1.202 2.366,-1.202c1.087,0 1.786,0.578 2.396,1.192c0.185,0.186 0.36,0.374 0.545,0.53c0.176,0.15 0.353,0.278 0.586,0.278c0.359,0 0.65,0.291 0.65,0.65c0,0.359 -0.291,0.65 -0.65,0.65c-0.7,-0 -1.24,-0.38 -1.757,-0.892c-0.471,-0.467 -0.91,-1.108 -1.77,-1.108c-0.852,-0 -1.262,0.636 -1.709,1.098c-0.502,0.518 -1.029,0.902 -1.732,0.902c-0.007,-0 -0.014,-0 -0.02,-0c-0.009,-0.001 -0.045,0 -0.1,-0c-0.703,-0 -1.23,-0.384 -1.732,-0.902c-0.447,-0.462 -0.857,-1.098 -1.709,-1.098c-0.86,-0 -1.299,0.641 -1.77,1.108c-0.517,0.512 -1.057,0.892 -1.757,0.892c-0.359,-0 -0.65,-0.291 -0.65,-0.65c-0,-0.359 0.291,-0.65 0.65,-0.65c0.233,0 0.41,-0.128 0.586,-0.278c0.185,-0.156 0.36,-0.344 0.545,-0.53c0.61,-0.614 1.309,-1.192 2.396,-1.192c1.096,0 1.776,0.583 2.366,1.202c0.177,0.186 0.345,0.372 0.522,0.529c0.163,0.144 0.329,0.269 0.553,0.269c0.001,0 0.003,0 0.005,0c0.053,0 0.092,0 0.115,0Zm-5.818,3.333c-0.101,-0.344 0.096,-0.706 0.44,-0.807c0.344,-0.101 0.706,0.097 0.807,0.441c-0,-0 0.207,0.533 0.984,0.533c0.233,0 0.41,-0.128 0.586,-0.278c0.184,-0.156 0.36,-0.344 0.545,-0.53c0.61,-0.614 1.309,-1.192 2.396,-1.192c1.095,0 1.776,0.583 2.366,1.202c0.177,0.186 0.344,0.372 0.521,0.529c0.164,0.144 0.33,0.269 0.554,0.269c0.819,0 1.088,-0.587 1.088,-0.587c0.131,-0.334 0.509,-0.499 0.843,-0.368c0.334,0.13 0.499,0.508 0.368,0.842c-0,-0 -0.512,1.413 -2.299,1.413c-0.703,-0 -1.23,-0.384 -1.732,-0.902c-0.447,-0.462 -0.858,-1.098 -1.709,-1.098c-0.86,-0 -1.299,0.641 -1.77,1.108c-0.517,0.512 -1.057,0.892 -1.757,0.892c-1.853,-0 -2.231,-1.467 -2.231,-1.467Zm-0,-7c-0.101,-0.344 0.096,-0.706 0.44,-0.807c0.344,-0.101 0.706,0.097 0.807,0.441c-0,-0 0.207,0.533 0.984,0.533c0.233,0 0.41,-0.128 0.586,-0.278c0.184,-0.156 0.36,-0.344 0.545,-0.53c0.61,-0.614 1.309,-1.192 2.396,-1.192c1.095,0 1.776,0.583 2.366,1.202c0.177,0.186 0.344,0.372 0.521,0.529c0.164,0.144 0.33,0.269 0.554,0.269c0.819,0 1.088,-0.587 1.088,-0.587c0.131,-0.334 0.509,-0.499 0.843,-0.368c0.334,0.13 0.499,0.508 0.368,0.842c-0,-0 -0.512,1.413 -2.299,1.413c-0.703,-0 -1.23,-0.384 -1.732,-0.902c-0.447,-0.462 -0.858,-1.098 -1.709,-1.098c-0.86,-0 -1.299,0.641 -1.77,1.108c-0.517,0.512 -1.057,0.892 -1.757,0.892c-1.853,-0 -2.231,-1.467 -2.231,-1.467Z"/>
</svg>`;
    }
    
    function parametersForAIColors(parameters, colors, dynamism) {
        const config = getParameters();
        const speed = ScenesCommon.valueOrDefaultFromParameters(parameters, config, "speed");
        const direction = ScenesCommon.valueOrDefaultFromParameters(parameters, config, "direction");

        let result = {
            "colors": [],
            "speed": speed,
            "direction": direction
        };

        if (colors === null) {
            colors = ScenesCommon.generateAIColorFromParameters(parameters, config);
        } else {
            colors = ScenesCommon.alterArrayForMinMaxParameterName(colors, config, "colors");
        }

        colors.forEach(function (color) {
            result.colors.push(color.colorString);
        });

        if (dynamism !== null) {
            let dynamismFloat = parseFloat(dynamism);

            if (dynamismFloat < ScenesCommon.DYNAMISM_SLOW) { // Slow
                result.speed = 100;
            } else if (dynamismFloat < ScenesCommon.DYNAMISM_MEDIUM) { // Medium
                result.speed = 55;
            } else if (dynamismFloat < ScenesCommon.DYNAMISM_FAST) { // Fast
                result.speed = 10;
            } else { // Fast, too
                result.speed = 10;
            }
        }

        return result;
    }

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


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

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

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

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

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

function parametersForAIColors(parameters, colors, dynamism) {
    return CometScene.parametersForAIColors(parameters, colors, dynamism);
}
