/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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 { ADORE } from "ADORE";
import { EventHub } from "event/EventHub";
import { MessageType } from "event/MessageType";
import { Event } from "event/Event";
/// <reference path="../typings/jquery.d.ts" />
/**
 * Created by tangqianchong on 2016/11/23.
 */
export class Toast extends Widget {
    private static TemplateToast: string = `
    <div class="adore_toast">
        <span>我的任务</span>
        <span></span>
        <span></span>
        <div class="showButton waves-effect waves-light large">查看</div>
        <div class="closeButton waves-effect waves-light large mdi-content-clear"></div>
    </div>
    `;
    private toastId: string;
    private timeoutSign: number;

    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        this.$thisNode = $(Toast.TemplateToast).attr("id", this.id);
        this.buttonClickEventDispatch();
    }

    public setContent(args: { content: { toastName: string, receiveTime: string } }) {
        this.$thisNode.find("span").eq(1).html(args.content.toastName);
        this.$thisNode.find("span").eq(2).html(args.content.receiveTime);
    }

    public resetShowTime(args: { time: number }) {
        let own = this;
        //重置计时器
        if (own.timeoutSign) {
            clearTimeout(own.timeoutSign);
        }
        own.timeoutSign = setTimeout(function () {
            own.$thisNode.get(0).addEventListener("webkitAnimationEnd", function () {
                // own.$thisNode.remove();
                EventHub.dispatchEvent(MessageType.WIDGET, { id: own.id, evtType: Event.ON_CLOSE });
            });
            own.$thisNode.addClass("removing");
        }, args.time * 1000);
    }

    private buttonClickEventDispatch() {
        let own = this;
        let showButton: JQuery = this.$thisNode.find(".showButton");
        let closeButton: JQuery = this.$thisNode.find(".closeButton");

        //点击查看按钮事件
        showButton.on("click", function () {
            EventHub.dispatchEvent(MessageType.WIDGET, { id: own.id, evtType: Event.CLICK });
        });

        //点击关闭按钮事件
        closeButton.on("click", function () {
            own.$thisNode.get(0).addEventListener("webkitAnimationEnd", function () {
                // own.$thisNode.remove();
                EventHub.dispatchEvent(MessageType.WIDGET, { id: own.id, evtType: Event.ON_CLOSE });
            });
            own.$thisNode.addClass("removing");
        });
    }
}