/**
 * Base.Chart 1.0
 *
 * Requiere
 *     Base 1.0
 *     Base.Filter 1.0
 *
 *     morris.js
 */
$(function() {

    $.extend(Base, {
        Chart: {

            create: function (type, options) {

                var _i18n = {
                    language: {}
                };

                _i18n = $.extend(true, _i18n, Base.i18n.chart);

                if (options._i18n !== undefined && options._i18n != null) {
                    _i18n = $.extend(true, _i18n, options._i18n);
                }

                var defaultOptions = {
                    element: 'graph',
                    data: [],
                    xkey: 'X',
                    ykey: ['Y'],
                    labels: ['Y'],
                    reload_url: null,
                    postdata: {},
                    show_legend: true,
                    colors : null
                };

                var finallyOptions = $.extend(true, defaultOptions, options);


                var realoadUrl = function (chart, options) {

                    Base.ajax(
                        options.reload_url,
                        options.postdata,
                        function (data) {

                            chart.setData(data);
                        }
                    );
                };


                var chart = null;
                switch (type) {

                    case 'line':
                        finallyOptions = $.extend(true,
                            {
                                parseTime: false,
                                hideHover: true,
                                barOpacity: 1.0,
                                barRadius: [0, 0, 0, 0],
                                xLabelAngle: 20,
                                barRatio: 0.4,
                                resize: true
                            },
                            finallyOptions);

                        if(null != options.colors){
                            finallyOptions = $.extend(true, {lineColors: options.colors, }, finallyOptions);
                        }

                        chart = new Morris.Line(finallyOptions);
                        break;

                    case 'bar':
                        finallyOptions = $.extend(true,
                            {
                                parseTime: false,
                                hideHover: true,
                                barOpacity: 1.0,
                                barRadius: [0, 0, 0, 0],
                                xLabelAngle: 20,
                                barRatio: 0.4,
                                resize: true,
                                stacked: false
                            },
                            finallyOptions);

                        if(null != options.colors){
                            finallyOptions = $.extend(true, {barColors: options.colors, }, finallyOptions);
                        }

                        chart = new Morris.Bar(finallyOptions);
                        break;

                    case 'stacked':
                        finallyOptions = $.extend(true,
                            {
                                parseTime: false,
                                hideHover: true,
                                barOpacity: 1.0,
                                barRadius: [0, 0, 0, 0],
                                xLabelAngle: 20,
                                barRatio: 0.4,
                                resize: true,
                                stacked: true
                            },
                            finallyOptions);
                        chart = new Morris.Bar(finallyOptions);

                        if(null != options.colors){
                            finallyOptions = $.extend(true, {barColors: options.colors, }, finallyOptions);
                        }

                        break;

                    case 'area':
                        finallyOptions = $.extend(true,
                            {
                                parseTime: false,
                                hideHover: true,
                                barOpacity: 1.0,
                                barRadius: [0, 0, 0, 0],
                                xLabelAngle: 20,
                                barRatio: 0.4,
                                resize: true
                            },
                            finallyOptions);

                        if(null != options.colors){
                            finallyOptions = $.extend(true, {barColors: options.colors, }, finallyOptions);
                        }

                        chart = new Morris.Area(finallyOptions);
                        break;

                    case 'ring':
                        finallyOptions = $.extend(true,
                            {
                                parseTime: false,
                                hideHover: true,
                                barOpacity: 1.0,
                                barRadius: [0, 0, 0, 0],
                                xLabelAngle: 20,
                                barRatio: 0.4,
                                resize: true
                            },
                            finallyOptions);

                        if(null != options.colors){
                            finallyOptions = $.extend(true, {colors: options.colors, }, finallyOptions);
                        }

                        chart = new Morris.Donut(finallyOptions);
                        break;
                }

                //Load Data Ajax
                if (finallyOptions.reload_url != null) {

                    realoadUrl(chart, finallyOptions);
                }

                if(finallyOptions.show_legend){

                    chart.options.labels.forEach(function(label, i){

                        var legendItem = $("<div class='graph_legend_item'></div>");

                        var legendLabel= $('<span class="graph_legend_item_label">'+label+'</span>');

                        var legendBox = "";

                        if(type == 'line') {
                            legendBox = $('<div class="graph_legend_box"></div>').css('background-color', chart.options.lineColors[i]);
                        }

                        if(type == 'bar' || type == 'stacked' || type == 'area') {
                            legendBox = $('<div class="graph_legend_box"></div>').css('background-color', chart.options.barColors[i]);
                        }

                        if(type == 'ring') {
                            legendBox = $('<div class="graph_legend_box"></div>').css('background-color', chart.options.colors[i]);
                        }

                        $(legendItem).append(legendBox);
                        $(legendItem).append(legendLabel);


                        $('#legend_' + options.element).append(legendItem);

                    })
                }

                return chart;
            },

            _qxChart : function (table) {

                return {

                    ver : function () {

                        console.log("Base.Chart 1.0");
                        console.log("Requiere: Chart 1.0; Chart.Filter 1.0");
                    }
                }
            }
        }

    });

    $.fn.QxChart = function () {

        var chart = this;

        return Base.Chart._qxChart(chart);
    }
});