#include "PMDCamCubeSourcePluginWidget.h"

#include "ritkDebugManager.h"

PMDCamCubeSourcePluginWidget::PMDCamCubeSourcePluginWidget(QWidget *parent) :
	QWidget(parent)
{
	setupUi(this);

	SetState(DisconnectedState);

	// Connect the signals and slots
	connect(this->m_ConnectDisconnectButton,				SIGNAL(clicked()),			this,	SLOT(ConnectDisconnectButtonEvent()));
	connect(this->m_PushButtonPause,						SIGNAL(clicked()),			this,	SIGNAL(PauseEvent()));
	connect(this->m_PushButtonPlayForward,					SIGNAL(clicked()),			this,	SIGNAL(PlayEvent()));
	connect(this->m_PushButtonStepForward,					SIGNAL(clicked()),			this,	SIGNAL(StepForwardEvent()));
	connect(this->m_IntegrationTimeSpinBox,					SIGNAL(valueChanged(int)),	this,	SIGNAL(IntegrationTimeEvent(int)));
	connect(this->m_AverageSourceDataCheckBox,				SIGNAL(toggled(bool)),		this,	SLOT(SourceDataAveragingEventSlot(bool)));
	connect(this->m_NumFramesToAverageSourceDataSpinBox,	SIGNAL(valueChanged(int)),	this,	SIGNAL(NumFramesToAverageSourceDataEvent(int)));

	this->m_IntegrationTimeSpinBox->setValue(250);

	UpdateGUI();
}


PMDCamCubeSourcePluginWidget::~PMDCamCubeSourcePluginWidget()
{
	
}


//----------------------------------------------------------------------------
void 
PMDCamCubeSourcePluginWidget::SetState(CamInterfaceStateType State)
{
	QMutexLocker locker(&m_StateMutex);
	m_CurrentState = State;
}


//----------------------------------------------------------------------------
PMDCamCubeSourcePluginWidget::CamInterfaceStateType
PMDCamCubeSourcePluginWidget::GetState()
{
	QMutexLocker locker(&m_StateMutex);
	return m_CurrentState;
}


//----------------------------------------------------------------------------
void
PMDCamCubeSourcePluginWidget::UpdateGUI()
{
	switch( GetState() )
	{
		case DisconnectedState:
			this->m_ConnectDisconnectButton->setText("CONNECT");
			this->m_ConnectDisconnectButton->setEnabled(true);
			this->m_PushButtonPause->setEnabled(false);
			this->m_PushButtonPlayForward->setEnabled(false);
			this->m_PushButtonStepForward->setEnabled(false);
			this->m_IntegrationTimeSpinBox->setEnabled(false);
			break;
	
		case ConnectingState:
			this->m_ConnectDisconnectButton->setText("CONNECTING...");
			this->m_ConnectDisconnectButton->setEnabled(false);
			this->m_PushButtonPause->setEnabled(false);
			this->m_PushButtonPlayForward->setEnabled(false);
			this->m_PushButtonStepForward->setEnabled(false);
			this->m_IntegrationTimeSpinBox->setEnabled(false);
			break;
		
		case ConnectedState:
			this->m_ConnectDisconnectButton->setText("DISCONNECT");
			this->m_ConnectDisconnectButton->setEnabled(true);
			this->m_PushButtonPause->setEnabled(true);
			this->m_PushButtonPlayForward->setEnabled(true);
			this->m_PushButtonStepForward->setEnabled(true);
			this->m_IntegrationTimeSpinBox->setEnabled(true);
			break;

		case PlayLiveState:
			this->m_ConnectDisconnectButton->setEnabled(false);
			this->m_PushButtonPlayForward->setEnabled(false);
			this->m_PushButtonStepForward->setEnabled(false);
			break;
	}
}


void
PMDCamCubeSourcePluginWidget::SourceDataAveragingEventSlot(bool b)
{
	if ( b )
		this->m_NumFramesToAverageSourceDataSpinBox->setEnabled(true);
	else
		this->m_NumFramesToAverageSourceDataSpinBox->setEnabled(false);
	emit SourceDataAveragingEvent(b);
}


//----------------------------------------------------------------------------
void
PMDCamCubeSourcePluginWidget::ConnectDisconnectButtonEvent()
{
	if ( GetState() == ConnectedState )
		emit DisconnectEvent();
	else if ( GetState() == DisconnectedState )
		emit ConnectEvent();
	else
		LOG_DEB("Invalid call...");
}