/**
 * License Agreement.
 *
 * JBoss RichFaces - Ajax4jsf Component Library
 *
 * Copyright (C) 2007 Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */ 
package org.ajax4jsf.bean;

import java.util.Date;
import java.util.EventListener;
import java.util.EventObject;

import org.ajax4jsf.event.PushEventListener;

public class AjaxPushTestBean implements Runnable {

    private boolean enabled = false;

    private Date startDate;

    private PushEventListener listener;

    private Thread thread;

    private int eventsSent = 0;

    private String content = "content";

    /**
     * Gets value of content field.
     * @return value of content field
     */
    public String getContent() {
        return content;
    }

    /**
     * Set a new value for content field.
     * @param content a new value for content field
     */
    public void setContent(String content) {
        this.content = content;
    }

    /**
     * Gets value of eventsSent field.
     * @return value of eventsSent field
     */
    public int getEventsSent() {
        return eventsSent;
    }

    /**
     * Set a new value for eventsSent field.
     * @param eventsSent a new value for eventsSent field
     */
    public void setEventsSent(int eventsSent) {
        this.eventsSent = eventsSent;
    }

    public void addListener(EventListener listener) {
        synchronized (listener) {
            if (this.listener != listener) {
                this.listener = (PushEventListener) listener;
            }
        }
    }

    public void run() {
        while (thread != null) {
            try {
                long left = System.currentTimeMillis() - startDate.getTime();
                if (left >= 60000) {
                    stop();
                }
                listener.onEvent(new EventObject(this));
                eventsSent++;
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void start() {
        if (thread == null) {
            thread = new Thread(this);
            thread.setDaemon(true);
            thread.start();
            startDate = new Date();
            setEnabled(true);
        }
    }

    public void stop() {
        if (thread != null) {
            startDate = null;
            eventsSent = 0;
            setEnabled(false);
            thread = null;
        }
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void restart() {
        stop();
        start();
    }
}
