#ifndef CueDirector_INCLUDED
	#define CueDirector_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
 
///////////////////////////////////////////////////////////////////////
// Some definitions needed by both the header file and the .cpp file //
///////////////////////////////////////////////////////////////////////
#define BIT_STORE_SIZE	8		// This module uses "bit" stores to count tally responses. As each storage element is 32 bits we
								// can test for up to 256 tally responses in one store.
#define BIT_SIZE		BIT_STORE_SIZE * 32
#define SDI_MTX_SIZE	128		// Note that data arrays must be defined larger than this if we want to use BNCS numbering 1 to 128.
#define NUM_CCUS		10		// The maximum numbers of CCUs that can feed signals to Studio A or C
#define CCU_ALLOC_1		11		// First slot in Tally Infodriver (hereafter TID) that holds CCU to studio allocation code.
#define CCU_ALLOC_10	(CCU_ALLOC_1 + NUM_CCUS - 1)	// Last slot for CCU allocation data
#define MTX_TO_MIX_25	181		// TID slot number that holds destination on StC router that feeds mixer input 25
#define MTX_TO_MIX_40	196		// TID slot number that holds destination on StC router that feeds mixer input 40
#define NUM_MIX_INPUT	40		// The Kahuna has 40 SDI inputs, with 24 pre-wired to the router outputs.
#define NUM_MIX_PATCHED	(MTX_TO_MIX_40 - MTX_TO_MIX_25 +1)		// Number of inputs associations stored in Tally Infodriver
#define STC_KEY_ASSOC	901		// TID first slot number that holds the Key association master source for each SDI source. NOT YET ACTIVE
#define TALLY_END		(STC_KEY_ASSOC+SDI_MTX_SIZE+1)		// End of the tally data in the tally infodriver
#define ALLOC_C			3		// Value in Tally Infodriver and table CCU_Alloc that indicates CCU is allocted to Studio C for control functions
#define MV01 27					// First matrix output that feeds the Multiviewer
#define MV48 74					// Last matrix output that feeds the Multiviewer 

class CueDirector : public bncs_script_helper
{
public:
	CueDirector( bncs_client_callback * parent, const char* path );
	virtual ~CueDirector();

	void buttonCallback( buttonNotify *b );
	int revertiveCallback( revertiveNotify * r );
	void databaseCallback( revertiveNotify * r );
	bncs_string parentCallback( parentNotify *p );
	void timerCallback( int );
	
private:
	int stc_sdi_id;				// Holds the BNCS driver ID for Studio C SDI router
	int cta_sdi_id;				// Holds the BNCS driver id for CTA SDI router
	int tally_info_id;			// Holds the BNCS driver id for the "Tally" infodriver
	int stc_mixer_id;			// Holds the BNCS driver id for the Studio C mixer tallies
	int stc_tsl_id;				// Holds the BNCS driver id for the Studio C TSL driver
	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 StudioC_TX_Status;		// Holds the current off/Rehearse/Tx status.
	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];		// 
	int Mixer_In[SDI_MTX_SIZE + 1];			// 
	int Table_A[SDI_MTX_SIZE + 1];			// Studio C SDI router map
	int Table_B[SDI_MTX_SIZE + 1];			// CTA to Studio C map
	int Table_C[SDI_MTX_SIZE + 1];			// CTA SDI router map
	int Table_D[SDI_MTX_SIZE + 1];			// Source to CCU lookup table
	int Table_E[NUM_MIX_INPUT + 1];			// Mixer Channel to CCU look up table
	int Table_KA[SDI_MTX_SIZE + 1];			// Key association values for Studio C
	int Table_M[NUM_MIX_INPUT + 1];			// Mixer Input Source map
	int Table_N[NUM_MIX_INPUT + 1];			// Mixer On-Air Tally State
	int CCU_Alloc[NUM_CCUS + 1];			// Holds the CCU to Studio Allocation. Value = 1 means allocated to Studio C.
	bool Table_ST[SDI_MTX_SIZE + 1];		// Source Tally list indexed by StC matrix in. slot is true when the source is on-air

	bncs_string Table_F[SDI_MTX_SIZE + 1];	// 

	bncs_stringlist ccuList;				// Holds list of CCU source id's
	bncs_stringlist stc_iml;				// Holds list of Studio C sdi router inputs fed by CTA SDI destination
	bncs_stringlist stcFromCta;				// Holds list of CTA destination id that feeds each stc_iml
	bncs_stringlist umd_addr;				// Used when sub-dividing comma delimited lists

	bool drivers_registered;				// Flag used by destructor to see if unregistering required.
	bool init_polls;						// Set true during initial data loading process

	UINT32 stc_sdi_poll[BIT_STORE_SIZE];	// Bit store used by initial tally of StC router
	UINT32 cta_sdi_poll[BIT_STORE_SIZE];	// Bit store used by initial tally of CTA SDI router
	UINT32 tally_poll[BIT_STORE_SIZE];		// Bit store used by initial tally of the "Tally InfoDriver"
	UINT32 mixer_poll[BIT_STORE_SIZE];		// Bit store used by initial poll for mixer on-air data
	bool timeout_active;
	bool stc_sdi_allpolled;					// When true, signals that revertives have been received for all Studio C SDI router destinations
	bool cta_sdi_allpolled;					// When true, signals that revertives have been received for all CTA SDI 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

	// Local methods
	void clearAllCheckBit(UINT32 *store);			// Clear all bits in bitstore "store" to 0
	void setCheckBit(int bit_ID, UINT32 *store);	// Support routine for the checking for poll responses. Sets bit bit_ID true in unsigned int array "store"
	int  countCheckBit(UINT32 *store);				// Returns the number of '1" bits in unsigned integer array "store"
	void initialRevertives(revertiveNotify *r);
	void runningRevertives(revertiveNotify *r);
	void pollall_tally_infodriver(void);
	void buildTable_E(void);				// Builds table that maps mixer input to CCU ID.
	void buildTable_F(void);				// Builds the table indexed by source that lists the UMD's showing the source name. 
	void buildTable_M(void);				// Builds the table of Matrix source ID as a function of mixer input number
	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 cameraTally(int mixCh);			// Updates the CameraTally for the mixer channel passed as a parameter
};


#endif // CueDirector_INCLUDED