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

/**
 * Informationholder for parsing config and controlling
 * creation and communication of components.
 *
 * @author akipp
 */

package de.unibi.airobots.resaidroid;

import java.io.IOException;
import java.util.HashMap;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.res.Resources;
import android.util.Log;

public class TabComponentMap {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String TAG = "TabComponentMap";
	private Resources res;

	/**
	 * Map containing classes with Key Component and Value TabClass
	 */
	//TODO create own classes for maps?
	private HashMap<String, String> classes;
	private HashMap<String, String> tabName;
	private HashMap<String, Integer> tabPositions;

	/**
	 * Initialize classes map with several pairs of Component and TabClass
	 * 
	 * @param res
	 * 
	 * @param tabTemplate
	 */
	public TabComponentMap(Resources _res) {

		res = _res;

		classes = new HashMap<String, String>();
		tabName = new HashMap<String, String>();
		tabPositions = new HashMap<String, Integer>();
		// TODO ERROR HANDLING ON XML PARSE ERROR

		parseLogConfiguration();

	}

	public String getTabName(String _class) {
		if (tabName.get(_class) != null) {
			return tabName.get(_class);
		}
		return "UNKNOWN";
	}
	
	public String getTabNameByComponentName(String _component) {
		return tabName.get(classes.get(_component));
	}

	public int getTabPositionByThread(String _thread) {
		if (tabPositions.get(_thread) != null) {
			return tabPositions.get(_thread);
		}
		return -1;
	}
	
	/**
	 * Returns the TabClass for a given component
	 * 
	 * @param component
	 *            to find TabClass for
	 * @return name of TabClass
	 */
	public synchronized String getTabClass(String component) {
		try {
			return classes.get(component);
		} catch (Exception e) {
			// TODO: handle exception
			Log.i(TAG, "###EXCEPTION " + e.getMessage());
		}
		return null;
	}

	/**
	 * Returns the Component for a given TabClass
	 * 
	 * @param _class
	 *            to find Component for
	 * @return name of Component
	 */
	public String getComponent(String _class) {
		
		for (String component : classes.keySet()) {
			if (classes.get(component).equals(_class)) {
				return component;
			}
		}
		return null;
	}

	public void parseLogConfiguration() {

		XmlPullParser xpp = res.getXml(R.xml.serverconfig);
		String className = null, name = null, threadName = null;
		int position = 0;

		try {
			while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
				if (xpp.getEventType() == XmlPullParser.START_TAG) {
					if (xpp.getName().equals("Component")) {
						
						for (int i = 0; i < xpp.getAttributeCount(); i++) {
							if (xpp.getAttributeName(i).equals("class")) {
								className = xpp.getAttributeValue(i);
							}
							if (xpp.getAttributeName(i).equals("name")) {
								name = xpp.getAttributeValue(i);
							}
							if (xpp.getAttributeName(i).equals("thread")) {
								threadName = xpp.getAttributeValue(i);
							}
							if (xpp.getAttributeName(i).equals("position")) {
								position = Integer.parseInt(xpp.getAttributeValue(i));
							}
						}
						
						if (className != null && threadName != null
								&& name != null) {
								tabName.put(className, name);
								classes.put(threadName, className);
								tabPositions.put(threadName, position);
						} else {
							Log.i(TAG, "some element was null...");
						}

					}
				}

				xpp.next();
			}

		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}