#include <windows.h>
#include <stdio.h>
#include <bncs_string.h>
#include <bncs_config.h>
#include "monFollow.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( monFollow )

// constructor - equivalent to ApplCore STARTUP
monFollow::monFollow( bncs_client_callback * parent, const char * path ) : bncs_script_helper( parent, path )
{
	m_device = 0;
	m_source = 0;
	m_dest = 0;
	m_following = false;

	m_dest = getWorkstationSetting("monitor");
}

// destructor - equivalent to ApplCore CLOSEDOWN
monFollow::~monFollow()
{
}

// all button pushes and notifications come here
void monFollow::buttonCallback( buttonNotify *b )
{
	b->dump("barf");
	if( b->panel() == PNL_MAIN )
	{
		if (b->id() == "button")
		{
			m_following = !m_following;
			if (m_following)
			{
				routerCrosspoint(m_device, m_source, m_dest);
				textPut("stylesheet=mon_following", PNL_MAIN, "button");
			}
			else
				textPut("stylesheet=mon_notfollowing", PNL_MAIN, "button");
		}
		else if (b->id() == "m")
		{
			routerCrosspoint(m_device, m_source, m_dest);
		}
	}
}

// all revertives come here
int monFollow::revertiveCallback( revertiveNotify * r )
{
	if (r->device() == m_device && r->index() == m_dest)
		textPut("text", r->sInfo(), PNL_MAIN, "tally");
	return 0;
}

// all database name changes come back here
void monFollow::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 monFollow::parentCallback( parentNotify *p )
{
	p->dump("barf");

	if( p->command() == "return" )
	{
		if( p->value() == "all" )
		{	// Persisting values for bncs_vis_ed
			bncs_stringlist sl;
			
			sl << bncs_string( "panel=%1" ).arg( m_panel );
			sl << bncs_string("source=%1").arg(m_source);

			return sl.toString( '\n' );
		}
	}
	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

		getDev(m_instance, &m_device);

		routerRegister(m_device, m_dest, m_dest);
		routerPoll(m_device, m_dest, m_dest);
	}
	else if (p->command() == "panel")
	{
		panelDestroy(PNL_MAIN);

		m_panel = p->value();
		
		panelShow(PNL_MAIN, m_panel);
	}
	else if( p->command() == "source" )
	{
		m_source = p->value();
		if (m_following)
			routerCrosspoint(m_device, m_source, m_dest);
	}

	// ***** 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 << "panel=monFollow | m | tally";
		sl << "source=[value]";
		
		return sl.toString( '\n' );
	}

	return "";
}

// timer events come here
void monFollow::timerCallback( int id )
{
	switch( id )
	{
	case TIMER_SETUP:
		timerStop(id);
		break;

	default:	// Unhandled timer event
		timerStop(id);
		break;
	}
}


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


