#include <windows.h>
#include <stdio.h>
#include <bncs_string.h>
#include <bncs_config.h>
#include "UMDsend.h"

#define MAIN_PANEL_NAME	"umdsend.bncs_ui"
#define MAIN_PANEL_ID	1	

#define TIMER_SETUP	1

// Macro to make this class visible to the outside world
EXPORT_BNCS_SCRIPT( UMDsend )

// constructor - equivalent to ApplCore STARTUP
UMDsend::UMDsend( bncs_client_callback * parent, const char * path ) : bncs_script_helper( parent, path )
{
	// Initialise the global variables
	myText = "";
	infoDriver_id = 0;
	slot_id = 0;

	// Show a panel from file umdsend.bncs_ui and we'll know it as our panel MAIN_PANEL_ID
	panelShow(MAIN_PANEL_ID, MAIN_PANEL_NAME);
}

// Destructor - equivalent to ApplCore CLOSEDOWN
UMDsend::~UMDsend()
{
	// Unregister any revertive selections
	if ((infoDriver_id != 0) && (slot_id != 0))
	{
		infoUnregister(infoDriver_id);
		infoDriver_id = 0;
		slot_id = 0;
	}
}

// All button pushes and notifications come here
void UMDsend::buttonCallback( buttonNotify *b )
{
	// Only have displays on this component, so no buttons to process
}

// All revertives come here.
int UMDsend::revertiveCallback( revertiveNotify * r )
{
	if ((r->device() == infoDriver_id) && (r->index() == slot_id))
	{
		// UMD text has been updated by a user.
		textPut("text", r->sInfo(), MAIN_PANEL_ID, "umdText");
	}
	
	return 0;
}

// all database name changes come back here
void UMDsend::databaseCallback( revertiveNotify * r )
{
	// Not relevant to this component.
}

// 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 UMDsend::parentCallback( parentNotify *p )
{
	if (p->command() == "return")
	{
		if (p->value() == "all")
		{
			// Persisting data for visual editor
			bncs_stringlist sl = "";
			sl << bncs_string("InfoDriverID=%1").arg(infoDriver_id);
			sl << bncs_string("Slot=%1").arg(slot_id);
			return(sl.toString('\n'));
		}
	}

	else if( p->command() == "text" )
	{
		// We have received a new text item to send to the UMD
		myText = p->value();	// Transfer delivered text to local store

		// Send the text to the infodriver, provided the infodriver and slot are non-zero.
		if ((infoDriver_id != 0) && (slot_id != 0))
		{
			// The revertive will update the local display
			infoWrite(infoDriver_id, p->value(), slot_id, false);
		}
	}
	else if(p->command() == "InfoDriverID")
	{	
		// Our infodriver id is being updated. Start buy checking that the new value
		// is greater than 0 and less than 1000. Otherwise ignore the value
		int new_id = 0;
		new_id = p->value().firstInt();	// Extract the infodriver ID
		
		if ((new_id > 0) && (new_id < 1000))
		{
			// Valid Infodriver number. Must unregister any old connection.
			if ((infoDriver_id > 0) && (slot_id > 0)) {
				infoUnregister(infoDriver_id);		// Clear the old registration
			}
			infoDriver_id = new_id;					// Store the updated value
			infoRegister(infoDriver_id, slot_id, slot_id, false);	// Register new infodriver/slot for revertives
		}
	}

	else if( p->command() == "Slot" )
	{	
		// Persisted value or 'Command' being set here
		int new_slot = 0;
		new_slot = p->value().firstInt();	// Extract slot number.

		if ((new_slot >= 1) && (new_slot <= 4096))
		{
			// Valid slot number
			if ((infoDriver_id > 0) && (slot_id > 0))	{
				infoUnregister(infoDriver_id);		// Clear the old registration
			}
			slot_id = new_slot;					// Store the updated slot value
			infoRegister(infoDriver_id, slot_id, slot_id, false);	// Register new infodriver/slot for revertives
		}
	}

	// ***** CONNECTIONS EVENTS HELPER LIST *****
	else if( p->command() == "_events" )
	{	
		// Helper-list of everything in this component generated by hostNotify's
		bncs_stringlist sl;

		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;
		
		sl << "text=[value]";			// Text to show on the UMD
		sl << "InfoDriverID=[value]";	// Set the infodriver ID that hosts the UMD output
		sl << "Slot=[value]";			// Set the slot to which we send teh text
		return sl.toString( '\n' );
	}

	return "";
}

// timer events come here
void UMDsend::timerCallback( int id )
{
	switch( id )
	{
	case TIMER_SETUP:
		timerStop(id);
		break;

	default:	// Unhandled timer event
		timerStop(id);
		break;
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// Callbacks above - Methods below ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


