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

    private static Template: string = `<div></div>`;

    private cssPaths: Array<string> = [];

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

    }

    public html(args: { html: string }) {
        if (args.html) {
            this.$thisNode.html(args.html);
        }
    }
    /**
     * htmlFragments:[{'fragmentID':'xxx','html':'*'},...]
     */
    public setHtmlFragments(args: { htmlFragments: Array<{ fragmentID: string, html: string }>, count: number }) {
        let htmls = args.htmlFragments;
        if (htmls && htmls.length > 0) {
            let tempArr = new Array();
            for (let i = 0; i < htmls.length; i++) {
                let fragmentNode = this.$thisNode.find("#" + htmls[i].fragmentID);
                if (fragmentNode.length > 0) {
                    fragmentNode.html(htmls[i].html);
                } else {
                    tempArr.push(htmls[i]);
                }
            }
            //最多尝试2次(若DOM还没准备好，tempArr就可能不为空，需要继续执行剩下的tempArr，下同)
            let count = args.count ? 0 : args.count;
            if (tempArr.length > 0 && count <= 2) {
                setTimeout(this.setHtmlFragments({ htmlFragments: tempArr, count: (count + 1) }), 200);
            }
        }
    }

    /**
     * images:[{'imageID':'xxx','imagePath':'*'},...]
     */
    public setImages(args: { images: Array<{ imageID: string, imagePath: string }>, count: number }) {
        let images = args.images;
        if (images && images.length > 0) {
            let tempArr = new Array();
            for (let i = 0; i < images.length; i++) {
                let imageNode = this.$thisNode.find("#" + images[i].imageID);
                if (imageNode.length > 0) {
                    imageNode.attr("src", images[i].imagePath);
                } else {
                    tempArr.push(images[i]);
                }
            }
            //最多尝试2次
            let count = args.count ? 0 : args.count;
            if (tempArr.length > 0 && count <= 2) {
                setTimeout(this.setImages({ images: tempArr, count: (count + 1) }), 200);
            }
        }
    }

    /**
     * texts:[{'textID':'xxx','text':'*'},...]
     */
    public setTexts(args: { texts: Array<{ textID: string, text: string }>, count: number }) {
        let texts = args.texts;
        if (texts && texts.length > 0) {
            let tempArr = new Array();
            for (let i = 0; i < texts.length; i++) {
                let textNode = this.$thisNode.find("#" + texts[i].textID);
                if (textNode.length > 0) {
                    textNode.text(texts[i].text);
                } else {
                    tempArr.push(texts[i]);
                }
            }
            //最多尝试2次
            let count = args.count ? 0 : args.count;
            if (tempArr.length > 0 && count <= 2) {
                setTimeout(this.setTexts({ texts: tempArr, count: (count + 1) }), 200);
            }
        }
    }

    /**
     * cssPaths:[{'cssID':'xxx','cssPath':'*.css'},...]
     */
    public setCssPaths(args: { cssPaths: Array<{ cssID: string, cssPath: string }> }) {
        let cssPaths = args.cssPaths;
        if (cssPaths && cssPaths.length > 0) {
            let $headNode = $("head");
            for (let i = 0; i < cssPaths.length; i++) {
                if ($headNode.find("#" + cssPaths[i].cssID).length === 0) {
                    $("<link href='" + cssPaths[i].cssPath + "' type='text/css' rel='stylesheet'>").attr("id", cssPaths[i].cssID).appendTo($headNode);
                    this.cssPaths.push(cssPaths[i].cssID);
                }
            }

        }
    }

    public dispose() {
        super.dispose();
        let $headNode = $("head");
        for (let i = 0; i < this.cssPaths.length; i++) {
            $headNode.find("#" + this.cssPaths[i]).remove();
        }
        this.cssPaths.splice(0, this.cssPaths.length); //删除
    }

}