#include <windows.h>
#include <stdio.h>
#include <bncs_string.h>
#include <bncs_config.h>
#include "ConnectionsEngine.h"

#define PNL_MAIN	1

#define TIMER_SETUP	1

// this nasty little macro to make our class visible to the outside world
EXPORT_BNCS_SCRIPT( ConnectionsEngine )

// constructor - equivalent to ApplCore STARTUP
ConnectionsEngine::ConnectionsEngine( bncs_client_callback * parent, const char * path ) : bncs_script_helper( parent, path )
{
	// show a panel from file p1.bncs_ui and we'll know it as our panel PNL_MAIN
	panelShow( PNL_MAIN, "p1.bncs_ui" );
	myOutput = "Type something first!"; //just to give you some output if somebody sends text without having typed anything

	// you may need this call to set the size of this component 
	//  if it's used in a popup window 
//	setSize( 1024,668 );		// set the size explicitly
	setSize( PNL_MAIN );		// set the size to the same as the specified panel
}

// destructor - equivalent to ApplCore CLOSEDOWN
ConnectionsEngine::~ConnectionsEngine()
{
}

// all button pushes and notifications come here
void ConnectionsEngine::buttonCallback( buttonNotify *b )
{
	if( b->panel() == PNL_MAIN )
	{
		switch( b->id() )
		{
		case 1:	//if button 1 has been clicked, send the contents of the textbox to the calling component
				//the first word in the hostNotify command has to be the same as the events command helper list
				//then clear the variable 

			hostNotify("return=" + myOutput);
			myOutput = "";
	
			
		}
		if (b->id() == "LineEdit")
		{
			//the line edit box has had its value changed, so update the variable
			myOutput = b->value();
		}
		if (b->id() == "ON")
		{
			//the ON button has been pressed, the GPI selected in the parameters box will be turned on
			gpiSwitch(7, 1, m_myParam, true);
		}
		if (b->id() == "OFF")
		{
			//the OFF button has been pressed, the GPI selected in the parameters box will be turned off
			gpiSwitch(7, 0, m_myParam, true);
		}
	}
}

// all revertives come here
int ConnectionsEngine::revertiveCallback( revertiveNotify * r )
{
return 0;
}

// all database name changes come back here
void ConnectionsEngine::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 ConnectionsEngine::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 from VisEd being set here, this is the GPI value from the parameter set box
		m_myParam = p->value();
	}
	else if (p->command() == "myInput")
	{	//Value passed through from the Connections Engine, if the selected command is passing something
		//to the 'myInput' parameter, display this on DisplayLabel
		myInput = p->value();
		textPut("text", myInput, PNL_MAIN, "DisplayLabel");
	}

	// ***** CONNECTIONS EVENTS HELPER LIST *****
	else if( p->command() == "_events" )
	{	// Helper-list of everything in this component generated by hostNotify's
		//This has to match the wording in the button-call to hostNotify
		bncs_stringlist sl;

		sl << "return=*";
		
		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 << "myInput=[value]";
		
		return sl.toString( '\n' );
	}

	return "";
}

// timer events come here
void ConnectionsEngine::timerCallback( int id )
{
	switch( id )
	{
	case TIMER_SETUP:
		timerStop(id);
		break;

	default:	// Unhandled timer event
		timerStop(id);
		break;
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// Callbacks above - Methods below ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


