#ifndef UMD_and_Tally_INCLUDED
	#define UMD_and_Tally_INCLUDED

#include <bncs_script_helper.h>

#ifdef WIN32
	#ifdef DOEXPORT_SCRIPT
		#define EXPORT_SCRIPT __declspec(dllexport) 
	#else
		#define EXPORT_SCRIPT 
	#endif
#else 
	#define EXPORT_SCRIPT
#endif
 
class UMD_and_Tally : public bncs_script_helper
{
public:
	UMD_and_Tally( bncs_client_callback * parent, const char* path );
	virtual ~UMD_and_Tally();

	void buttonCallback( buttonNotify *b );
	int revertiveCallback( revertiveNotify * r );
	void databaseCallback( revertiveNotify * r );
	bncs_string parentCallback( parentNotify *p );
	void timerCallback( int );
	
private:
	
// Some defintions needed by both the header file and the .cpp file
#define SDI_MTX_SIZE	128		// Note that arrays must be defined larger than this if we want to use BNCS numbering 1 to 128.
#define AUDIO_MTX_SIZE	64		// CTA Stereo router size
#define NUM_MIX_INPUT	36		// The current Sony has 32 SDI inputs, with 24 pre-wired to the router outputs.
#define CCU_ALLOC_1		11		// First slot in Tally Infodriver (hereafter TID) that holds CCU to studio allocation code.
#define MTX_TO_MIX_1	101		// TID slot number that has data needed by table RTRtoMIX. NUM_MIX_INPUT entries.
#define MTX_TO_MIX_LAST	(MTX_TO_MIX_1 + NUM_MIX_INPUT -1)	// TID slot number that holds the last slot of data needed by RTRtoMIX
#define STA_NAME_MODE	200		// TID slot number that holds the studio A UMD mode. "0" means normal name display, "1" means show a UMD address "MyID=001" etc
#define STA_UMD_BRIGHT	201		// TID slot number that holds a value to indicate the Studio A UMD brightness. 0=off, 1=one seventh, 2=one half, 3=full
#define STA_NAME_SET	202		// TID slot number that holds the currently active name set. 0 means use matrix short names, 1 to 8 are user name sets
#define CTA_NAME_MODE	205		// TID slot number that holds the CTA UMD mode. "0" means normal name display, "1" means show a UMD address "MyID=001" etc
#define CTA_UMD_BRIGHT	206		// TID slot number that holds a value to indicate the CTA UMD brightness. 0=off, 1=one seventh, 2=one half, 3=full
#define STA_UMD_ADDR	301		// TID first slot number that holds the Studio A UMD address for each matrix destination. 0=unused.
#define CTA_UMD_ADDR	501		// TID first slot number that holds the CTA UMD address for each video matrix destination. 0=unused.
#define CTA_UMD_ADDR_AUD 631	// TID first slot number that holds the CTA UMD address for each audio matrix destination. 0=unused.
#define STA_KEY_ASSOC	701		// TID first slot number that holds the Key association master source for each SDI source.
#define TALLY_END		(STA_KEY_ASSOC+SDI_MTX_SIZE+1)	// End of the tally data in the tally infodriver
#define NUM_CCUS		10		// The maximum numbers of CCUs that can feed signals to Studio A
#define ALLOC_A			1		// Value in Tally Infodriver and table CCU_Alloc that indicates CCU is allocted to Studio A for control functions

	int cta_sdi_id;				// Holds the BNCS driver id for CTA SDI router
	int cta_audio_id;			// Holds the BNCS driver id for CTA audio router
	int tally_info_id;			// Holds the BNCS driver id for the "Tally" infodriver
	int sta_tsl_id;				// Holds the BNCS driver id for the Studio A TSL UMD chain
	int cta_tsl_id;				// Holds the BNCS driver id for the CTA and Edit 6 TSL UMD chain
	int sta_mixer_id;			// Holds the BNCS driver id for the Studio A mixer
	int sta_names_id;			// Holds the BNCS device id for the user names. Database 9 has the names for databases 1 to 8
	int tally_gpi_id;			// GPI driver for camera tally
	int primary_offset;			// Contact number of first primary tally GPI out relay

	int revert_count;			// Used during the initial revertive processing.
	int sta_umd_mode;			// Studio A UMD operating mode
	int sta_umd_brightness;		// Studio A UMD brightness
	int sta_umd_active_names;	// Studio A active name set
	int cta_umd_mode;
	int cta_umd_bright;
	int StudioA_TX_Status;		// Holds the current off/Rehearse/Tx status.
	int id_count;				// Used by the StA UMD identifier mode.
	int id_count_cta;			// Used by the CTA UMD identifier mode
	int src_id1;				// Used by function buildTable_ST()
	int src_id2;				// Used by function buildTable_ST()
	int tally_src;				// Used in function runningRevertives
	int tally_val;				// Used in function runningRevertives
	
