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

export class PictureList extends Widget {

    private static Template0 = ` <div class="row"></div>`;
    private static Template1 = `<div class="adore-picture-list-img col s4 m3 l2 waves-effect"><img class="responsive-img"></div>`;


    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        this.$thisNode = $(PictureList.Template0).attr({ id: id });
    }

    public addItems(args: { items: { id: string, imagePath: string }[] }) {
        let items = args.items;
        if (!items || items.length === 0) {
            return;
        }
        let $father = this.$thisNode.parent();
        if ($father.length !== 0) {//回调函数调用addItems,初始化是调用addItems,$father不存在
            this.$thisNode.detach();
        }
        for (let i = 0; i < items.length; i++) {
            if (items[i].id) {
                if (this.$thisNode.find("#" + items[i].id).length !== 0) {
                    this.setItem(items[i]);
                } else {
                    this.addItem(items[i]);
                }
            } else {
                this.addItem(items[i]); //沒有id属性的直接添
            }
        }
        if ($father.length !== 0) {//回调函数调用addItems
            this.$thisNode.appendTo($father);
        }
    }

    public setItems(args: { items: { id: string, imagePath: string }[] }) {
        let items = args.items;
        this.clear(); //先刪除所有
        this.addItems({ items: items });
    }

    private addItem(args: { id: string, imagePath: string }) {
        let itemNode: JQuery = args.id ? $(PictureList.Template1).attr({ id: args.id }) : $(PictureList.Template1);
        itemNode.appendTo(this.$thisNode);
        this.setItemAttr(itemNode, args);
        let own = this;
        // 添加click
        itemNode.find("img").on("click", function (e) {
            let index = own.indexOf(itemNode[0]);
            let itemID = itemNode.attr("id");
            EventHub.dispatchEvent(MessageType.WIDGET, {
                id: own.id,
                evtType: Event.CLICK,
                index: index,
                itemID: itemID
            });
        });
    }

    private indexOf(item: HTMLElement): number {
        let items = this.$thisNode.find("div.adore-picture-list-img");
        for (let i = 0; i < items.length; i++) {
            if (item === items[i]) {
                return i;
            }
        }
        return -1;
    }

    private setItem(args: { id: string, imagePath: string }) {
        let itemNode = this.$thisNode.find("#" + args.id);
        this.setItemAttr(itemNode, args);
    }

    private setItemAttr(itemNode: JQuery, args: { id: string, imagePath: string }) {
        itemNode.find("img").attr("src", args.imagePath);
    }

    public removeItem(args: { itemIds: string[] }) {
        let ids = args.itemIds;
        if (ids && ids.length > 0) {
            for (let i = 0; i < ids.length; i++) {
                this.$thisNode.find("#" + ids[i]).remove();
            }
        }
    }

    public clear() {
        this.$thisNode.find("div.adore-picture-list-img").remove();
    }

    public setFocus() {

    }
}
