/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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/7/14.
 */
define(["adoreapp/widget/BaseWidget"
    , "adoreapp/widget/constants/ADORE"
    , "adoreapp/widget/EventHub"], function (BaseWidget, Adore, EventHub) {
    return BaseWidget.extend(function (base) {
        return {
            _initWidget: function (attrs) {
                this.templates = attrs.template.split("<!-- split -->");
                this.jqueryNode = $(this.templates[0]).attr("id", this.id);
                this.attachNode = this.jqueryNode;
                this.style = attrs.style;
                this.itemTemplate = this.templates[1];
                this.isHorizontal = true;//默认是水平布局(左图片右文字)
                if (this.style & Adore.Vertical === Adore.Vertical) {
                    this.isHorizontal = false;
                    this.itemTemplate = this.templates[2];
                }
            },

            /**
             * @attrs {items:[{name:"",imagePath:"",msgNum:""},...]}//
             */
            _addItems: function (attrs) {
                var items = attrs.items;
                if (!items || items.length === 0) {
                    return;
                }
                for (var i = 0; i < items.length; i++) {
                    if (items[i].name) {
                        if (this.jqueryNode.find("#" + items[i].name).length !== 0) {
                            this.setItem(items[i]);
                        } else {
                            this.addItem(items[i]);
                        }
                    }
                }
                if (this.fontSize) {
                    this.jqueryNode.find("span").css({fontSize: attrs.fontSize});
                }
            },

            /**
             * @item{name:"",imagePath:"",msgNum:""}
             */
            setItem: function (item) {
                var itemNode = this.jqueryNode.find("#" + item.name);
                this.setItemAttr(itemNode, item);
            },

            /**
             * @item{name:"",imagePath:"",msgNum:""}
             */
            addItem: function (item) {
                var itemNode = $(this.itemTemplate).attr({id: item.name});
                itemNode.appendTo(this.jqueryNode);
                this.setItemAttr(itemNode, item);
                var own = this;
                // 添加click
                itemNode.find("a").on("click", function (e) {
                    //添加选中色
                    $("a.adore_list_li_a_selected").removeClass("adore_list_li_a_selected");
                    $(this).addClass("adore_list_li_a_selected");
                    //事件处理待完善
                    var itemIndex = own.indexOf(itemNode[0]);
                    e.preventDefault();
                    EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET, {
                        id: own.id,
                        selectIndex: itemIndex,
                        evtType: EventHub.ON_CHANGE
                    });
                });
            },

            /**
             * @item{name:"",imagePath:"",msgNum:""}
             */
            setItemAttr: function (itemNode, item) {
                itemNode.find("a").find("span").text(item.name);
                //设置图片
                if (item.imagePath) {
                    itemNode.find("img").attr("src", item.imagePath);
                    if (this.isHorizontal) {
                        itemNode.find("img").css({width: "18px", height: "18px"});
                    } else {
                        itemNode.find(".adore_list_span").css("margin-top", "-5px");
                        //itemNode.find("img").css({width: "40px", height: "40px"});
                    }
                }
                //设置消息提示
                if (item.msgNum) {
                    var msgNode = itemNode.find(".adore_list_message");
                    if (msgNode.length === 0) {
                        msgNode = $(this.templates[3]);
                        itemNode.prepend(msgNode);
                        if (this.isHorizontal){
                            msgNode.addClass("adore_list_message_horizontal");
                        }else{
                            msgNode.addClass("adore_list_message_vertical");
                        }
                    }
                    //msgNode.text(item.msgNum);
                } else {
                    itemNode.find(".adore_list_message").remove();
                }
            },

            _setItems: function (attrs) {
                var items = attrs.items;
                this._clear();//先刪除所有
                this._addItems({items: items});
            },

            _setFontSize: function (attrs) {
                this.fontSize = attrs.fontSize;
                if (this.jqueryNode) {
                    this.jqueryNode.find("span").css({fontSize: attrs.fontSize});
                }
            },

            indexOf: function (item) {
                var items = this.jqueryNode.find("li");
                for (var i = 0; i < items.length; i++) {
                    if (item === items[i]) {
                        return i;
                    }
                }
                return -1;
            },

            _select: function (attrs) {
                var index = attrs.index;
                var allA = this.jqueryNode.find("a");
                if (index < 0 || index >= allA.length) {
                    return;
                }
                $(allA[index]).trigger("click");
            },

            _removeItem: function (attrs) {
                var ids = attrs.itemNames;
                if (ids && ids.length > 0) {
                    for (var i = 0; i < ids.length; i++) {
                        this.jqueryNode.find("#" + ids[i]).remove();
                    }
                }
            },

            _clear: function () {
                this.jqueryNode.find("li").remove();
            }
        };
    });
});