/**
 * Created by tangqianchong on 2015/10/26.k
 */
define(["adoreapp/widget/BaseWidget"], function (BaseWidget) {
    "use strict";
    return BaseWidget.extend(function (base) {
        return {
            _initWidget: function (attrs) {
                this.jqueryNode = $(attrs.template).attr({id: this.id});
            },
            _setText: function (attrs) {
                this.text = attrs.text;
                this.texts = this.text.split(" ");
                this.textsLength = this.texts.length;

                if (this.layoutFinished) {
                    this.createPart();
                    this.setText();
                    this.setWidth();
                    this.setPartToCurrent();
                }
            },
            _setIndex: function (attrs) {
                this.index = attrs.index;
                if (this.layoutFinished) {
                    this.createPart();
                    this.setText();
                    this.setWidth();
                    this.setPartToCurrent();
                }
            },

            //some assert functions
            createPart: function () {
                this.jqueryNode.children().remove();
                for (var i = 1; i <= this.textsLength; i++) {
                    var part = this.createDiv("wizard-part", i);
                    this.jqueryNode.append(part);

                    var left = this.createDiv("wizard-left", i);
                    var leftLine = this.createDiv("wizard-leftLine", i);
                    left.append(leftLine);

                    var right = this.createDiv("wizard-right", i);
                    var rightLine = this.createDiv("wizard-rightLine", i);
                    right.append(rightLine);

                    var up = this.createDiv("wizard-up", i);
                    var outCircle = this.createDiv("wizard-outCircle", i);
                    var inCircle = this.createDiv("wizard-inCircle", i);
                    var circleText = this.createSpan("wizard-circleText", i).html(i);
                    inCircle.append(circleText);
                    up.append(outCircle).append(inCircle);

                    var down = this.createDiv("wizard-down", i);
                    var text = this.createSpan("wizard-text", i);
                    down.append(text);

                    part.append(left).append(right).append(up).append(down);
                }
            },
            createDiv: function (className, index) {
                return ($("<div>").addClass(className).attr("id", this.jqueryNode.attr("id") + "-" + (className.split("-"))[1] + index));
            },
            createSpan: function (className, index) {
                return ($("<span>").addClass(className).attr("id", this.jqueryNode.attr("id") + "-" + (className.split("-"))[1] + index));
            },
            getComponent: function (className, index) {
                return (this.jqueryNode.find("#" + this.jqueryNode.attr("id") + "-" + (className.split("-"))[1] + index));
            },

            //partStateChangeFunction
            removeTick: function (index) {
                var component = this.getComponent("wizard-inCircle", index);
                var leftTick = component.find(".wizard-leftTick");
                var rightTick = component.find(".wizard-rightTick");
                leftTick.remove();
                rightTick.remove();
            },
            setNormalPart: function (index) {

                this.removeTick(index);
                this.getComponent("wizard-outCircle", index).removeClass().addClass("wizard-outCircle");
                this.getComponent("wizard-inCircle", index).removeClass().addClass("wizard-inCircle");
                this.getComponent("wizard-circleText", index).removeClass().addClass("wizard-circleText");
            },
            setCurrentPart: function (index) {
                this.removeTick(index);
                this.getComponent("wizard-outCircle", index).removeClass().addClass("wizard-outCircle-current");
                this.getComponent("wizard-inCircle", index).removeClass().addClass("wizard-inCircle");
                this.getComponent("wizard-circleText", index).removeClass().addClass("wizard-circleText-current")
            },
            setTickPart: function (index) {
                this.removeTick(index);
                var leftTick = $("<div>").addClass("wizard-leftTick");
                var rightTick = $("<div>").addClass("wizard-rightTick");

                this.getComponent("wizard-outCircle", index).removeClass().addClass("wizard-outCircle-tick");
                this.getComponent("wizard-inCircle", index).removeClass().addClass("wizard-inCircle-tick");
                this.getComponent("wizard-inCircle", index).append(leftTick).append(rightTick);
                this.getComponent("wizard-circleText", index).removeClass().addClass("wizard-circleText-tick");
            },


            //functionUsedByThe _function
            setWidth: function () {
                //handleHeightCalculate
                var mainHeight = parseInt(this.wizardHeight);
                var upHeight = parseInt(mainHeight/4)*3;
                var downHeight = parseInt(mainHeight/4);
                //水平伸长的处于组件最上和最下部分的阻挡模块的高度
                var assistUpBlockHeight = (mainHeight - upHeight - downHeight) / 2;
                var assistDownBlockHeight = mainHeight-upHeight-downHeight-assistUpBlockHeight;

                var outCircleDiameter=upHeight-3;
                var inCircleDiameter=upHeight-7;
                var textCircleDiameter=upHeight-11;

                var realWidth = parseInt(this.wizardWidth);
                var partWidth = realWidth / this.textsLength;
                var lineWidth = (partWidth - 66) / 2 - 1;
                for (var i = 1; i <= this.textsLength; i++) {
                    this.getComponent("wizard-part", i).css("width", partWidth);
                    this.getComponent("wizard-left", i).css("width", lineWidth);
                    this.getComponent("wizard-right", i).css("width", lineWidth);
                }
            },
            setText: function () {
                for (var i = 1; i <= this.textsLength; i++) {
                    this.getComponent("wizard-text", i).html(this.texts[i - 1]);
                }
            },
            setPartToCurrent: function () {
                for (var i = 1; i <= this.textsLength; i++) {
                    this.setNormalPart(i);
                }
                for (var j = 1; j < this.index; j++) {
                    this.setTickPart(j);
                }
                this.setCurrentPart(this.index);
            },

            //base.layout overwritten
            _layout: function () {
                base._layout.call(this);
                this.wizardWidth = this.jqueryNode.css("width");
                this.wizardHeight = this.jqueryNode.css("height");
                this.createPart();
                this.setText();
                this.setWidth();
                this.setPartToCurrent();
                this.layoutFinished = true;

                //functionDealsWithWindowsSizeChangeEvent
                this.originalWidth = window.innerWidth;
                var own = this;
                $(window).bind("resize." + this.id, function (e) {
                    if ($("#" + own.id).length === 0) {//没有正确销毁时（如父亲直接销毁）的逻辑
                        $(window).unbind("resize." + own.id);
                    }
                    if (window.innerWidth !== own.originalWidth) {
                        own.originalWidth = window.innerWidth;
                        // refresh
                        own.wizardWidth = own.jqueryNode.css("width");
                        own.setWidth();
                    }
                })
            }
        };
    });
});
