// *****************************************************************************
//  Copyright (C): Christian Nastasi, 2011                                    
//  Author(s): Christian Nastasi                                              
//  Developed at the:                                                         
//  - Retis LAB, TeCIP Institute, Scuola Superiore Sant'Anna (Pisa)           
//  - School of Elec Eng and Computer Science, Queen Mary University (London) 
//  This file is distributed under the terms in the attached LICENSE_2 file.  
//  If you do not find this file, copies can be found by writing to:          
//  - c.nastasi@sssup.it                                                      
//  - nastasichr@gmail.com                                                    
//  - andrea.cavallaro@eecs.qmul.ac.uk                                        
// *****************************************************************************

#ifndef __WiseCameraSimplePeriodicTracker_h__
#define __WiseCameraSimplePeriodicTracker_h__ 

#include <vector>
#include <map>
#include "WiseCameraApplication.h"
#include "WiseCameraDetectionsMessage_m.h"

class WiseCameraSimplePeriodicTracker : public WiseCameraApplication {
private:
	typedef enum {
		INVALID = 0,
		DETECTIONS,
	} sample_type_t;

	typedef enum {
		FSM_INIT = 0,
		FSM_WAIT_FIRST_SAMPLE,
		FSM_WAIT_END_FIRST_SAMPLE,
		FSM_WAIT_SAMPLE,
		FSM_WAIT_END_SAMPLE,
	} fsm_state_t; 

protected:
	double sampling_time_step;
	unsigned long tracking_step;
	std::vector<WiseTargetDetection> detections;

private:
	bool network_ready;
	fsm_state_t fsm_state;
	double sample_lifetime;
	double sampling_end2start_time;
	std::map<std::string, bool> connectivity_map;

protected:
	void startup();
	void finishSpecific();
	//void fromNetworkLayer(WiseApplicationPacket *, const char *, double, double);
	void fromNetworkLayer(cPacket *, const char *, double, double);
	void handleSensorReading(WiseCameraMessage *);
	void timerFiredCallback(int);
	void notifyNeighbourDiscovered();

	void send_message(WiseApplicationPacket*);
	void send_message(WiseApplicationPacket*, const std::string&);

	virtual void at_startup() = 0 ;
	virtual void at_timer_fired(int index) = 0;
	virtual void at_tracker_init() = 0;
	virtual void at_tracker_first_sample() = 0;
	virtual void at_tracker_end_first_sample() = 0;
	virtual void at_tracker_sample() = 0;
	virtual void at_tracker_end_sample() = 0;
	virtual void process_network_message(WiseApplicationPacket *m) = 0 ;
	virtual void make_measurements(const std::vector<WiseTargetDetection>&)
									   = 0 ;

private:
	void fsm_tracker(sample_type_t t, bool restart = false);
	void handle_detections(const WiseCameraDetectionsMessage *);
};

#endif // __WiseCameraSimplePeriodicTracker_h__	
