#include <windows.h>
#include <stdio.h>
#include <bncs_string.h>
#include <bncs_config.h>
#include "Gal_Lights.h"

#define MAIN_PANEL	1

#define TIMER_SETUP	1

// this nasty little macro to make our class visible to the outside world
EXPORT_BNCS_SCRIPT( Gal_Lights )

// constructor - equivalent to ApplCore STARTUP
Gal_Lights::Gal_Lights( bncs_client_callback * parent, const char * path ) : bncs_script_helper( parent, path )
{
	// Set default values for global variables
	gpiDriver = 0;		// GPI driver ID
	pcrHouse = 0;		// GPI Contact ID for PCR house lights
	pcrOps= 0;			// GPI Contact ID for PCR operational lights
	scrHouse = 0;		// GPI Contact ID for SCR house lights
	scrOps = 0;			// GPI Contact ID for SCR operational lights
	
	// Get GPI driver data and offsets
	getDev("StA PCR House Lights", &gpiDriver, &pcrHouse);
	getDev("StA PCR Prod Lights", &gpiDriver, &pcrOps);
	getDev("StA SCR House Lights", &gpiDriver, &scrHouse);
	getDev("StA SCR Prod Lights", &gpiDriver, &scrOps);

	// Show a panel p1.bncs_ui
	panelShow( MAIN_PANEL, "p1.bncs_ui" );
}

// destructor - equivalent to ApplCore CLOSEDOWN
Gal_Lights::~Gal_Lights()
{
	
}

// all button pushes and notifications come here
void Gal_Lights::buttonCallback( buttonNotify *b )
{
	if ((b->panel() == MAIN_PANEL) && (b->id().startsWith("SW")))
	{
		switch (b->id().firstInt())
		{
			case 1:
				// PCR House Off
				gpiSwitch(gpiDriver, true, pcrHouse);
				break;
			case 2:
				// PCR House On
				gpiSwitch(gpiDriver, false, pcrHouse);
				break;
			case 3:
				// PCR Operational Off
				gpiSwitch(gpiDriver, true, pcrOps);
				break;
			case 4:
				// PCR Operational On
				gpiSwitch(gpiDriver, false, pcrOps);
				break;
			case 5:
				// SCR House Off
				gpiSwitch(gpiDriver, true, scrHouse);
				break;
			case 6:
				// SCR House On
				gpiSwitch(gpiDriver, false, scrHouse);
				break;
				break;
			case 7:
				// SCR Operational Off
				gpiSwitch(gpiDriver, true, scrOps);
				break;
			case 8:
				// SCR Operational On
				gpiSwitch(gpiDriver, false, scrOps);
				break;
		}
	}
}

// all revertives come here
int Gal_Lights::revertiveCallback( revertiveNotify * r )
{
/*	switch( r->device() )
	{
		case 123:
 			textPut( "text", r->sInfo(), PNL_MAIN, 3 );
			break;
	}
*/	return 0;
}

// all database name changes come back here
void Gal_Lights::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 Gal_Lights::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 Gal_Lights::timerCallback( int id )
{
	switch( id )
	{
	case TIMER_SETUP:
		timerStop(id);
		break;

	default:	// Unhandled timer event
		timerStop(id);
		break;
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// Callbacks above - Methods below ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


