#ifndef ALARMMGR_H
#define ALARMMGR_H

#include "supplier.h"
#undef _WINDOWS_
#include "lrseventsC.h"
#include "types.h"
#include "alarm.h"
#include "file.h"
#include "ostask.h"
#include <list>

using namespace std ;

typedef list<Alarm*> ALARMLIST;
typedef ALARMLIST::iterator ALARM_POSITION;

/*--------------------------------------------------------
CLASS 
	AlarmManager

FILENAME
 	alarm.h

DESCRIPTION	
	AlarmManager manages alarm logging. It maintains two singly linked-list
	of Alarm objects; an active and inactive alarm list. Alarms are
	logged when they become active.

AUTHOR
	Danelle Cline. Copyright 1998 MBARI.				
LOG
	Modifier 	Date    		Change
	--------   	----    		--------

--------------------------------------------------------*/

class AlarmManager : public TaskBase
{
protected:
	
	// Constructor.  Opens default alarm log file for writing, and 
	// write header
	AlarmManager();	
	
public:		

	// Returns AlarmManager singleton instance
	static AlarmManager *instance();
	
	// Destructor
	~AlarmManager();

	// Runs alarm manager event loop. Checks for active alarms and promotes
	// or demotes them to an active or inactive lsit
	void *run();

	// Wait for thread running in this task to exit up to <milliSeconds>.
	Int32 wait (Nat32 milliSeconds);

	// For debugging. Dumps active and inactive list to stdout
	void dump();

	// Close the AlarmManager task. This will stop the active task
	// from running
	int close();

	// Create log file with <filename>
	// Returns TRUE if able to create file with filename, else
	// returns FALSE
	MBool createLogFile(char *filename);

	// Return number of active alarms = number of alarms that have
	// been tripped at least once
	Int16 getNumActiveAlarms();

	// If called successively, returns active alarms text from active linked list from
	// head to tail. Returns the newest alarms first.
	const char *getNextActiveAlarmText();

	// If called successively, returns active alarms in linked list from
	// head to tail
	Alarm* getNextActiveAlarm();

	// Write <msg> to alarm log file
	void writeMsg(const char *msg);	

	// Return pointer to log file name
	const char* getLogFileName();			

	// Insert alarm into inactive alarms linked list
	void insert(Alarm *pAlarm);

	// Initializes the log file
	void initialize(Supplier *supplier);

private:
	// AlarmManager singleton instance
	static AlarmManager *_pInstance;

	// Format for timestamp of alarm log entries
	static TimeFormat _timeFormat;

	// Update trip count of alarm and log to file if alarm 
	// logic true
	void update(Alarm *alarm);

	// Move <pAlarm> from active to inactive list
	void demoteToInactive(Alarm *pAlarm);

	// Move <pAlarm> from inactive to active alarm list
	void promoteToActive(Alarm *pAlarm);

	void writeTimeStamp();

	// Flag to close the AlarmManager task run() method
	Boolean _shutdown;

	FILEINFO _alarmLog;			
	ALARM_POSITION findAlarm(Alarm *pAlarm);	
	
	ALARMLIST _inactiveAlarmList;
	ALARMLIST _activeAlarmList;	
	ALARM_POSITION _nextActivePos;	
	BinarySemaphore _lock;
	BinarySemaphore _shutdownComplete;

	// CORBA event supplier and message to send as an event
	Supplier *_pSupplier;
	LRS::Message _messageEvent;
};

#endif
