#include <windows.h>
#include <stdio.h>
#include <bncs_string.h>
#include <bncs_config.h>
#include "slinkNetStatus.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( slinkNetStatus )

// constructor - equivalent to ApplCore STARTUP
slinkNetStatus::slinkNetStatus( bncs_client_callback * parent, const char * path ) : bncs_script_helper( parent, path )
{
	m_device = 0;

	// show a panel from file slinkNetStatus.bncs_ui and we'll know it as our panel PNL_MAIN
	panelShow( PNL_MAIN, "slinkNetStatus.bncs_ui" );
	setSize( PNL_MAIN );		// set the size to the same as the specified panel

	m_client = new ccClient(this);
	m_client->connect();
}

// destructor - equivalent to ApplCore CLOSEDOWN
slinkNetStatus::~slinkNetStatus()
{
	delete m_client;
}

// all button pushes and notifications come here
void slinkNetStatus::buttonCallback( buttonNotify *b )
{
	if( b->panel() == PNL_MAIN )
	{
		switch( b->id() )
		{
			case 1:			textPut( "text", "you pressed|control 1", PNL_MAIN, 4 );		break;
			case 2:			textPut( "text", "you pressed|control 2", PNL_MAIN, 4 );		break;
			case 3:			textPut( "text", "you pressed|control 3", PNL_MAIN, 4 );		break;
		}
	}
}

// all revertives come here
int slinkNetStatus::revertiveCallback( revertiveNotify * r )
{
/*	switch( r->device() )
	{
		case 123:
 			textPut( "text", r->sInfo(), PNL_MAIN, 3 );
			break;
	}
*/	return 0;
}

// all database name changes come back here
void slinkNetStatus::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 slinkNetStatus::parentCallback( parentNotify *p )
{
	if( p->command() == "return" )
	{
		if( p->value() == "all" )
		{	// Persisting values for bncs_vis_ed
			bncs_stringlist sl;
			
			sl << bncs_string( "name=%1" ).arg( m_name);
			sl << bncs_string("workstation=%1").arg(m_ws);

			return sl.toString( '\n' );
		}
		else if( p->value() == "name" )
		{	// Specific value being asked for by a textGet
			return(bncs_string("%1=%2").arg(p->value()).arg(m_name));
		}
		else if (p->value() == "workstation")
		{	// Specific value being asked for by a textGet
			return(bncs_string("%1=%2").arg(p->value()).arg(m_ws));
		}
	}
	else if( p->command() == "instance" && p->value() != m_instance )
	{	// Our instance is being set/changed
		m_instance = p->value();
		
		m_client->revertiveDeregisterAll();

		getDev(m_instance, &m_device);

		// any-old registration will do to receive NS messages
		m_client->revertiveRegister(m_device, 4096, 4096);
	}
	else if( p->command() == "name" )
	{	// Persisted value or 'Command' being set here
		m_name = p->value();
	}
	else if (p->command() == "workstation")
	{	// Persisted value or 'Command' being set here
		m_ws = p->value().toInt();
		m_client->deviceMessage(m_ws, m_device, "?=?", 0);

		textPut("text", m_name + " - WS " + m_ws, PNL_MAIN, "name");

	}
	// ***** 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 slinkNetStatus::timerCallback( int id )
{
	switch( id )
	{
	case TIMER_SETUP:
		timerStop(id);
		break;

	default:	// Unhandled timer event
		timerStop(id);
		break;
	}
}


void slinkNetStatus::cccDeviceMessage(int workstation, int device, const string & message, int reference)
{
	if (workstation != m_ws)
		return;

	bncs_stringlist sl(message.c_str(), '=');

	if (sl[0] == "address")
		textPut("text", sl[1], PNL_MAIN, sl[0]);
	else if (sl[0] == "peer")
		textPut("text", sl[1], PNL_MAIN, sl[0]);
	else if (sl[0] == "port")
		textPut("text", sl[1], PNL_MAIN, sl[0]);
	else if (sl[0] == "alt")
		textPut("text", sl[1], PNL_MAIN, sl[0]);
	else if (sl[0] == "fabian")
	{
		switch (sl[1])
		{
		default:
			textPut("stylesheet=enum_alarm", PNL_MAIN, "status");
			textPut("text=BROKEN", PNL_MAIN, "status");
			break;

		case 3:
			textPut("stylesheet=enum_warning", PNL_MAIN, "status");
			textPut("text=Ok (RxOnly)", PNL_MAIN, "status");
			break;
		case 4:
			textPut("stylesheet=enum_ok", PNL_MAIN, "status");
			textPut("text=Ok (TxRx)", PNL_MAIN, "status");
			break;
		}
	}
}
