/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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 huangdeyun on 2015/3/3.
 */
define(["adoreapp/widget/WidgetManager"
    , "adoreapp/widget/BaseWidget"
    , "adoreapp/util/TabUtil"], function (WidgetManager, BaseWidget, TabUtil) {
    var $tab_min_width = 40;
    var movingIconWidth = 65;//两个移动图标占用的宽度
    return BaseWidget.extend(function (base) {
            return {
                _initWidget: function (attrs) {
                    this.jqueryNode = $(attrs.template).attr("id", this.id);
                    this.a = this.jqueryNode.find("a");
                    var tabId = this.father.id;
                    this.isLayout = false;
                    var parentId = this.parent + "_tabs";
                    this.a.on("click", function (e) {
                        $("#" + parentId).find("a").removeClass("tab_selected").addClass("tab_not_selected");
                        $(this).removeClass("tab_not_selected").addClass("tab_selected");
                        TabUtil.handleTabClick(e, $(this), parentId);
                    });
                },

                _setText: function (attrs) {
                    var text = attrs.text;
                    this.a.text(text);
                },

                _setControl: function (attrs) {
                    this.a.attr("href", "#" + attrs.control);
                },

                append: function () {
                    if (this.parent && this.father._tabs) {
                        var lis = this.father._tabs.find("li.adore_tab");
                        if (lis.length == 0) {
                            this.jqueryNode.insertAfter(this.father._tabs.find("li.tab_left_move"));
                        } else {
                            this.jqueryNode.insertAfter($(lis[lis.length - 1]));
                        }
                    } else {
                        this.jqueryNode.appendTo(document.body);
                    }
                },
                _layout: function () {
                    //var control = this.a.attr("href");
                    //$(control).css({position:"absolute",width:"100%",top:"48px",bottom:"0px",overflow:"auto"});
                    var layout = this.father ? this.father.layout : undefined;
                    if (layout && layout.toString() === "RowGridLayout" && this.layoutData) {
                        var column = this.layoutData.getColumn();
                        var offset = this.layoutData.getOffset();
                        var row = this.layoutData.getRow();
                        var style = column + " col " + offset;
                        this.jqueryNode.addClass(style);
                    }
                    this.append();
                    this.isLayout = true;
                    if (this.isSelected === true) {
                        this.selected(this.jqueryNode);
                    }else{
                        // refresh
                        var tabItemNodes = this.father._tabs.find(".adore_tab");
                        if(tabItemNodes.length!==0){
                            this.selected($(tabItemNodes[0]))
                        }
                    }
                },
                _dispose: function () {
                    var tabs = $("#" + this.parent + "_tabs");
                    //找到当前TabItem的下标
                    var lis = tabs.find("li.adore_tab");
                    var currentIndex = -1;
                    var i;
                    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.father._tabs) {
                        if (tabs.find("a.active").length <= 0) {
                            var showIndex;
                            if (currentIndex <= 0) {
                                showIndex = currentIndex + 1;
                            } else {
                                showIndex = currentIndex - 1;
                            }
                            if (showIndex >= 0 && showIndex < lis.length) {
                                //set active
                                var tmpA = $(lis[showIndex]).find("a");
                                tmpA.addClass("active")
                                //set div block
                                $(tmpA.attr("href")).css("display", "block");
                                // 添加选中色
                                tabs.find("a").removeClass("tab_selected").addClass("tab_not_selected");
                                tmpA.removeClass("tab_not_selected").addClass("tab_selected");
                            }
                        }
                    }
                    // dispose
                    base._dispose.call(this);
                    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();
                    }
                },

                _setSelected: function () {
                    this.isSelected = true;
                    if (this.isLayout === true) {
                        this.selected(this.jqueryNode);
                    }
                },
                selected: function (selectTabItem) {
                    var tabs = $("#" + this.parent + "_tabs");
                    // 把当前已选中的tabItem取消选中
                    var a_active = tabs.find("a.active");
                    var activeControl = a_active.attr("href");
                    $(activeControl).css("display", "none");
                    a_active.removeClass("active");

                    //选中当前tabItem
                    var selectTabItemA =  selectTabItem.find("a").addClass("active");
                    var activeDiv =  selectTabItemA.attr("href");
                    $(activeDiv).css("display", "block");

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

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