/// class ProspectDirector.Web.UI.Canvas.Charts.RenderBase

/**
@constructor
@class RenderBase
@namespace Web.UI.Canvas.Charts
**/
ProspectDirector.Web.UI.Canvas.Charts.RenderBase = Class.extend({

    //================================
    // FIELDS
    //================================

    _options: null,

    //================================
    // CONSTRUCTOR
    //================================

    init: function (options) {

        this._options = $.extend({
            data: {},
            domNode: null
        }, options);

    },

    //================================
    // PROPERTIES
    //================================

    get_options: function () {
        return this._options;
    },

    get_data: function () {
        return this.get_options().data;
    },

    get_domNode: function () {
        return this.get_options().domNode;
    },

    //================================
    // PUBLIC METHODS
    //================================

    render: function () {

        var data = this.get_data(),
            result = null,
            options = null;

        if (data.errors.length > 0) {
            this._handleErrors(data);
            return null;
        };

        options = this.getChartOptions(data);

        result = this.get_domNode().kendoChart(options);

        return result;

    },

    getChartOptions: function (data) {
        return {
            theme: "silver",
            categoryAxis: this._getCategoryAxis(data),
            chartArea: this._getChartArea(data),
            legend: this._getLegend(data),
            plotArea: this._getPlotArea(data),
            series: this._getSeries(data),
            seriesDefaults: this._getSeriesDefaults(data),
            title: this._getTitle(data),
            valueAxis: this._getValueAxis(data),
            transitions: false
        }
    },

    //================================
    // PRIVATE METHODS
    //================================

    _handleErrors: function (data) {

        var index = 0,
            errors = data.errors,
            height = this.get_options().height,
            width = this.get_options().width,
            message = "<b style=\"font-size:16px;\">Unable to create chart.</b><br/><br/>";

        if (this.get_options().isDesign === true) {

            $(errors).each(function (index, error) {
                message += error.message + "<br/>";
            });

        }
        else {
            message += "Contact your system administrator.<br/>";
        }

        $table = $("<table style=\"height:" + (height ? height + "px" : "100%") + ";width:" + (width ? width + "px" : "100%") + ";background-color:#FFFFFF;border:0px;vertical-align:middle;text-align:center;box-sizing:border-box;\"><tbody><tr><td><span style=\"font-size:12px;\">" + message + "</span></td></tr></tbody></table>");

        this.get_domNode().append($table);

    },

    _getTitle: function (chart) {

        var result = {},
            title = null;

        if (!chart.style || !chart.style.title) {
            return result;
        }

        title = chart.style.title;

        result["align"] = title.align;
        result["font"] = title.fontSize + " " + title.fontName;
        result["position"] = title.position;
        result["text"] = title.text;
        result["visible"] = title.visible;
        result["color"] = title.color;

        return result;

    },

    _getPlotArea: function (chart) {

        var result = {
            border: {}
        },
        plotArea = null;

        if (!chart.style || !chart.style.plotArea) {
            return result;
        }

        plotArea = chart.style.plotArea;

        result["background"] = plotArea.backColor;

        result.border["color"] = plotArea.borderColor;
        result.border["width"] = plotArea.borderWidth;

        return result;

    },

    _getChartArea: function (chart) {

        var result = {
            border: {}
        },
        chartArea = null;

        if (!chart.style || !chart.style.chartArea) {
            return result;
        }

        chartArea = chart.style.chartArea;

        if (this.get_options().height) {
            result["height"] = this.get_options().height;
        }

        if (this.get_options().width) {
            result["width"] = this.get_options().width;
        }

        result["background"] = chartArea.backColor;
        result.border["color"] = chartArea.borderColor;
        result.border["dashType"] = chartArea.borderStyle;
        result.border["width"] = chartArea.borderWidth;

        return result;

    },

    _getCategoryAxis: function (chart) {

        var result = [],
            index = 0,
            category = null,
            categories = [],
            categoryAxis = null;

        if (chart.data && chart.data.categories) {

            categories = chart.data.categories;

            if (categories.length > 0) {

                for (index; index < categories.length; index++) {
                    category = categories[index];
                    if (category.isDate) {
                        category = this._formatDateValues(category);
                    }
                    categoryAxis = this._getCategoryAxisStyle(chart);
                    categoryAxis.categories = category.data;
                    categoryAxis.justified = false;
                    categoryAxis.labels["format"] = this._getDataFormatString(category["format"]);
                    result[index] = categoryAxis;
                }

            }
            else {
                result = this._getCategoryAxisStyle(chart);
            }

        }

        return result;

    },

    _getCategoryAxisStyle: function (chart) {

        var result = {
            categories: [],
            labels: {},
            line: {},
            title: {},
            majorGridLines: {},
            minorGridLines: {},
            majorTicks: {},
            minorTicks: {}
        },
        labels = null,
        line = null,
        title = null,
        majorGridLines = null,
        minorGridLines = null,
        categoryAxis = null;

        if (!chart.style || !chart.style.categoryAxis) {
            return result;
        }

        categoryAxis = chart.style.categoryAxis;

        if (categoryAxis.labels) {

            labels = categoryAxis.labels;

            result.labels["color"] = labels.color;
            result.labels["font"] = labels.fontSize + " " + labels.fontName;
            result.labels["rotation"] = labels.rotation;
            result.labels["visible"] = labels.visible;

        }

        if (categoryAxis.line) {

            line = categoryAxis.line;

            result.line["color"] = line.color;
            result.line["width"] = line.width;
            result.line["visible"] = line.visible;

        }

        if (categoryAxis.title) {

            title = categoryAxis.title;

            result.title["color"] = title.color;
            result.title["font"] = title.fontSize + " " + title.fontName;
            result.title["position"] = title.position;
            result.title["rotation"] = title.rotation;
            result.title["text"] = title.text;
            result.title["visible"] = title.visible;

        }

        if (categoryAxis.majorGridLines) {

            majorGridLines = categoryAxis.majorGridLines;

            result.majorGridLines["color"] = majorGridLines.color;
            result.majorGridLines["dashType"] = majorGridLines.dashType;
            result.majorGridLines["visible"] = majorGridLines.visible;
            result.majorTicks["visible"] = majorGridLines.tickMarks;

        }

        if (categoryAxis.minorGridLines) {

            minorGridLines = categoryAxis.minorGridLines;

            result.minorGridLines["color"] = minorGridLines.color;
            result.minorGridLines["dashType"] = minorGridLines.dashType;
            result.minorGridLines["visible"] = minorGridLines.visible;
            result.minorTicks["visible"] = minorGridLines.tickMarks;

        }

        return result;

    },

    _getLegend: function (chart) {

        var result = {
            border: {},
            labels: {}
        },
        border = null,
        labels = null,
        legend = null;

        if (!chart.style || !chart.style.legend) {
            return result;
        }

        legend = chart.style.legend;

        result["position"] = legend.position;
        result["visible"] = legend.visible;

        if (legend.border) {

            border = legend.border;

            result.border["color"] = border.color;
            result.border["width"] = border.width;

        }

        if (legend.labels) {

            labels = legend.labels;

            result.labels["color"] = labels.color;
            result.labels["font"] = labels.fontSize + " " + labels.fontName;

        }

        return result;

    },

    _getSeriesDefaults: function (chart) {

        return {
            tooltip: {
                visible: true
            }
        };

    },

    _getSeries: function (chart) {

        var result = [],
            index = 0,
            serie = null,
            series = chart.data.series;

        for (index; index < series.length; index++) {
            serie = $.extend(true, {}, {
                tooltip: {
                    format: this._getDataFormatString(series[index]["format"])
                }
            }, series[index]);
            if (serie.isDate) {
                serie = this._formatDateValues(serie);
            }
            serie.stack = chart.style.valueAxis.stack;
            result[index] = serie;
        }

        return result;

    },

    _getValueAxis: function (chart) {

        var result = {
            categories: [],
            labels: {},
            line: {},
            title: {},
            majorGridLines: {},
            minorGridLines: {},
            majorTicks: {},
            minorTicks: {},
            axisCrossingValue: [0, -1000000000000, -1000000000000]
        },
        labels = null,
        line = null,
        title = null,
        majorGridLines = null,
        minorGridLines = null,
        valueAxis = null;

        if (!chart.style || !chart.style.valueAxis) {
            return result;
        }

        valueAxis = chart.style.valueAxis;

        if (valueAxis.labels) {

            labels = valueAxis.labels;

            result.labels["color"] = labels.color;
            result.labels["font"] = labels.fontSize + " " + labels.fontName;
            result.labels["rotation"] = labels.rotation;
            result.labels["visible"] = labels.visible;

        }

        if (valueAxis.line) {

            line = valueAxis.line;

            result.line["color"] = line.color;
            result.line["width"] = line.width;
            result.line["visible"] = line.visible;

        }

        if (valueAxis.title) {

            title = valueAxis.title;

            result.title["color"] = title.color;
            result.title["font"] = title.fontSize + " " + title.fontName;
            result.title["position"] = title.position;
            result.title["rotation"] = title.rotation;
            result.title["text"] = title.text;
            result.title["visible"] = title.visible;

        }

        if (valueAxis.majorGridLines) {

            majorGridLines = valueAxis.majorGridLines;

            result.majorGridLines["color"] = majorGridLines.color;
            result.majorGridLines["dashType"] = majorGridLines.dashType;
            result.majorGridLines["visible"] = majorGridLines.visible;
            result.majorTicks["visible"] = majorGridLines.visible;

        }

        if (valueAxis.minorGridLines) {

            minorGridLines = valueAxis.minorGridLines;

            result.minorGridLines["color"] = minorGridLines.color;
            result.minorGridLines["dashType"] = minorGridLines.dashType;
            result.minorGridLines["visible"] = minorGridLines.visible;
            result.minorTicks["visible"] = minorGridLines.visible;

        }

        if (chart.data && chart.data.series.length > 0) {
            result.labels["format"] = this._getDataFormatString(chart.data.series[0]["format"]);
        }

        return result;

    },

    _getDataFormatString: function (format) {

        var result = null

        if (format) {
            result = "{0:" + format + "}";
        }

        return result;

    },

    _formatDateValues: function (src) {

        var index = 0,
            datum = null;

        if (src.isDate) {

            for (index; index < src.data.length; index++) {
                datum = src.data[index];
                if (datum != null) {
                    var num = Date.parse(datum);
                    if (!isNaN(num)) {
                        src.data[index] = kendo.toString(new Date(num), src.format);
                    }
                }
            }

        }

        return src;

    }

});
