/*
* This file is part of the Remote Sensor Actuator Interface (ReSAI).
*
* Copyright(c) Andreas Kipp, Frederic Siepmann
* http://opensource.cit-ec.de/projects/resai
*
* This file may be licensed under the terms of of the
* GNU Lesser General Public License Version 3 (the ``LGPL''),
* or (at your option) any later version.
*
* Software distributed under the License is distributed
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the LGPL for the specific language
* governing rights and limitations.
*
* You should have received a copy of the LGPL along with this
* program. If not, go to http://www.gnu.org/licenses/lgpl.html
* or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The development of this software was supported by the
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
* The Excellence Cluster EXC 277 is a grant of the Deutsche
* Forschungsgemeinschaft (DFG) in the context of the German
* Excellence Initiative.
*
*/

/**
 * Beacon class controlling connection to ReSAI server.
 *
 * @author akipp
 */

package de.unibi.airobots.resaijavaclient.communicaton;

import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JOptionPane;

import de.unibi.airobots.resaijavaclient.constants.Constants;

public class Beacon implements Observer {

	private XMPPCommunicator com;
	private int serverBeaconCount = 0;
	private Timer timer;

	public Beacon(Communicator communicator) {

		com = (XMPPCommunicator) communicator;
		XMPPPacketListener pl = new XMPPPacketListener();
		com.addListener(pl);
		pl.addObserver(this);

		timer = new Timer();
		timer.schedule(new Checker(), 0, 5000);
	}

	private class Checker extends TimerTask {

		@Override
		public void run() {

			if (serverBeaconCount > 5) {
				timer.cancel();
				JOptionPane.showConfirmDialog(null, "ReSAI Server not responding. Shutting down.","Error", JOptionPane.CLOSED_OPTION);
				System.exit(-1);
				
			}

			HashMap<String, String> propertiesToSend = new HashMap<String, String>();
			propertiesToSend.put(Constants.CMD, Constants.BEACON);
			propertiesToSend.put(Constants.CLIENT_TIME,
					String.valueOf(System.currentTimeMillis()));
			propertiesToSend.put(Constants.TAG, Constants.MAINDISPATCHER);
			com.sendMessage(com.getServerAccount(), propertiesToSend,
					"Calling ReSAI Server...");

			System.out.println("SENDING BEACON TO " + com.getServerAccount());
			for (String prop : propertiesToSend.keySet()) {
				System.out.println("SENDING: " + prop + " / " + propertiesToSend.get(prop));
			}
			
			serverBeaconCount++;
		}

	};

	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof XMPPPacketListener) {
			XMPPPacketListener pl = (XMPPPacketListener) o;

			HashMap<String, String> receivedProperties = pl
					.getReceivedProperties();

			if (receivedProperties.containsKey(Constants.CMD)) {
				if (receivedProperties.get(Constants.CMD).equals(Constants.BEACON)) {
					serverBeaconCount = 0;
				}
			}

		}
	}

}