#include <windows.h>
#include <stdio.h>
#include <bncs_string.h>
#include <bncs_config.h>
#include "tallyCpp.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( tallyCpp )

// constructor - equivalent to ApplCore STARTUP
tallyCpp::tallyCpp( bncs_client_callback * parent, const char * path ) : bncs_script_helper( parent, path )
{
	m_singleLine = false;
	m_index = 0;
	m_device = 0;

	// show a panel from file p1.bncs_ui and we'll know it as our panel PNL_MAIN
	panelShow( PNL_MAIN, "tally.bncs_ui" );

	setSize(PNL_MAIN);
}

// destructor - equivalent to ApplCore CLOSEDOWN
tallyCpp::~tallyCpp()
{
}

// all button pushes and notifications come here
void tallyCpp::buttonCallback( buttonNotify *b )
{
}

// all revertives come here
int tallyCpp::revertiveCallback( revertiveNotify * r )
{
	if ( m_singleLine )
 		textPut( "text", r->sInfo().replace( '|', ' ' ), PNL_MAIN, "tally" );
	else
		textPut("text", r->sInfo(), PNL_MAIN, "tally");
	return 0;
}

// all database name changes come back here
void tallyCpp::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 tallyCpp::parentCallback( parentNotify *p )
{
	if( p->command() == "return" )
	{
		if( p->value() == "all" )
		{	// Persisting values for bncs_vis_ed
			bncs_stringlist sl;
			
			sl << bncs_string( "single_line=%1" ).arg( m_singleLine?"true":"false" );
			sl << bncs_string("index=%1").arg(m_index);

			return sl.toString( '\n' );
		}
	}
	else if( p->command() == "instance" && p->value() != m_instance )
	{	// Our instance is being set/changed
		m_instance = p->value();

		checkReg();
	}

	else if( p->command() == "index" )
	{	// Persisted value or 'Command' being set here
		m_index= p->value();

		checkReg();
	}

	else if (p->command() == "single_line")
	{	// Persisted value or 'Command' being set here
		m_singleLine = (p->value() == "true") ? true: false;
	}

	// ***** 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 << "index=[value]";
		
		return sl.toString( '\n' );
	}

	return "";
}

void tallyCpp::checkReg(void)
{
	if (m_device)
		routerUnregister(m_device);

	getDev(m_instance, &m_device);

	if (m_index && m_device)
	{
		routerRegister(m_device, m_index, m_index);
		routerPoll(m_device, m_index, m_index);
	}
	else
		textPut("text=", PNL_MAIN, "tally");
}


// timer events come here
void tallyCpp::timerCallback( int id )
{
}
