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

export class Toolbar extends Composite {
    private static TemplateToolbar = `
    <nav class="top-nav adore_header">
    <div></div>
    </nav>`;
    private static Template = `
    <a class="waves-effect btn-flat adore_button adore_button_flat btn-large" style="color:white" tabindex="-1">
    <i style="position: absolute;right: 10px;font-size: 2.4em" class="mdi-navigation-more-vert">
    </i>
    <span>
    </span>
    </a>
    `;
    private typeFlag: string;
    private instance: string;
    private $ulNode: JQuery;
    private $dropDownBtn: JQuery;
    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        this.typeFlag = "";
        this.instance = "Toolbar";
    }
    public toString(): string {
        return this.typeFlag;
    }
    private createDropDownBox() {
        let own = this;
        let dropDownListWidth = 108;
        this.$ulNode = $("<ul>").addClass("card-panel adore_toolbar_dropDownList")
            .insertAfter(this.$thisNode)
            .css({ width: dropDownListWidth, display: "none", height: 0, padding: 0 })
            .on("click", function () {
                $(this).attr("popup", "true").css({ height: 0, display: "none" });
            });
        this.$dropDownBtn = $(Toolbar.Template)
            .appendTo(this.$attachNode).on("click", function () {
                if (parseInt(own.$ulNode.css("height").replace("px", "")) === 0) {
                    own.$ulNode.attr("popup", "true").css({ height: own.children.length * 54, display: "block" });
                } else {
                    own.$ulNode.attr("popup", "true").css({ height: 0, display: "none" });
                }
            });
        this.$attachNode = this.$ulNode;
    }
    public layout() {
        super.layout();
        //add dropDownList
        let own = this;
        let originAttachNode = this.$attachNode;
        let dropDownListWidth = 108;
        if (parseInt(this.$thisNode.css("width").replace("px", "")) < dropDownListWidth) {
            this.typeFlag = "ToolBar";
            this.createDropDownBox();
        }

        $(window).resize(function () {
            if (parseInt(own.$thisNode.css("width").replace("px", "")) < dropDownListWidth) {
                if (own.$attachNode === originAttachNode) {
                    //
                    if (own.$ulNode) {
                        own.$attachNode = own.$ulNode;
                        own.$ulNode.css({ display: "none", height: 0 });
                        own.$dropDownBtn.css({ display: "block" });
                    } else {
                        own.createDropDownBox();
                    }
                    own.typeFlag = "ToolBar";
                    for (let i = 0; i < own.children.length; i++) {
                        if (own.children[i] instanceof Button) {
                            (<Button>own.children[i]).refreshToolbar(<Button>own.children[i]);
                        }
                    }
                } else {
                    //不处理
                }
            } else {
                if (own.$attachNode === originAttachNode) {
                    //不处理
                } else {
                    own.$attachNode = originAttachNode;
                    own.typeFlag = "";
                    own.$dropDownBtn.css("display", "none");
                    own.$ulNode.css({ display: "none", height: 0 });
                    for (let i = 0; i < own.children.length; i++) {
                        own.children[i].postLayout();
                        // if (own.children[i].tooltip) {
                        //     own.children[i].jqueryNode.find("i").attr("class", own.children[i].icon);
                        //     own.children[i].setText({ text: "" });
                        // }
                    }
                }
            }
        });
    }
}
