#include <windows.h>
#include <stdio.h>
#include <bncs_string.h>
#include <bncs_config.h>
#include "gpi675test.h"

#define MAIN_PANEL	1

#define TIMER_SETUP	1

#define GPI_DRIVER 675

// this nasty little macro to make our class visible to the outside world
EXPORT_BNCS_SCRIPT( gpi675test )

// constructor - equivalent to ApplCore STARTUP
gpi675test::gpi675test( bncs_client_callback * parent, const char * path ) : bncs_script_helper( parent, path )
{
	// Show a panel from file gpi675test.bncs_ui and we'll know it as our panel MAIN_PANEL
	panelShow(MAIN_PANEL, "gpi675test.bncs_ui");

	for (int i = 0; i < 65; i++) outstate[i] = false;

	// Number the outputs
	for (int i = 1; i <= 64; i++)
	{
		textPut("text", bncs_string("%1").arg(i,'0',2,10), MAIN_PANEL, bncs_string("Out_%1").arg(i,'0',2,10));
	}

	// Number the inputs
	for (int o = 101; o <= 148; o++)
	{
		textPut("text", bncs_string("%1").arg(o), MAIN_PANEL, bncs_string("In_%1").arg(o));
	}

	gpiRegister(GPI_DRIVER, 1, 148);
	gpiPoll(GPI_DRIVER, 1, 148);
}

// destructor - equivalent to ApplCore CLOSEDOWN
gpi675test::~gpi675test()
{
	gpiUnregister(GPI_DRIVER);
}


// all button pushes and notifications come here
void gpi675test::buttonCallback( buttonNotify *b )
{
	if(b->panel() == MAIN_PANEL)
	{
		if (b->id().startsWith("Out_"))
		{
			int ix;
			bool newState;
			ix = b->id().firstInt();
			newState = (outstate[ix] == 0) ? true : false;	// Toggle the state
			gpiSwitch(GPI_DRIVER, newState, ix ,true);
		}
	}
}

// all revertives come here
int gpi675test::revertiveCallback( revertiveNotify * r )
{

	if (r->device() == GPI_DRIVER)
	{
		int idx;
		bncs_string mimicState;

		idx = r->index();

		mimicState = (r->info() == 0) ? "destgroup_deselected" : "alarm_alarm";

		if (idx <= 64)
		{
			// Set output mimic state
			outstate[idx] = r->info();
			textPut("statesheet", mimicState, MAIN_PANEL, bncs_string("Out_%1").arg(idx, '0', 2, 10));
		}

		if ((idx > 100) && (idx <= 148))
		{
			// Set input mimic state
			textPut("statesheet", mimicState, MAIN_PANEL, bncs_string("In_%1").arg(idx, '0', 3, 10));
		}
	}

	return 0;
}

// all database name changes come back here
void gpi675test::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 gpi675test::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 gpi675test::timerCallback( int id )
{
	switch( id )
	{
	case TIMER_SETUP:
		timerStop(id);
		break;

	default:	// Unhandled timer event
		timerStop(id);
		break;
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// Callbacks above - Methods below ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


