/**
 * @file
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is part of The iWear Framework.
 * In particular this file is part of the Framework Core 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/basemodulemanager.h>
#include <iwear/eventdispatcher.h>
#include <iwear/i18n.h>
#include <iwear/moloader.h>
#include <iwear/configuration.h>
#include <iwear/servicemanager.h>

#include <iwsens/sensormanager.h>
#include <iwsens/sensorhandler.h>
#include <iwsens/sensorsurveillance.h>
#include <iwsens/timesensor.h>
#include <iwsens/loadsensor.h>


//#include <iwear-ser_sensor/lightsensor.h>

#include <iwpower/powermanager.h>

#include <iwear-context/contextmanager.h>
#include <iwear-context/testcontextlistener.h>
#include <iwear-context/contextlistenerrecurringappointment.h>

#include <iwear-context/contextxmlparser.h>


#include <iomanip>

using namespace std;
using namespace iwear;
using namespace iwear::sensor;
using namespace iwear::sensor::power;
//using namespace iwear::sensor::ser_sensor;
using namespace iwear::context;

int main ( void )
{
    MoLoader mo1("iwear_core");
    MoLoader mo2("iwear_context");
    MoLoader mo3("iwsens");
    i18n::add_loader(mo1);
    i18n::add_loader(mo2);
    i18n::add_loader(mo3);

    try
    {
	i18n::set_language(iwear_locale);
    }
    catch(const exception& e)
    {
	d_err << "Set Language Exception : " << e.what() << endl;
    }

    d_err.set_debuglevel(nonsens);
    //d_err.set_debuglevel(debug);
    //d_err.set_debuglevel(info);
    d_dbg << setprecision(15);
    d_nons << setprecision(15);

    d_inf << ANSI_BLUE << "Testing the ContextManager" << ANSI_NORMAL << endl;
    
    try {
	Configuration conf;
	ServiceManager service_manager (conf);
	EventDispatcher ed(&service_manager);
	ed.Init();
	ed.Start();

	BaseModuleManager mm(conf);   // The Master Main Base Global Manager
	SensorManager sm(&mm, conf);  // The Sensor Manager, which is master to location and sensorhandler
	SensorHandler sh(&sm, conf);  // The Sensor Handler which has direct sensors under its control
	PowerManager pm(&sm, conf);

	TimeSensor ts (&sh);
	LoadSensor ls (&sh);
//	LightSensor light_sensor(&sh, unit_lux, true, 30);
    	
	
	SensorSurveillance ssu(&service_manager, sm,ed);
	ssu.Init();
	ssu.Start();
	
	ContextManager cm(&sm);
	
	/* xml context test */
// 	ContextXMLParser context_xml_parser(&cm);
// 	list<ContextObject*>* contexts_from_file = context_xml_parser.load_contexts_from_file("xml/context.xml");
// 	string contextname0("after dusk");
// 	string contextname01("before dawn");
// 	string contextname1("Start Plena 29.10.");
// 	string contextname2("Stop Plena 29.10.");
	
// 	string light_in_pr("light in project room");

	/* 5 sec delay test */
	string contextname3("5 sec delay");
	SensorContext sc(contextname3, TIME, ts.get_value()+5, GREATER, unit_second, 0.0);
	cm.register_context(&sc);

	string contextname4("day");
	
	{
//	    TestContextListener tcl0(&cm, contextname0);
//	    TestContextListener tcl1(&cm, contextname1);
//	    TestContextListener tcl2(&cm, contextname2);
	    TestContextListener tcl3(&cm, contextname3);
//	    TestContextListener tcl4(&cm, contextname4);

//	    TestContextListener tcl_light_in_pr(&cm, light_in_pr);

	    DateTime d_tm(ts.get_value() +2);

	    ContextListenerRecurringAppointment clra(
		&cm,
		"termin",
		d_tm.get_time_s("%d.%m.%Y%%%H:%M"),
		2, // 2 sec length
		5, // 5 sec interval
		SECOND);

	    DateTime actual_time(ts.get_value());
	    d_dbg << i18n::trans("actual time is: ") << ts.get_value() << endl;
	    d_dbg << i18n::trans("actual time is: ") << actual_time.get_time_d() << endl;
	    d_dbg << i18n::trans("actual time is: ") 
		  << actual_time.get_time_s("%d.%m.%Y%%%H:%M") << endl;
	    
	    actual_time.set_time_d(ts.get_value());
	    d_dbg << i18n::trans("time before set is: ") << actual_time.get_time_d() << endl;
	    actual_time.set_time_d(actual_time.get_time_d());
	    d_dbg << i18n::trans("time after set is: ") << actual_time.get_time_d() << endl;


	    usleep(10000000); // 10 sec
	    
 	    cm.deregister_context(cm.get_context("termin"));
	    
	    usleep(10000000); // 3 sec
	    
// 	    d_inf << ANSI_BLUE << "test.cpp - Deregistred " << contextname3 << ANSI_NORMAL << endl;
	}
	
        d_inf << ANSI_BLUE << i18n::trans("Testing Done") << ANSI_NORMAL << endl;
// 	delete contexts_from_file;
    }
    catch( const exception& e ) {
        d_err << ANSI_RED << "Exception " << endl;
        d_err << e.what() << ANSI_NORMAL << endl;
    }


}
