#include <windows.h>
#include <stdio.h>
#include <bncs_string.h>
#include <bncs_config.h>
#include "TakeACTE_names.h"

#define MYPANEL	1
#define MYPANEL_NAME "TakeACTE_names.bncs_ui"

#define CLEAR_TIMER 1

// Make our class visible to the outside world
EXPORT_BNCS_SCRIPT(TakeACTE_names)

// Constructor - equivalent to ApplCore STARTUP
TakeACTE_names::TakeACTE_names(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
TakeACTE_names::~TakeACTE_names()
{
	panelDestroy(MYPANEL);
}

// all button pushes and notifications come here
void TakeACTE_names::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 TakeACTE_names::revertiveCallback(revertiveNotify * r)
{
	return 0;
}

// all database name changes come back here
void TakeACTE_names::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 TakeACTE_names::parentCallback(parentNotify *p)
{
	// We only expect two inputs - the source number and the destination number (both expressed as a bncs_string)
	if (p->command() == "return"){
		if (p->value() == "all")
		{
			{	// Persisting values for bncs_vis_ed
				bncs_stringlist sl;

				sl << bncs_string("routerDriver=%1").arg(m_routerDriverNumber);
				sl << bncs_string("instance=%1").arg(m_instance);
				return sl.toString('\n');
			}
		}
		else if (p->value() == "routerDriver")
		{	// Specific value being asked for by a textGet
			return(bncs_string("%1=%2").arg(p->value()).arg(m_routerDriverNumber));
		}
		else if (p->value() == "instance")
		{	// Specific value being asked for by a textGet
			return(bncs_string("%1=%2").arg(p->value()).arg(m_instance));
		}
	}
	else if (p->command() == "instance" && p->value() != m_instance)
	{	// Our instance is being set/changed
		m_instance = p->value();
		getDev(m_instance, &m_dev, &m_idx);
	}
	 else 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;
		bncs_string srcName;
		routerName(m_dev, 0, src_no, srcName);
		bncs_string name = (valid_source) ? srcName : "---";
		textPut("text", name, 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;
		bncs_string dstName;
		routerName(m_dev, 1, dest_no, dstName);
		bncs_string name = (valid_dest) ? dstName : "---";
		textPut("text", name , 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 TakeACTE_names::timerCallback(int id)
{
	
	if (id == CLEAR_TIMER)
	{
		timerStop(CLEAR_TIMER);
		hostNotify("cleardown=-1");
	}
	else
		timerStop(id);
	
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// Callbacks above - Methods below ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

void TakeACTE_names::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;
		}
	}
}
