#include <windows.h>
#include <stdio.h>
#include <bncs_string.h>
#include <bncs_config.h>
#include "Playpen.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( Playpen )

// constructor - equivalent to ApplCore STARTUP
Playpen::Playpen( 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
Playpen::~Playpen()
{
}

// all button pushes and notifications come here
void Playpen::buttonCallback( buttonNotify *b )
{
	if( b->panel() == PNL_MAIN )	// Check this is our main panel
	{
		textPut("add", "b->command() = " + b->command(), PNL_MAIN, "List_1");
		textPut("add", "b->id() = " + b->id(), PNL_MAIN, "List_1");
		textPut("add", "b->value() = " + b->value(), PNL_MAIN, "List_1");
		textPut("add", "b->subs() = " + bncs_string(b->subs(),'0',3,10), PNL_MAIN, "List_1");
		if (b->subs() > 0)
		{
			for (int i = 0; i < b->subs(); i++)
			{
				textPut("add", "b->sub(" + bncs_string(i) + ") = " + b->sub(i), PNL_MAIN, "List_1");
			}
		}
		textPut("add", "----------------", PNL_MAIN, "List_1");
		// Send data to the debug window
		b->dump();
		debug("\n");
		
		if (b->id() == "myTest_1")
		{
			textPut("text", b->id(), PNL_MAIN ,"Disp_1");
			textPut("text", b->command(), PNL_MAIN, "Disp_2");
			textPut("text", b->value(), PNL_MAIN, "Disp_3");
			return;
		}

		if (b->id() == "myTest_2")
		{
			textPut("colour.text=blue", PNL_MAIN, "myTest_1");
			//textGet("stylesheet", PNL_MAIN, "myTest_1", cmd);
			//textPut("text", cmd, PNL_MAIN, "Disp_4");
			textGet("return=mapdb", PNL_MAIN, "ng1", cmd);
			textPut("text", cmd, PNL_MAIN, "Disp_4");
			textPut("selected", 3, PNL_MAIN, "dg1");
			return;
		}

		if (b->id() == "dg1")
		{
			textPut("text", b->id(), PNL_MAIN, "Disp_1");
			textPut("text", b->command(), PNL_MAIN, "Disp_2");
			textPut("text", b->value(), PNL_MAIN, "Disp_3");
			return;
		}


	}	
}

// all revertives come here
int Playpen::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 Playpen::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 Playpen::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 Playpen::timerCallback( int id )
{
	switch( id )
	{
	case TIMER_SETUP:
		timerStop(id);
		break;

	default:	// Unhandled timer event
		timerStop(id);
		break;
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// Callbacks above - Methods below ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