	// Look up tables needed for tally processing
	int RTRtoMIX[NUM_MIX_INPUT + 1];		// Holds the matrix destinations that feed the mixer inputs.
	int Mixer_In[SDI_MTX_SIZE + 1];			// Holds the mixer input fed by destination.
	int Table_A[NUM_MIX_INPUT + 1];			// Current sources applied to vision mixer inputs.
	int Table_B[NUM_MIX_INPUT + 1];			// Copy of the mixer tally list.
	int Table_C[SDI_MTX_SIZE + 1];			// List holding the current SDI matrix source for each destination.
	int Table_D[SDI_MTX_SIZE + 1];			// Lists the UMD addresses used by the Studio A UMD chain.
	int Table_E[SDI_MTX_SIZE + 1];			// Holds the key association look up table.
	int Table_D_CTA[SDI_MTX_SIZE + 1];		// Holds the UMD addresses used by the CTA/Edit 6 displays for the video element.
	int Table_G[SDI_MTX_SIZE + 1];			// Holds the CCU source to CCU allocation.
	int AudioStatus[AUDIO_MTX_SIZE + 1];	// Holds the audio router status.
	int Table_Aud[AUDIO_MTX_SIZE + 1];		// Holds CTA Audio UMD address per destination.
	int CCU_Alloc[NUM_CCUS + 1];			// Holds the CCU to Studio Allocation. Value 1 means allocated to Studio A.
	bool Table_ST[SDI_MTX_SIZE + 1];		// Holds the computed list of flags to show if this source is on-air.

	bncs_string Table_F[SDI_MTX_SIZE + 1];	// Holds a list of UMD's displaying each destination of the matrix

	bncs_string Mtx_Name[SDI_MTX_SIZE + 1];			// List of the short matrix names
	bncs_string UserNames_1[SDI_MTX_SIZE + 1];		// List of the User Names, set 1
	bncs_string UserNames_2[SDI_MTX_SIZE + 1];		// List of the User Names, set 2
	bncs_string UserNames_3[SDI_MTX_SIZE + 1];		// List of the User Names, set 3
	bncs_string UserNames_4[SDI_MTX_SIZE + 1];		// List of the User Names, set 4
	bncs_string UserNames_5[SDI_MTX_SIZE + 1];		// List of the User Names, set 5
	bncs_string UserNames_6[SDI_MTX_SIZE + 1];		// List of the User Names, set 6
	bncs_string UserNames_7[SDI_MTX_SIZE + 1];		// List of the User Names, set 7
	bncs_string UserNames_8[SDI_MTX_SIZE + 1];		// List of the User Names, set 8
	
	bncs_stringlist ccuList;				// Holds the list of CCU source id's
	bncs_stringlist umd_addr;				// Used when sub-dividing comma delimited lists
	
	bncs_string *CurrentName;				// Pointer to the current name set
	bncs_string audio_src_name;				// Used in Audio router revertive processing
	bncs_string *dbpointer;					// Used in database update processing

	bool drivers_registered;				// Used by destructor
	bool init_polls;						// Set true during initial data loading process
	bool sdi_poll[SDI_MTX_SIZE + 1];		// Used to check that we have received all revertives from the initial video router poll
	bool audio_poll[AUDIO_MTX_SIZE + 1];	// Used to check that we have received all revertives from the initial audio router poll
	bool tally_poll[TALLY_END];				// Used to check that we have received all revertives from the initial tally infodriver poll
	bool mixer_poll[NUM_MIX_INPUT + 1];		// Used during the initial data gathering stage.
	bool umd_sta_used[SDI_MTX_SIZE + 1];	// Used during UMD addr mode processing of Studio A chain
	bool umd_cta_used[SDI_MTX_SIZE + 1];	// Used during UMD addr mode processing of CTA chain
	bool timeout_active;
	bool sdi_allpolled;						// When true, signals that revertives have been received for all SDI router destinations
	bool audio_allpolled;					// When true, signals that revertives have been received for all audio router destinations
	bool mixer_allpolled;					// When true, signals that tally state revertives have been received for all mixer inputs
	bool tally_allpolled;					// When true, signals that revertives have been received for all relevant tally infodriver slots
	bool umd_id_active;						// Set true when Studio A UMD chain is showing the address ID strings
	bool cta_umd_id_active;					// Set true when CTA/Edit 6 UMD chain is showing the address ID strings

	bncs_config myVals;						// Used during reading of values from wn_academy.xml

	bncs_string m_myParam;					// Used by parent callback functions
	bncs_string m_instance;

	// Local methods
	void initialRevertives(revertiveNotify *r);
	void runningRevertives(revertiveNotify *r);
	void pollall_tally_infodriver(void);
	void buildTable_A(void);			// Builds Table_A from the matrix state and table RTRtoMIX.
	void buildTable_F(void);			// Builds the table indexed by source that lists the UMD's showing the source name. 
	void buildTable_G(void);			// Builds the table that holds the CCU number as a function of the source index.
	void buildTable_ST(void);			// Builds the Source Tally table using data from table B.
	void updateAll(void);				// Used to do a full update of all tally and UMD data.
	void updateStAbrightnessMimic(void);// Updates the panel display of UMD brighness setting for Studio A.
	void updateNameSetMimic(void);		// Update mimic display of current name set in use;
	void updateUMDnames(void);			// Update studio A UMD chain and mimics with current names
	void setUMDNameSet(void);			// Select the UMD name set to be used for Studio A.
	void updateCTAbrightnessMimic(void);// Updates the panel display of UMD brighness setting for CTA/Edit 6.
	void updateCTAUMDnames(void);		// Update studio A UMD chain and mimics with current names
};


#endif // UMD_and_Tally_INCLUDED