#include <windows.h>
#include <stdio.h>
#include <bncs_string.h>
#include <bncs_config.h>
#include "TakeA.h"

#define MYPANEL	1
#define MYPANEL_NAME "TakeA.bncs_ui"

#define CLEAR_TIMER 1

// Make our class visible to the outside world
EXPORT_BNCS_SCRIPT(TakeA)

// Constructor - equivalent to ApplCore STARTUP
TakeA::TakeA(bncs_client_callback *parent, const char * path) : bncs_script_helper(parent, path)
{
	// Show our component panel
	panelShow(MYPANEL, MYPANEL_NAME);

	// Initialise variables
	m_src_id = "-1";
	m_dest_id = "-1";
	src_no = -1;
	dest_no = -1;;
	valid_source = false;
	valid_dest = false;
	take_enabled = false;

	textPut("text", (src_no > 0) ? m_src_id : "---", MYPANEL, "Src");
	textPut("text", (dest_no > 0) ? m_dest_id : "---", MYPANEL, "Dest");

	controlDisable(MYPANEL, "Take");		// Disable the take button.
}

// destructor - equivalent to ApplCore CLOSEDOWN
TakeA::~TakeA()
{
	panelDestroy(MYPANEL);
}

// all button pushes and notifications come here
void TakeA::buttonCallback( buttonNotify *b )
{
	if(b->panel() == MYPANEL)
	{
		if (b->id() == "Take")
		{
			// Send the source id (as a string) provided both source and destination are valid numbers.
			hostNotify("cmd="+m_src_id);
			timerStart(CLEAR_TIMER, 100);	// Start a timer ready to issue clear commands on timeout
			valid_source = false;			// Clear down the take system
			valid_dest = false;				// by clearing the flags and stores
			src_no = 0;
			dest_no = 0;
			m_src_id = "";
			m_dest_id = "";
			textPut("text", "---", MYPANEL, "Src");
			textPut("text", "---", MYPANEL, "Dest");
			check_enableTake();
		}
	}
}

// all revertives come here
int TakeA::revertiveCallback( revertiveNotify * r )
{
	return 0;
}

// all database name changes come back here
void TakeA::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 TakeA::parentCallback(parentNotify *p)
{
	// We only expect two inputs - the source number and the destination number (both expressed as a bncs_string)

	 if (p->command() == "Source")
	{
		// Have received a new source number from the source keypad.
		m_src_id = p->value();							// Save the new source value
		src_no = m_src_id.toInt();						// Make an integer of the value
		valid_source = (src_no > 0) ? true : false;
		textPut("text", (valid_source) ? m_src_id : "---", MYPANEL, "Src");
		check_enableTake();
		return "";
	}

	else if (p->command() == "Dest")
	{
		m_dest_id = p->value();
		dest_no = m_dest_id.toInt();
		valid_dest = (dest_no > 0) ? true : false;
		textPut("text", (valid_dest) ? m_dest_id : "---", MYPANEL, "Dest");
		check_enableTake();
		return "";
	}

	// ***** CONNECTIONS EVENTS HELPER LIST *****
	else if(p->command() == "_events")
	{	
		// Helper-list of everything in this component generated by hostNotify's
		bncs_stringlist sl;

		sl << "cmd=*";					// Used by the TAKE action
		sl << "cleardown=*";			// Used to initiate the clear of the source/destination name grid
		
		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 << "Source=[value]";
		sl << "Dest=[value]";

		return sl.toString('\n');
	}

	return "";
}

// timer events come here
void TakeA::timerCallback( int id )
{
	
	if (id == CLEAR_TIMER)
	{
		timerStop(CLEAR_TIMER);
		hostNotify("cleardown=-1");
	}
	else
		timerStop(id);
	
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// Callbacks above - Methods below ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

void TakeA::check_enableTake(void)
{
	// Enable/Disable the take control. To enable the control, a valid source and a valid destination must be selected.
	if (take_enabled)
	{
		// Are both source and destination still valid. If not, disable the take button
		if (!(valid_source && valid_dest))
		{
			controlDisable(MYPANEL, "Take");
			take_enabled = false;
		}
	}
	else
	{
		// Not currently enabled - do we need to enable?
		if (valid_source && valid_dest)
		{
			controlEnable(MYPANEL, "Take");
			take_enabled = true;
		}
	}
}
