/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * Special Declaration: These technical material reserved as the technical secrets by AGREE 
 * TECHNOLOGY have been protected by the "Copyright Law" "ordinances on Protection of Computer 
 * Software" and other relevant administrative regulations and international treaties. Without 
 * the written permission of the Company, no person may use (including but not limited to the 
 * illegal copy, distribute, display, image, upload, and download) and disclose the above 
 * technical documents to any third party. Otherwise, any infringer shall afford the legal 
 * liability to the company.
 */
/**
 * Created by liushuai on 2015/1/15.
 */
define(["adoreapp/widget/BaseWidget"
    , "adoreapp/widget/EventHub"
    , "adoreapp/widget/constants/ADORE"], function (BaseWidget, EventHub, ADORE) {
    'use strict';
    return BaseWidget.extend(function (base) {
        return {
            _initWidget: function (attrs) {
                var templates = attrs.template.split("<!-- split -->");

                var style = attrs.style;

                this.float = false;
                this.isLarge = false;
                if ((style & ADORE.Float) === ADORE.Float) {
                    this.jqueryNode = $(templates[1]).attr({id: this.id});
                    this.btnNode = this.jqueryNode.find("a").attr({id: this.id});
                    this.isFloat = true;
                } else if ((style & ADORE.Flat) === ADORE.Flat) {
                    this.jqueryNode = $(templates[2]).attr({id: this.id});
                    this.btnNode = this.jqueryNode;
                } else if ((style & ADORE.Normal) === ADORE.Normal) {
                    this.jqueryNode = $(templates[0]).attr({id: this.id});
                    this.btnNode = this.jqueryNode;
                }


                if ((style & ADORE.Large) === ADORE.Large) {
                    this.btnNode.addClass("btn-large");
                    this.isLarge = true;
                }

                var own = this;
                this.btnNode.on("click", function (e) {
                    EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET, {id: this.id, evtType: EventHub.CLICK});
                }).on("focus", function (e) {
                    own.sendFocusGain();
                });
            },

            _setTooltip: function (attrs) {
                this.tooltip = attrs.tooltip;
            },

            _setFocus: function () {
                if (document.activeElement !== this.btnNode[0]) {
                    this.btnNode.focus();
                }
            },

            _setEnabled: function (attrs) {
                var enabled = attrs.enabled;
                if (enabled) {
                    this.jqueryNode.removeClass("disabled");
                } else {
                    this.jqueryNode.addClass("disabled");
                }
            },

            _setText: function (attrs) {
                this.text = attrs.text;
                this.btnNode.find("span").text(this.text);
                this.adjustInternalLayout();
            },

            _setIcon: function (attrs) {
                this.icon = attrs.icon;
                this.btnNode.find("i").attr("class", this.icon);
                if (this.isLarge) {
                    this.btnNode.find("i").css("font-size", "3rem");
                }
                this.adjustInternalLayout();
            },

            /**
             * 调整内部布局
             */
            adjustInternalLayout: function () {
                if (this.icon && this.text) {
                    this.btnNode.find("span").addClass("adore_button_span");
                    this.btnNode.find("i").addClass("adore_button_icon");
                } else {
                    this.btnNode.find("span").removeClass("adore_button_span");
                    this.btnNode.find("i").removeClass("adore_button_icon");
                }
            },

            _layout: function () {
                base._layout.call(this);
                window.Waves.displayEffect({id: this.id});
            },

            _css: function (attrs) {
                if (this.isFloat) {
                    this.btnNode.css(attrs.cssStyle);
                } else {
                    base._css.call(this, attrs);
                }
            }
        };
    });
});