/*
 *  @author yangzeming

 */
define(function (require, exports, module) {

    var Chart = _.Class({
        initial: function (options) {
            var defaultOp = {
                con: "body",
                // data: [],//{ "Name": "港口数据", "Count": "2324" }, { "Name": "机场数据", "Count": "7660"}],
                left: 38,
                bottom: 20,
                txtBottom: 2,
                yh: 160,

                xw: 230



            };
            this.options = $.extend(true, {}, defaultOp, options);
            this.$con = $(this.options.con);
            this.eData = this.gData();
            this.render();

        },
        gData: function () {
            var data = this.options.data,
                xduration = data.length,
                width = (this.options.xw - 10) / xduration,
                left = width - 38,
                max = 0;
            min = 0;
            var dataObj = {
                ydata: [],
                xdata: []
            };
            function getBetterMaxValue(pMaxValue) {
                for (var i = 10; i > 1; i--)
                    if (pMaxValue >= Math.pow(10, i)) return parseInt(parseFloat(pMaxValue) / Math.pow(10, i) + 1) * Math.pow(10, i)
                return parseInt(parseFloat(pMaxValue) / 10 + 1) * 10
            }
            for (var i = 0, len = data.length; i < len; i++) {
                var it = +data[i]["Count"];
                if (it > max) {
                    max = it;
                }
                if (it < min) {
                    min = it;
                }
            }
            var betterMin = getBetterMaxValue(min);
            var betterMax = getBetterMaxValue(max),
                sMin = betterMax / 5;
            for (var i = 0; i < 5; i++) {
                dataObj.ydata.push(sMin * (i + 1));
            }
            dataObj.betterMax = betterMax;
            dataObj.yh = this.options.yh - 10;




            for (var i = 0, len = data.length; i < len; i++) {
                var item = {};
                item.data = data[i];
                item.txtLeft = left + width * i + this.options.left;
                item.txtBottom = this.options.txtBottom-2;
                item.barLeft = item.txtLeft;
                item.barBottom = this.options.bottom + 1;
                dataObj.xdata.push(item);

            }
            return dataObj;
        },
        render: function () {
            this.renderLineY();
            this.renderLineX();
            this.renderYData();
            this.renderXData();
            this.renderData();

        },
        renderLineY: function () {
            var line = '<div class="eui-chart-line-y" data-role="liney"></div>';
            this.$con.append($(line).css({
                left: this.options.left,
                bottom: this.options.bottom,
                height: this.options.yh

            }));


        },
        renderLineX: function () {
            var line = '<div class="eui-chart-line-x" data-role="linex"></div>';
            this.$con.append($(line).css({
                left: this.options.left + 2,
                bottom: this.options.bottom - 1,
                width: this.options.xw

            }));

        },
        renderData: function () {

            var data = this.eData.xdata;
            for (var i = 0, len = data.length; i < len; i++) {
                var color = ["yellow", "blue", "green"],
                    tmp = color[i] ? color[i] : "yellow";

                var bar = $('<div class="eui-bar eui-bar-'+tmp+'"><div class="eui-bar-'+tmp+'-b" ></div></div>'),
                     item = data[i];
                var height = (+item.data.Count / this.eData.betterMax) * (this.eData.yh - 8);
                bar.css({
                    left: item.barLeft,
                    bottom: item.barBottom,
                    height: 0

                });
                //bar.hide();

                this.$con.append(bar);
                bar.animate({ height: height }, 1000, function () {

                });

            }


        },
        renderYData: function () {
            var data = this.eData.ydata,
                yh = this.options.yh - 15,
                dur = yh / 5;


            for (var i = 0, len = data.length; i < len; i++) {
                var item = data[i],
                    txt = $(' <div class="eui-bar-text" >' + item + '</div>');
                txt.css({
                    left: 0,
                    bottom: this.options.bottom + dur * (i + 1)
                });

                this.$con.append(txt);
            }
        },
        renderXData: function () {
            var data = this.eData.xdata;
            for (var i = 0, len = data.length; i < len; i++) {
                var item = data[i],
                    txt = $(' <div class="eui-bar-text" >' + item.data.Name + '</div>');
                txt.css({
                    left: item.txtLeft,
                    bottom: item.txtBottom
                });

                this.$con.append(txt);

            }


        }

    });
    module.exports = Chart;
});