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 {WidgetManager} from "util/WidgetManager";
import {TabUtil} from "util/TabUtil";
import {TabFolder} from "widget/tabFolder/TabFolder";

export class TabItem extends Widget {

    private static TemplateNormal = `
       <li class="col adore_tab">
            <a class="waves-effect"></a>
       </li>`;
     private static TemplateClose=`
     <li class="tab col adore_tab" style="position:relative;">
        <i class="tab_icon_close"></i>
        <a class="waves-effect" style="position:absolute;"></a>
     </li>`;

    private $a: JQuery;
    private $i: JQuery;
    private relatedControlId: string;
    private isLayout: boolean;
    private isSelected: boolean;

    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        if(style==ADORE.Close){
            this.$thisNode = $(TabItem.TemplateClose).attr("id", this.id);
        }
        else{
            this.$thisNode = $(TabItem.TemplateNormal).attr("id", this.id);
        }
        this.$a = this.$thisNode.find("a");
        this.$i = this.$thisNode.find("i");
        let tabId = this.parent.getId();
        this.isLayout = false;
        let own = this;
        let parentTabsId: string = tabId + "_tabs";
        this.$a.on("click", function (e: BaseJQueryEventObject) {
            $("#" + parentTabsId).find("a").removeClass("tab_selected").addClass("tab_not_selected");
            $(this).removeClass("tab_not_selected").addClass("tab_selected");
            TabUtil.handleTabClick(e, $(this), parentTabsId);
        });
        this.$i.on("click", function () {
            //console.log(own.id);
            EventHub.dispatchEvent(MessageType.WIDGET, {
                id: own.id,
                evtType: Event.ON_CLOSE
            });
        });
    }


    public setText(args: { text: string }) {
        let text = args.text;
        this.$a.text(text);
    }

    public setControl(args: { control: string }) {
        this.$a.attr("href", "#" + args.control);
        this.relatedControlId = args.control;
    }

    public append() {
        if (this.parent && this.parent instanceof TabFolder) {
            let tempParent = this.parent as TabFolder;
            let lis = tempParent.$tabs.find("li.adore_tab");
            if (lis.length === 0) {
                this.$thisNode.insertAfter(tempParent.$tabs.find("li.tab_left_move"));
            } else {
                this.$thisNode.insertAfter($(lis[lis.length - 1]));
            }
        } else {
            this.$thisNode.appendTo(document.body);
        }
    }

    public layout() {
        if (this.relatedControlId) {
            let widget = WidgetManager.byId(this.relatedControlId) as Widget;
            if (widget) {
                widget.$thisNode.appendTo(this.parent.$attachNode);
            }
        }
        this.append();
        this.isLayout = true;
        if (this.isSelected === true) {
            this.selected(this.$thisNode);
        } else {
            // refresh
            let tabItemNodes = (this.parent as TabFolder).$tabs.find(".adore_tab");
            if (tabItemNodes.length !== 0) {
                this.selected($(tabItemNodes[0]));
            }
        }
    }

    public dispose() {
        let tabs = (this.parent as TabFolder).$tabs;
        //找到当前TabItem的下标
        let lis = tabs.find("li.adore_tab");
        let currentIndex = -1;
        let i: number;
        for (i = 0; i < lis.length; i++) {
            if ($(lis[i]).find("a").attr("href") === this.$a.attr("href")) {
                $(lis[i]).find("a").removeClass("active");
                currentIndex = i;
                break;
            }
        }
        if (currentIndex >= 0 && this.parent && this.parent instanceof TabFolder) {
            if (tabs.find("a.active").length <= 0) {
                let showIndex: number;
                if (currentIndex <= 0) {
                    showIndex = currentIndex + 1;
                } else {
                    showIndex = currentIndex - 1;
                }
                if (showIndex >= 0 && showIndex < lis.length) {
                    //set active
                    let tmpA = $(lis[showIndex]).find("a");
                    tmpA.addClass("active")
                    //set div block
                    $(tmpA.attr("href")).removeClass("tab_control_not_visible").addClass("tab_control_visible");
                    // 添加选中色
                    tabs.find("a").removeClass("tab_selected").addClass("tab_not_selected");
                    tmpA.removeClass("tab_not_selected").addClass("tab_selected");
                }
            }
        }
        // dispose
        super.dispose();
        if (tabs.find(".adore_tab").length > 0) {
            // refresh
            //tabs.tabs();
            TabUtil.tabsSwitch(tabs);
            TabUtil.refreshTabs(tabs);
        } else {
            //若已经没有TabItem了，需要把indicator去掉
            tabs.find("div.my_indicator").remove();
        }
    }

    public setSelected() {
        this.isSelected = true;
        if (this.isLayout) {
            this.selected(this.$thisNode);
        }
    }

    public selected(selectTabItem: JQuery) {
        let tabs = (this.parent as TabFolder).$tabs;
        // 把当前已选中的tabItem取消选中
        let a_active = tabs.find("a.active");
        let activeControl = a_active.attr("href");
        $(activeControl).removeClass("tab_control_visible").addClass("tab_control_not_visible");
        a_active.removeClass("active");

        //选中当前tabItem
        let selectTabItemA = selectTabItem.find("a").addClass("active");
        let activeDiv = selectTabItemA.attr("href");
        $(activeDiv).removeClass("tab_control_not_visible").addClass("tab_control_visible");

        // 添加选中色
        tabs.find("a").removeClass("tab_selected").addClass("tab_not_selected");
        selectTabItemA.removeClass("tab_not_selected").addClass("tab_selected");

        // refresh
        // let t = tabs.find("ul.tabs");
        if (tabs.find(".adore_tab").length > 0) {
            //tabs.tabs();
            TabUtil.tabsSwitch(tabs);
            TabUtil.refreshTabs(tabs);
        }
    }

}