#include <windows.h>
#include <stdio.h>
#include <bncs_string.h>
#include <bncs_config.h>
#include "ComponentTest.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( ComponentTest )

// constructor - equivalent to ApplCore STARTUP
ComponentTest::ComponentTest( 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" );

	// 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
ComponentTest::~ComponentTest()
{
}

// all button pushes and notifications come here
void ComponentTest::buttonCallback( buttonNotify *b )
{
	if( b->panel() == 1 )
	{
		switch( b->id() )
		{
			case 1:			
				
				hostNotify("text=Hello from Child");
				
			break;
			
		}
	}
}

// all revertives come here
int ComponentTest::revertiveCallback( revertiveNotify * r )
{
/*	switch( r->device() )
	{
		case 123:
 			textPut( "text", r->sInfo(), 1, 3 );
			break;
	}
*/	return 0;
}

// all database name changes come back here
void ComponentTest::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 ComponentTest::parentCallback( parentNotify *p )
{
	if( p->command() == "return" )
	// each section here returns something to the parent in response to a textGet
	// from the parent panel, or from bncs_vis_ed.
	{
		if( p->value() == "all" )
		{	// Returns to bncs_vis_ed the list of parameters you see in the childs properties.
			// the global variables here (e.g. m_myParam1) are storing the values and in this case
			// passing them back to the list you see in properties.
			bncs_stringlist sl;
			
			sl << bncs_string( "myTestParam1=%1" ).arg( m_myParam1 );
			sl << bncs_string( "myTestParam2=%1" ).arg( m_myParam2 );
			
			return sl.toString( '\n' );
		}

		else if( p->value() == "myTestParam1" )
		{	// returns myTestParam1 from the global variable m_myParam1 when asked for by a textGet
			return( 
					bncs_string( "%1=%2" )
					.arg( p->value() )
					.arg( m_myParam1 ) 
				  );
		}

		else if( p->value() == "myTestParam2" )
		{	// returns myTestParam2 from the global variable m_myParam2 when asked for by a textGet
			return( 
					bncs_string( "%1=%2" )
					.arg( p->value() )
					.arg( m_myParam2 ) 

					// This function bncs_string, just joins together the strings inside the .arg() bits.
					// In this case, if m_myParam2 had the value 6 it would make the string
					// "myTestParam2=6"
				  );
		}
		
		else if( p->value() == "text" )
		{	// Specific value being asked for by a textGet
			return( 
					bncs_string( "%1=%2" )
					.arg( p->value() )
					.arg( "some hidden text from child" ) 
				  );
		}
	}
	// each section here receives something from the parent in response to a textPut
	// from the parent panel, or from bncs_vis_ed.

	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() == "myTestParam1" )
	{	// Persisted value or 'Command' being set here
		m_myParam1 = p->value();
		textPut("text",m_myParam1,PNL_MAIN,12);
	}

	else if( p->command() == "myTestParam2" )
	{	// Persisted value or 'Command' being set here
		m_myParam2 = p->value();
		textPut("text",m_myParam2,PNL_MAIN,13);
	}

	else if( p->command() == "text" )
	{	// This is triggered from the parent by a 
		// textPut("text", <message>,<ID of Script Control>, <Panel ID>)
		// The "text" is picked up by this 'else if'
		// and the text of the <message> appears in p->value().

		// This example puts the message onto control 11 on this child.
		
		textPut("text", p->value(),PNL_MAIN,11);
	}

	else if( p->command() == "colour" )
	{	// This is triggered from the parent by a 
		// textPut("colour.background", <colour>,<ID of Script Control>, <Panel ID>)
		// The "colour.background" is picked up by this 'else if'
		// and the colour appears in p->value().

		// This example puts makes a the label that is the background of the child (control 101) change colour
		//textPut("text", p->value() ,PNL_MAIN,11);
		textPut("colour.background", p->value(),PNL_MAIN,101);
	}

	// ***** CONNECTIONS EVENTS HELPER LIST *****
	else if( p->command() == "_events" )
	{	// Helper-list of everything in this component generated by hostNotify's
		bncs_stringlist sl;

		sl << "text=*"; // the word "text" here has to match the word "text" in hostNotify("text=Hello from Child")
		
		
		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 << "myTestParam1=[value]";
		sl << "myTestParam2=[value]";
		sl << "text=[value]";
		sl << "colour=[colour]";
		
		return sl.toString( '\n' );
	}

	return "";
}

// timer events come here
void ComponentTest::timerCallback( int id )
{
	switch( id )
	{
	case TIMER_SETUP:
		timerStop(id);
		break;

	default:	// Unhandled timer event
		timerStop(id);
		break;
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// Callbacks above - Methods below ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


