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

export class TabFolder extends Composite {

    private static TemplateNormal = `
        <div>
            <ul class="adore_tabs">
                <li class="adore_tab_space col"></li>
                <li class="tab_left_move col"><div class="arrow_left"/></li>
                <li class="tab_right_move"><div class="arrow_right"/></li>
            </ul>
        </div>`;

    public $tabs: JQuery;
    protected originalWidth: number;
    private resizeListener: any;
    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        this.$thisNode = $(TabFolder.TemplateNormal).attr("id", this.id);
        let tmpTab = this.$thisNode.find(".adore_tabs");
        this.$tabs = tmpTab.attr("id", this.id + "_tabs");
        this.$attachNode = this.$thisNode;
        let tabFolderId = this.id + "_tabs";
        this.$tabs.find("li.tab_left_move").on("click", function (e) {
            TabUtil.handleTabLeftMoveClick(tabFolderId);
        });
        this.$tabs.find("li.tab_right_move").on("click", function (e) {
            TabUtil.handleTabRightMoveClick(tabFolderId);
        });
        this.originalWidth = window.innerWidth;
        let own = this;
        this.resizeListener = function () {
            if (window.innerWidth !== own.originalWidth) {
                own.originalWidth = window.innerWidth;
                // refresh
                let tabs = $("#" + tabFolderId);
                if (tabs.find("li.adore_tab").length !== 0) {
                    //tabs.tabs();
                    TabUtil.tabsSwitch(tabs);
                    TabUtil.refreshTabs(tabs);
                }
            }
        };
        $(window).on("resize", this.resizeListener);
    }


    public addChild(child: Widget) {
        this.children.push(child);
    }

    public layout() {
        // super.layout();
        if (!document.getElementById(this.id)) {
            this.$thisNode.appendTo(this.parent.$attachNode);
        }
    }

    public dispose() {
        $(window).off("resize", this.resizeListener);
        //TODO resize_listener在TabUtil.ts中初始化，代码有点恶心，需优化
        let listener = this.$tabs.data("resize_listener");
        if (listener) {
            $(window).off("resize", listener);
        }
        super.dispose();
    }

}