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

            _addItems: function (attrs) {
                var items = attrs.items;
                if (!items || items.length === 0) {
                    return;
                }
                for (var i = 0; i < items.length; i++) {
                    if (items[i].id) {
                        if (this.jqueryNode.find("#" + items[i].id).length !== 0) {
                            this.setItem(items[i]);
                        } else {
                            this.addItem(items[i]);
                        }
                    } else {
                        this.addItem(items[i]);//沒有id属性的直接添
                    }
                }
            },

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


            /**
             * @item{id:"",imagePath:""}
             */
            addItem: function (item) {
                var tmpID = item.id ? item.id : "noID";//TODO 以image名称作为id
                var itemNode = $(this.templates[1]).attr({id: tmpID});
                itemNode.appendTo(this.jqueryNode);
                this.setItemAttr(itemNode, item);
                var own = this;
                // 添加click
                itemNode.find("img").on("click", function (e) {
                    var index = own.indexOf(itemNode[0]);
                    var itemID = itemNode.attr("id");
                    EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET, {
                        id: own.id,
                        evtType: EventHub.CLICK,
                        index: index,
                        itemID: itemID
                    });
                });
            },

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

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

            /**
             * @item{id:"",imagePath:""}
             */
            setItemAttr: function (itemNode, item) {
                itemNode.find("img").attr("src", item.imagePath);
            },

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

            _clear: function () {
                this.jqueryNode.find("div.adore-picture-list-img").remove();
            }
        };
    });
});
