#include <windows.h>
#include <stdio.h>
#include <bncs_string.h>
#include <bncs_config.h>
#include "LocalSDI.h"

#define PNL_MAIN	1
#define CAR_SDI_NAME "CAR SDI Router"

#define TIMER_SETUP	1

// ++++++++++++++++++++++++++++++++++++++++++++ //
// Make our class visible to the outside world. //
// ++++++++++++++++++++++++++++++++++++++++++++ //
EXPORT_BNCS_SCRIPT( LocalSDI )

// ++++++++++++++++++++++++++++++++++++++++++++ //
// Constructor - equivalent to ApplCore STARTUP //
// ++++++++++++++++++++++++++++++++++++++++++++ //
LocalSDI::LocalSDI( bncs_client_callback * parent, const char * path ) : bncs_script_helper( parent, path )
{
	// Ensure sensible defaults are set for the variables
	source_selected = false;
	dest_selected = false;
	source_index = 0;
	dest_index = 0;
	myLocalSDI = 0;
	car_router = 0;

	panelShow( PNL_MAIN, "p1.bncs_ui" );

	// Extract the classroom= parameter from the workstation file
	ws_sl = getWorkstationSetting("classroom").upper();

	// Read the various data from the global parameters file
	myVals = bncs_config("wn_academy." + ws_sl + ".sdirouter");			// Local SDI Router
	myLocalSDI = myVals.attr("value").toInt();

	myVals = bncs_config("wn_academy." + ws_sl + ".car_sd_os_first");	// First SD OS destination
	myFirstOS = myVals.attr("value").toInt();

	myVals = bncs_config("wn_academy." + ws_sl + ".car_sd_os_last");	// Last SD OS destination
	myLastOS = myVals.attr("value").toInt();

	// Find the number of the CAR SDI Router
	getDev(CAR_SDI_NAME, &car_router);

	// Set up the router parameters in the panel
	textPut("device", myLocalSDI, 1, 1);		// Identify the source device ID for control 1
	textPut("device.map", myLocalSDI, 1, 1);	// Identify the source map device. Database is set in p1_bncs.ui
	textPut("device", myLocalSDI, 1, 2);		// Identify destination device ID for control 2	

	routerRegister(car_router, myFirstOS, myLastOS);	// Register for revertives from CAR SD router
	routerPoll(car_router, myFirstOS, myLastOS);		// Now poll our destinations

}


// +++++++++++++++++++++++++++++++++++++++++++++ //
// Destructor - equivalent to ApplCore CLOSEDOWN //
// +++++++++++++++++++++++++++++++++++++++++++++ //
LocalSDI::~LocalSDI()
{
	routerUnregister(car_router);

}


// ++++++++++++++++++++++++++++++++++++++++++++++ //
// All button pushes and notifications come here. //
// ++++++++++++++++++++++++++++++++++++++++++++++ //
void LocalSDI::buttonCallback( buttonNotify *b )
{
	if (b->panel() == PNL_MAIN)
	{
		if (b->id() == 1)		// Is this the source grid button?
		{
			switch (b->value())
			{
				case 0:			// Source is de-selected
					source_selected = false;
					source_index = 0;
					break;

				default:		// A source has been selected
					source_selected = true;
					source_index = b->value().toInt();
					break;
			}

			// Check if we have both a source and destination selected. If so call the auto-take function
			if (source_selected && dest_selected) makeCrosspoint();
			
			return;				// Source Processing finished
		}

		if (b->id() == 2)		// Is this the destination grid button?
		{
			switch (b->value())
			{
				case 0:
					dest_selected = false;
					dest_index = 0;
					break;

				default:
					dest_selected = true;
					dest_index = b->value();
					break;
			}
			// Check if we have both a source and destination selected. If so call the auto-take function
			if (source_selected && dest_selected) makeCrosspoint();

			return;
		}

		if (b->id() == 3)		// Is this the "Clear Selection" button?
		{
			if (source_selected)
			{
				source_index = 0;
				source_selected = false;
				textPut("index", 0, PNL_MAIN, 1);
			}

			if (dest_selected)
			{
				dest_selected = false;
				dest_index = 0;
				textPut("selected", 0, PNL_MAIN, 2);
			}

			return;
		}
	}

}	// End of buttonCallback


// +++++++++++++++++++++++++ //
// All revertives come here. //
// +++++++++++++++++++++++++ //
int LocalSDI::revertiveCallback( revertiveNotify * r )
{

	if (r->device() == car_router)		// Check for CAR SDI router ID
	{
		textPut("text", r->sInfo(), PNL_MAIN, (11 + r->index() - myFirstOS));
	}

	return 0;
}

// all database name changes come back here
void LocalSDI::databaseCallback( revertiveNotify * r )
{
}

// all parent notifications come here i.e. when this script is just one 
//  component of another dialog then our host might want to tell us things
bncs_string LocalSDI::parentCallback( parentNotify *p )
{
	if( p->command() == "return" )
	{
		if( p->value() == "all" )
		{	// Persisting values for bncs_vis_ed
			bncs_stringlist sl;
			
			sl << bncs_string( "myParam=%1" ).arg( m_myParam );
			
			return sl.toString( '\n' );
		}

		else if( p->value() == "myParam" )
		{	// Specific value being asked for by a textGet
			return( bncs_string( "%1=%2" ).arg( p->value() ).arg( m_myParam ) );
		}

	}
	else if( p->command() == "instance" && p->value() != m_instance )
	{	// Our instance is being set/changed
		m_instance = p->value();
		//Do something instance-change related here
	}

	else if( p->command() == "myParam" )
	{	// Persisted value or 'Command' being set here
		m_myParam = p->value();
	}

	// ***** CONNECTIONS EVENTS HELPER LIST *****
	else if( p->command() == "_events" )
	{	// Helper-list of everything in this component generated by hostNotify's
		bncs_stringlist sl;

		sl << "notify=*";		
		
		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;
		
		sl << "myParam=[value]";
		
		return sl.toString( '\n' );
	}

	return "";
}

// timer events come here
void LocalSDI::timerCallback( int id )
{
	switch( id )
	{
	case 1:					// Autoclear selection timer
		timerStop(1);
		source_selected = false;			// Clear the selection of the source
		source_index = 0;
		textPut("index", 0, PNL_MAIN, 1);	
		dest_selected = false;				// Clear the selection of the destination
		dest_index = 0;
		textPut("selected", 0, PNL_MAIN, 2);
		break;

	default:	// Unhandled timer event
		timerStop(id);
		break;
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// Callbacks above - Methods below ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


// Routine to support making crosspoints when both source and destination are selected
void LocalSDI::makeCrosspoint(void)
{
	// Send the route crosspoint command to the drive
	routerCrosspoint(myLocalSDI, source_index, dest_index);

	// Start timer 1 that will cause the deselection of the sources when it times out
	timerStart(1, 300);

}
