#include <windows.h>
#include <stdio.h>
#include <bncs_string.h>
#include <bncs_config.h>
#include "CAR_HD_Mon.h"

#define MAIN_PANEL	1
#define MAIN_PANEL_NAME "CAR_HD_Mon.bncs_ui"
#define MONITOR_DEST 64								// Destination that has the monitoring
#define TIMER_BUTTON 1
#define TIMER_BUTTON_DURATION 200

// Make our class visible to the outside world
EXPORT_BNCS_SCRIPT(CAR_HD_Mon)

// ++++++++++++++++++++++++++++++++++++++++++++
// Constructor - equivalent to ApplCore STARTUP
// ++++++++++++++++++++++++++++++++++++++++++++
CAR_HD_Mon::CAR_HD_Mon(bncs_client_callback *parent, const char *path ) : bncs_script_helper(parent, path)
{
	// Initialise variables to safe values
	video_id = 0;

	// Read the BNCS driver ID from instances.xml
	getDev("CAR HD-SDI Router", &video_id);

	panelShow(MAIN_PANEL, MAIN_PANEL_NAME);

	routerRegister(video_id, MONITOR_DEST, MONITOR_DEST);
	routerPoll(video_id, MONITOR_DEST, MONITOR_DEST);
}

// Destructor - equivalent to ApplCore CLOSEDOWN
CAR_HD_Mon::~CAR_HD_Mon()
{
	routerUnregister(video_id);
}

// all button pushes and notifications come here
void CAR_HD_Mon::buttonCallback(buttonNotify *b)
{
	if (b->panel() == MAIN_PANEL)
	{
		if (b->id() == "Sources")
		{
			if (b->command() == "index")
			{
				if (b->value().toInt() > 0)
				{
					routerCrosspoint(video_id, b->value().toInt(), MONITOR_DEST);
					timerStart(TIMER_BUTTON, TIMER_BUTTON_DURATION);
				}
			}
		}
	}	// End if (b->panel() == MAIN_PANEL)
}


// +++++++++++++++++++++++++
// All revertives come here.
// +++++++++++++++++++++++++
int CAR_HD_Mon::revertiveCallback(revertiveNotify *r)
{
	if (r->device() == video_id)
	{
		if (r->index() == MONITOR_DEST)	// Hyper defensive programming!
		{
			textPut("text", r->sInfo(), MAIN_PANEL, "Mon_TALLY");
		}
	}

	return 0;
}

// all database name changes come back here
void CAR_HD_Mon::databaseCallback(revertiveNotify *r)
{
}


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// All parent notifications come here. For example when this script
// is just one component of another dialog then our host might want
// to tell us things.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bncs_string CAR_HD_Mon::parentCallback(parentNotify *p)
{
	if(p->command() == "return")
	{
		if(p->value() == "all")
		{	// Persisting values for bncs_vis_ed
			bncs_stringlist sl = "";
			return sl.toString( '\n' );
		}
	}

	// ***** CONNECTIONS EVENTS HELPER LIST *****
	else if(p->command() == "_events")
	{	// Helper-list of everything in this component generated by hostNotify's
		bncs_stringlist sl = "";
		return sl.toString( '\n' );
	}

	// ***** CONNECTIONS COMMANDS HELPER LIST *****
	else if(p->command() == "_commands")
	{	// Helper-list of any commands/parameters you might want to set at run-time
		bncs_stringlist sl = "" ;
		return sl.toString( '\n' );
	}

	return "";
}

// timer events come here
void CAR_HD_Mon::timerCallback( int id )
{
	switch( id )
	{
	case TIMER_BUTTON:
		timerStop(TIMER_BUTTON);						// Stop the timer
		textPut("select=-1", MAIN_PANEL, "Sources");	// Clear the sources pad selection
		break;

	default:	// Unhandled timer event
		timerStop(id);
		break;
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// Callbacks above - Methods below ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


