/*global define*/

define(
    [],
    function () {
        "use strict";

        var BASE_COLORS = [
                [ 136, 32, 32 ],
                [ 224, 64, 64 ],
                [ 240, 160, 72 ],
                [ 255, 248, 96 ],
                [ 128, 240, 72 ],
                [ 128, 248, 248 ],
                [ 88, 144, 224 ],
                [ 0, 72, 240 ],
                [ 136, 80, 240 ],
                [ 224, 96, 248 ]
            ],
            GRADIENTS = [0.75, 0.50, 0.25, -0.25, -0.50, -0.75],
            GROUPS = [];

        function toWebColor(triplet) {
            return '#' + triplet.map(function (v) {
                return (v < 16 ? '0' : '') + v.toString(16);
            }).join('');
        }

        function toGradient(triplet, value) {
            return triplet.map(function (v) {
                return Math.round(value > 0 ?
                        (v + (255 - v) * value) :
                        (v * (1 + value))
                    );
            });
        }

        function initializeGroups() {
            var i, group;

            // Ten grayscale colors
            group = [];
            while (group.length < 10) {
                group.push(toWebColor([
                    Math.round(28.3333 * group.length),
                    Math.round(28.3333 * group.length),
                    Math.round(28.3333 * group.length)
                ]));
            }
            GROUPS.push(group);

            // Ten basic colors
            GROUPS.push(BASE_COLORS.map(toWebColor));

            // ...and some gradients of those colors
            group = [];
            GRADIENTS.forEach(function (v) {
                group = group.concat(BASE_COLORS.map(function (c) {
                    return toWebColor(toGradient(c, v));
                }));
            });
            GROUPS.push(group);
        }



        function ColorController() {
            if (GROUPS.length === 0) {
                initializeGroups();
            }

            return {
                groups: function () {
                    return GROUPS;
                }
            };
        }

        return ColorController;
    }
);