/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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.
 */
import {Widget} from "widget/Widget";
import {Composite} from "widget/Composite";
import {ADORE} from "ADORE";
import {EventHub} from "event/EventHub";
import {MessageType} from "event/MessageType";
import {Event} from "event/Event";
/**
 * Created by huang.dy on 2016/5/23.
 */
export class Button extends Widget {

    private isFloat: boolean = false;
    private isLarge: boolean = false;
    private $btnNode: JQuery;
    private text: string;
    private icon: string;
    private tooltip:string;

    private static TemplateNormal = `
        <div class="waves-effect waves-light btn adore_button" tabindex="-1" data-position="top" data-delay="50">
            <span></span>
            <i></i>
        </div>`;
    private static TemplateFloat = `
        <div>
            <a class="btn-floating waves-effect waves-light adore_button" tabindex="-1" data-position="top" data-delay="50"><span></span><i></i></a>
        </div>`;
    private static TemplateFlat = `<a class="waves-effect btn-flat adore_button adore_button_flat" tabindex="-1" data-position="top" data-delay="50"><i></i><span></span></a>`;

    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        if ((this.style & ADORE.Float) === ADORE.Float) {
            this.$thisNode = $(Button.TemplateFloat).attr({ id: this.id });
            this.$btnNode = this.$thisNode.find("a").attr({ id: this.id });
            this.isFloat = true;
        } else if ((this.style & ADORE.Flat) === ADORE.Flat) {
            this.$thisNode = $(Button.TemplateFlat).attr({ id: this.id });
            this.$btnNode = this.$thisNode;
        } else if ((this.style & ADORE.Normal) === ADORE.Normal) {
            this.$thisNode = $(Button.TemplateNormal).attr({ id: this.id });
            this.$btnNode = this.$thisNode;
        }

        if ((this.style & ADORE.Large) === ADORE.Large) {
            this.$btnNode.addClass("btn-large");
            this.isLarge = true;
        }
        let own = this;
        this.$btnNode.on("click", (e: BaseJQueryEventObject) => {
            if (this.enabled) {
                EventHub.dispatchEvent(MessageType.WIDGET, { id: this.id, evtType: Event.CLICK });
            }
        }).on("focus", (e: BaseJQueryEventObject) => {
            if (this.enabled) {
                this.sendFocusGain();
            }
        });
    }

    public setFocus(): void {
        if (document.activeElement !== this.$btnNode[0]) {
            this.$btnNode.focus();
        }
    }

    public setTooltip(args: { tooltip: string }) {
        this.tooltip = args.tooltip;
        this.$thisNode.attr("data-tooltip",this.tooltip);
        this.$thisNode.tooltip({delay: 50});
    }

    public setEnabled(args: { enabled: boolean }) {
        super.setEnabled(args);
        if (this.enabled) {
            this.$thisNode.removeClass("disabled");
        } else {
            this.$thisNode.addClass("disabled");
        }
    }

    public setText(args: { text: string }) {
        this.text = args.text;
        this.$btnNode.find("span").text(this.text);
        this.adjustInternalLayout();
    }

    public setIcon(args: { icon: string }) {
        this.icon = args.icon;
        this.$btnNode.find("i").attr("class", this.icon);
        if (this.isLarge) {
            this.$btnNode.find("i").css("font-size", "3rem");
        }
        this.adjustInternalLayout();
    }

    /**
     * 调整内部布局
     */
    private adjustInternalLayout() {
        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");
        }
    }

    public postLayout() {
        window.Waves.displayEffect({ id: this.id });
    }

    public css(args: { cssStyle: {} }) {
        if (this.isFloat) {
            this.$btnNode.css(args.cssStyle);
        } else {
            super.css(args);
        }
    }

    //只为Toolbar
    public refreshToolbar(children: Button) {
        children.postLayout();
        let childCls = children.$btnNode[1].className
            .replace("waves-effect btn-flat adore_button adore_button_flat btn-large", "");
        children.$btnNode.removeClass(childCls).addClass("adore_toolbar_color");

        // if (children.tooltip) {
        //     children.$thisNode.find("i").removeAttr("class");
        //     children.setText({ text: children.tooltip });
        //    children.$btnNode.find("span").addClass("adore_toolbar_btnSpan");
        // }
    }
}