/*
* 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.
*
*/

/**
 * Panel for component information.
 *
 * @author akipp
 */


package de.unibi.airobots.resaijavaclient.gui.panels;

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

import javax.swing.JTextArea;

import de.unibi.airobots.resaijavaclient.communicaton.Communicator;
import de.unibi.airobots.resaijavaclient.communicaton.XMPPCommunicator;
import de.unibi.airobots.resaijavaclient.communicaton.XMPPPacketListener;
import de.unibi.airobots.resaijavaclient.constants.Constants;
import de.unibi.airobots.resaijavaclient.gui.panels.components.template.PanelTemplate;

public class ComponentPanel extends PanelTemplate {

	private static final long serialVersionUID = 1L;
	private HashMap<String, String> components = new HashMap<String, String>();
	private XMPPCommunicator com;

	private JTextArea txaList = new JTextArea(10, 30);

	public ComponentPanel(Communicator communicator) {
		com = (XMPPCommunicator) communicator;
		XMPPPacketListener pl = new XMPPPacketListener();
		com.addListener(pl);
		pl.addObserver(this);
		
		add(txaList);

		Timer t = new Timer();
		t.schedule(new ComponentStateRequester(), 0, 2000);
	}

	private class ComponentStateRequester extends TimerTask {

		@Override
		public void run() {
			HashMap<String, String> propertiesToSend = new HashMap<String, String>();
			propertiesToSend.put(Constants.CMD, "requestComponentStates");
			propertiesToSend.put(Constants.TAG, Constants.COMPONENTDISPATCHER);
			com.sendMessage(com.getServerAccount(), propertiesToSend,
					"Calling ReSAI Server...");
		}

	}

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

			if (packLis.getReceivedProperties().containsKey(Constants.COMPONENT_STATES)) {

				for (String prop : packLis.getReceivedProperties().keySet()) {

					if (prop.endsWith("_STATE")) {
						String component = prop.substring(0, prop.indexOf("_"));
						components.put(component, packLis
								.getReceivedProperties().get(prop));
						buildList();
					}
				}
			}

		}
	}

	private void buildList() {
		synchronized (components) {
			if (components.size() > 0) {

				txaList.setText("");

				for (String comp : components.keySet()) {
					txaList.append(comp + "( " + components.get(comp) + ")\n");
				}
			}
		}
	}

}