#include "CUDABilateralImageFilterPlugin.h"
#include "ritkManager.h"

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

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

	// Create GUI widget
	m_Widget = new CUDABilateralFilterWidget();

	// Connect signals/slots
	connect(m_Widget, SIGNAL(RangeDataCheckBoxChanged(bool)), this, SLOT(SetRangeFilterMode(bool)));
	connect(m_Widget, SIGNAL(RGBDataCheckBoxChanged(bool)), this, SLOT(SetRGBFilterMode(bool)));
	connect(m_Widget, SIGNAL(RangeSigmaSpatialChanged(float)), this, SLOT(SetRangeSigmaSpatial1(float)));
	connect(m_Widget, SIGNAL(RangeSigmaRangeChanged(float)), this, SLOT(SetRangeSigmaRange1(float)));	
	connect(m_Widget, SIGNAL(RGBSigmaSpatialChanged(float)), this, SLOT(SetRGBSigmaSpatial1(float)));
	connect(m_Widget, SIGNAL(RGBSigmaRangeChanged(float)), this, SLOT(SetRGBSigmaRange1(float)));

	connect(m_Widget, SIGNAL(RangeKernelSizeChanged(int)), this, SLOT(SetRangeKernelSize(int)));
	connect(m_Widget, SIGNAL(RGBKernelSizeChanged(int)), this, SLOT(SetRGBKernelSize(int)));
	

	// Range filtering by default
	emit SetRangeFilterMode(true);
	emit SetRGBFilterMode(false);
	m_Widget->Preset();
}


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


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


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


//----------------------------------------------------------------------------
void
CUDABilateralImageFilterPlugin::SetRangeSigmaSpatial1(float SigmaSpatial)
{
	if ( m_RangeSigmaSpatial == SigmaSpatial )
		return;

	LockPlugin();

	m_RangeSigmaSpatial = SigmaSpatial;

	LockPipeline();
	m_Filter->SetRangeSigmaSpatial(m_RangeSigmaSpatial);
	UnlockPipeline();

	UnlockPlugin();
}


//----------------------------------------------------------------------------
void
CUDABilateralImageFilterPlugin::SetRangeSigmaRange1(float SigmaRange)
{
	if ( m_RangeSigmaRange == SigmaRange )
		return;

	LockPlugin();

	m_RangeSigmaRange = SigmaRange;

	LockPipeline();
	m_Filter->SetRangeSigmaRange(m_RangeSigmaRange);
	UnlockPipeline();


	UnlockPlugin();
}


//----------------------------------------------------------------------------
void
CUDABilateralImageFilterPlugin::SetRangeKernelSize(int size)
{
	LockPlugin();
	LockPipeline();
	m_Filter->SetRangeKernelSize(size);
	UnlockPipeline();
	UnlockPlugin();
}


//----------------------------------------------------------------------------
void
CUDABilateralImageFilterPlugin::SetRGBSigmaSpatial1(float SigmaSpatial)
{
	if ( m_RGBSigmaSpatial == SigmaSpatial )
		return;

	LockPlugin();

	m_RGBSigmaSpatial = SigmaSpatial;

	/*LockPipeline();
	m_Filter->SetRGBSigmaSpatial(m_RGBSigmaSpatial);
	UnlockPipeline();*/

	UnlockPlugin();
}


//----------------------------------------------------------------------------
void
CUDABilateralImageFilterPlugin::SetRGBSigmaRange1(float SigmaRange)
{
	if ( m_RGBSigmaRange == SigmaRange )
		return;

	LockPlugin();

	m_RGBSigmaRange = SigmaRange;

	/*LockPipeline();
	m_Filter->SetRGBSigmaRange(m_RGBSigmaRange);
	UnlockPipeline();*/


	UnlockPlugin();
}


//----------------------------------------------------------------------------
void
CUDABilateralImageFilterPlugin::SetRGBKernelSize(int size)
{
	/*LockPlugin();
	LockPipeline();
	m_Filter->SetRGBKernelSize(size);
	UnlockPipeline();
	UnlockPlugin();*/
}


//----------------------------------------------------------------------------
void
CUDABilateralImageFilterPlugin::SetParameter(QString Name, QString Value)
{	
	if (!strcmp(Name.toStdString().c_str(), "RangeSigma"))
	{
		if ( !Value.isEmpty() ) 
		{
			m_Widget->m_RangeSigmaRangeLabel->setText(Value.toStdString().c_str());
			m_Widget->m_RangeSigmaRangeSlider->setValue(atof(Value.toStdString().c_str()));
			m_Filter->SetRangeSigmaRange(atof(Value.toStdString().c_str()));
		}	
	}
	if (!strcmp(Name.toStdString().c_str(), "SpatialSigma"))
	{
		if ( !Value.isEmpty() ) 
		{
			m_Widget->m_RangeSigmaSpatialLabel->setText(Value.toStdString().c_str());
			m_Widget->m_RangeSigmaSpatialSlider->setValue(atof(Value.toStdString().c_str())*10);
			m_Filter->SetRangeSigmaSpatial(atof(Value.toStdString().c_str()));
		}	
	}
	if (!strcmp(Name.toStdString().c_str(), "KernelSize"))
	{
		if ( !Value.isEmpty() ) 
		{
			m_Widget->m_RangeKernelComboBox->setCurrentIndex(atoi(Value.toStdString().c_str()));
			m_Filter->SetRangeKernelSize(atoi(Value.toStdString().c_str()));
		}	
	}
}
