/**
 * File: atomicfactorysensor.cpp
 * Created by: <Joern Reimerdes>
 * Created on: 2004/10/13 18:00 
 * @file
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is a part of The iWear Framework.
 * In particular is this file a part of the Framework context Library
 *
 * The iWear Framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation as in version 2 of the License.

 * 
 * The iWear Framework 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 General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * The iWear Framework; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <iwear-context/atomicfactorysensor.h>

namespace iwear {
namespace context {

    /** Destroys the Factory and objects that where used in the factory.
     *
     */
    AtomicFactorySensor::~AtomicFactorySensor(){
	delete sensor_type_enum_map;
    }
    
    /** This method created an SensorContext from the DOMElement.
     *
     */
    ContextObject* 
    AtomicFactorySensor::create_atomic_context(string context_name, 
					       DOMElement* context_node){
	ContextObject* context_object;
	XMLHandler* xml_handler = this->context_xml_handler->get_xml_handler();
	DOMElement* sensor_node = 
	    xml_handler->get_first_element_by_name(context_node, 
						   NODE_SENSOR);
	/* get type */
	const string sensor_type_string = 
	    this->get_type_from_node(sensor_node);
	SensorType sensor_type = 
	    this->sensor_type_enum_map->find(sensor_type_string)->second;
	/* get comparison */
	Comparison comparison = this->get_comparison(sensor_node);
	/* get unit */
	units unit = this->get_compare_unit(sensor_node);
	/* get accuracy */
	double accuracy = this->get_accuracy_value(sensor_node);
	/* get comparevalue */
	double compare_value = this->get_compare_value(sensor_node); 
	/* create sensor context */
 	context_object = 
 	    /*dynamic_cast<ContextObject*>*/(new SensorContext(context_name,
 							   sensor_type,
 							   compare_value,
 							   comparison,
 							   unit,
 							   accuracy));
	return context_object;
    }
    
    
    /** Gets the compare value of the DOM comparison element.
     *
     */
    double AtomicFactorySensor::get_compare_value(DOMElement* sensor_node){
	const XMLCh* xml_value = 
	    sensor_node->getAttribute(ATTRIBUTE_COMPARE_VALUE);
	const char* vstr = XMLString::transcode(xml_value);
	string value_string = vstr;
	delete[] vstr;
	/* get type */
	const string sensor_type_string = 
	    this->get_type_from_node(sensor_node);
	SensorType sensor_type = 
	    this->sensor_type_enum_map->find(sensor_type_string)->second;
	/* create value depending on sensor type */
	switch(sensor_type) {
 	case TIME: 
 	    {
       		DateTime date;
		const XMLCh* xml_unit = 
		    sensor_node->getAttribute(ATTRIBUTE_UNIT);
		const char* unit_string = XMLString::transcode(xml_unit);
		date.set_time_s(value_string.c_str(), unit_string);
		delete[] unit_string;
		return date.get_time_d();
		break;
	    }
	default: 
	    {
		return atof(value_string.c_str());
		break;
	    }
	} // sensor_type switch
	return 0.0;
    }

    /** Gets the unit of the DOM sensor node value.
     *
     */
    units AtomicFactorySensor::get_compare_unit(DOMElement* sensor_node){
	const XMLCh* xml_unit = 
	    sensor_node->getAttribute(ATTRIBUTE_UNIT);
	const char* unit_string = XMLString::transcode(xml_unit);
	units unit = this->unit_map->find(unit_string)->second;
	delete[] unit_string;
	return unit;
    }

    /** Gets the accuracy value of the DOM  sensor node.
     *
     */
    double AtomicFactorySensor::get_accuracy_value(DOMElement* sensor_node){
	const XMLCh* xml_value = 
	    sensor_node->getAttribute(ATTRIBUTE_ACCURACY);
	const char* double_string = XMLString::transcode(xml_value);

	double double_value = atof(double_string);

	///@todo TODO if double_string == mooh then return 0.0
	delete[] double_string;
	return double_value;
    }

    /** Gets the comparison of the DOM sensor node.
     *
     */
    Comparison AtomicFactorySensor::get_comparison(DOMElement* sensor_node){
	const char * comparison_string = 
	    XMLString::transcode(sensor_node->getAttribute(ATTRIBUTE_COMPARISON));
	string st = comparison_string;
	delete[] comparison_string;
	return this->comparison_enum_map->find(st)->second;
    }

} // context
} // iwear

