#include "CUDATemporalAveragingImageFilterPlugin.h"
#include "ritkManager.h"

CUDATemporalAveragingImageFilterPlugin::CUDATemporalAveragingImageFilterPlugin()
{
    // We need to initialize the CUDA context
    ritk::CudaContext::GetInstance(); 

	// Initialize filter
	m_Filter = CUDATemporalAveragingFilterType::New();

	// Filter modes
	m_FilterRange = false;

	// Create GUI widget
	m_Widget = new CUDATemporalAveragingFilterWidget();
	connect(m_Widget, SIGNAL(NumRangeDataFramesChanged(unsigned int)), this, SLOT(SetNumRangeDataFrames(unsigned int)));
	connect(m_Widget, SIGNAL(NumRGBDataFramesChanged(unsigned int)), this, SLOT(SetNumRGBDataFrames(unsigned int)));

	connect(m_Widget, SIGNAL(RangeDataCheckBoxChanged(bool)), this, SLOT(SetRangeFilterMode(bool)));
	connect(m_Widget, SIGNAL(RGBDataCheckBoxChanged(bool)), this, SLOT(SetRGBFilterMode(bool)));

	m_Widget->m_RangeDataFramesSlider->setValue(20);
	m_Widget->m_RGBDataFramesSlider->setValue(10);

	emit SetRangeFilterMode(true);
	emit SetRGBFilterMode(false);
}


CUDATemporalAveragingImageFilterPlugin::~CUDATemporalAveragingImageFilterPlugin()
{
	delete m_Widget;
}


//----------------------------------------------------------------------------
QWidget*
CUDATemporalAveragingImageFilterPlugin::GetPluginGUI()
{
	return m_Widget;
}


//----------------------------------------------------------------------------
CUDATemporalAveragingImageFilterPlugin::FilterPlugin::FilterPointer 
CUDATemporalAveragingImageFilterPlugin::GetFilter()
{	
	return (FilterPlugin::FilterType*)m_Filter;
}


//----------------------------------------------------------------------------
void
CUDATemporalAveragingImageFilterPlugin::SetNumRangeDataFrames(unsigned int NumFrames)
{
	LockPlugin();

	// Lock the pipeline (we're altering an element of it...)
	LockPipeline();
		
	// Update the filter
	m_Filter->SetNumRangeDataFrames(NumFrames);

	// Unlock the pipeline
	UnlockPipeline();

	UnlockPlugin();
}


//----------------------------------------------------------------------------
void
CUDATemporalAveragingImageFilterPlugin::SetNumRGBDataFrames(unsigned int NumFrames)
{
	LockPlugin();

	// Lock the pipeline (we're altering an element of it...)
	LockPipeline();
		
	// Update the filter
	m_Filter->SetNumRGBDataFrames(NumFrames);

	// Unlock the pipeline
	UnlockPipeline();

	UnlockPlugin();
}


//----------------------------------------------------------------------------
void
CUDATemporalAveragingImageFilterPlugin::SetParameter(QString Name, QString Value)
{	
	if (!strcmp(Name.toStdString().c_str(), "NumRangeDataFrames"))
	{
		SetNumRangeDataFrames(atoi(Value.toStdString().c_str()));
		m_Widget->ChangeRangeDataSliderValue(atoi(Value.toStdString().c_str()));
	}
	if (!strcmp(Name.toStdString().c_str(), "NumRGBDataFrames"))
	{
		SetNumRGBDataFrames(atoi(Value.toStdString().c_str()));
		m_Widget->ChangeRGBDataSliderValue(atoi(Value.toStdString().c_str()));
	}
}
