#include "QuadVisualizationPlugin.h"
#include "ritkDebugManager.h"
#include "ritkManager.h"
#include "ritkOpenGLRImageControlUnit.h"


QuadVisualizationPlugin::QuadVisualizationPlugin()
{
	// Create the widget
	m_Widget = new QuadVisualizationWidget();
	connect(this, SIGNAL(UpdateGUI()), m_Widget, SLOT(UpdateGUI()));
}

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


//----------------------------------------------------------------------------
QString
QuadVisualizationPlugin::GetName()
{
	return tr("QuadVisualizationPlugin");
}


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


//----------------------------------------------------------------------------
void
QuadVisualizationPlugin::ProcessEvent(ritk::Event::Pointer EventP)
{
	// New frame event
	if ( EventP->type() == ritk::NewFrameEvent::EventType )
	{
		// Cast the event
		ritk::NewFrameEvent::Pointer NewFrameEventP = qSharedPointerDynamicCast<ritk::NewFrameEvent,ritk::Event>(EventP);
		if ( !NewFrameEventP )
		{
			LOG_DEB("Event mismatch detected: Type=" << EventP->type());
			return;
		}
		ritk::NewFrameEvent::RImageConstPointer CurrentFrameP = NewFrameEventP->RImage;

		LockPlugin();
		m_Widget->SetRangeData(CurrentFrameP);
		UnlockPlugin();

		emit UpdateGUI();
	}
	else
	{
		LOG_DEB("Unknown event");
	}
}


//----------------------------------------------------------------------------
void
QuadVisualizationPlugin::SetParameter(QString Name, QString Value)
{	
	if (!strcmp(Name.toStdString().c_str(), "Payload1"))
	{
		if ( !Value.isEmpty() ) 
		{
			QStringList visualizationParams = Value.split(",");
			// 1. Visualization mode
			if(visualizationParams.at(0) == "2D")
				m_Widget->m_VisualizationUnit0->SetRender3D(false);
			else
				m_Widget->m_VisualizationUnit0->SetRender3D(true);

			if(visualizationParams.size()==6)
			{
				ritk::ImagePayloadVisualizationConfig VisualizationConfig;
				VisualizationConfig.ValidFlag = true; // SVEN should be checked ...
				// 2. PayloadIdentifier is not needed for identification
				VisualizationConfig.PayloadIdentifier = "Not needed";
				// 3. PayloadId
				VisualizationConfig.PayloadId = visualizationParams.at(1).toInt();
				// 4. ImageType is not needed for identification
				VisualizationConfig.ImageType = 0;

				// 5. LUTs not yet supported
				VisualizationConfig.UseLUT = false;
				VisualizationConfig.LUTID = 0;
				VisualizationConfig.UseClamping = false;
				ritk::OpenGLRImageControlUnit* cUnit = m_Widget->m_RImageEntity0->GetRImageControlUnit();

				int clampLow = visualizationParams.at(2).toInt();
				int clampHigh = visualizationParams.at(3).toInt();
				if(clampLow != clampHigh)
				{
					VisualizationConfig.UseClamping = true;
					VisualizationConfig.ClampBounds[0] = clampLow;
					VisualizationConfig.ClampBounds[1] = clampHigh;
				}

				bool enableInterpolation = visualizationParams.at(4).toInt();

				bool validFlagsOnly = visualizationParams.at(5).toInt();

				cUnit->SetPayloadConfig(VisualizationConfig, validFlagsOnly, enableInterpolation);
			}
		}	
	}
	if (!strcmp(Name.toStdString().c_str(), "Payload2"))
	{
		if ( !Value.isEmpty() ) 
		{
			QStringList visualizationParams = Value.split(",");
			// 1. Visualization mode
			if(visualizationParams.at(0) == "2D")
				m_Widget->m_VisualizationUnit1->SetRender3D(false);
			else
				m_Widget->m_VisualizationUnit1->SetRender3D(true);

			if(visualizationParams.size()==6)
			{
				ritk::ImagePayloadVisualizationConfig VisualizationConfig;
				VisualizationConfig.ValidFlag = true; // SVEN should be checked ...
				// 2. PayloadIdentifier is not needed for identification
				VisualizationConfig.PayloadIdentifier = "Not needed";
				// 3. PayloadId
				VisualizationConfig.PayloadId = visualizationParams.at(1).toInt();
				// 4. ImageType is not needed for identification
				VisualizationConfig.ImageType = 0;

				// 5. LUTs not yet supported
				VisualizationConfig.UseLUT = false;
				VisualizationConfig.LUTID = 0;
				VisualizationConfig.UseClamping = false;
				ritk::OpenGLRImageControlUnit* cUnit = m_Widget->m_RImageEntity1->GetRImageControlUnit();

				int clampLow = visualizationParams.at(2).toInt();

				int clampHigh = visualizationParams.at(3).toInt();
				if(clampLow != clampHigh)
				{
					VisualizationConfig.UseClamping = true;
					VisualizationConfig.ClampBounds[0] = clampLow;
					VisualizationConfig.ClampBounds[1] = clampHigh;
				}

				bool enableInterpolation = visualizationParams.at(4).toInt();

				bool validFlagsOnly = visualizationParams.at(5).toInt();

				cUnit->SetPayloadConfig(VisualizationConfig, validFlagsOnly, enableInterpolation);
			}
		}	
	}
	if (!strcmp(Name.toStdString().c_str(), "Payload3"))
	{
		if ( !Value.isEmpty() ) 
		{
			QStringList visualizationParams = Value.split(",");
			// 1. Visualization mode
			if(visualizationParams.at(0) == "2D")
				m_Widget->m_VisualizationUnit2->SetRender3D(false);
			else
				m_Widget->m_VisualizationUnit2->SetRender3D(true);

			if(visualizationParams.size()==6)
			{
				ritk::ImagePayloadVisualizationConfig VisualizationConfig;
				VisualizationConfig.ValidFlag = true; // SVEN should be checked ...
				// 2. PayloadIdentifier is not needed for identification
				VisualizationConfig.PayloadIdentifier = "Not needed";
				// 3. PayloadId
				VisualizationConfig.PayloadId = visualizationParams.at(1).toInt();
				// 4. ImageType is not needed for identification
				VisualizationConfig.ImageType = 0;

				// 5. LUTs not yet supported
				VisualizationConfig.UseLUT = false;
				VisualizationConfig.LUTID = 0;
				VisualizationConfig.UseClamping = false;
				ritk::OpenGLRImageControlUnit* cUnit = m_Widget->m_RImageEntity2->GetRImageControlUnit();

				int clampLow = visualizationParams.at(2).toInt();

				int clampHigh = visualizationParams.at(3).toInt();

				if(clampLow != clampHigh)
				{
					VisualizationConfig.UseClamping = true;
					VisualizationConfig.ClampBounds[0] = clampLow;
					VisualizationConfig.ClampBounds[1] = clampHigh;
				}

				bool enableInterpolation = visualizationParams.at(4).toInt();

				bool validFlagsOnly = visualizationParams.at(5).toInt();

				cUnit->SetPayloadConfig(VisualizationConfig, validFlagsOnly, enableInterpolation);
			}
		}	
	}
	if (!strcmp(Name.toStdString().c_str(), "Payload4"))
	{
		if ( !Value.isEmpty() ) 
		{
			QStringList visualizationParams = Value.split(",");
			// 1. Visualization mode
			if(visualizationParams.at(0) == "2D")
				m_Widget->m_VisualizationUnit3->SetRender3D(false);
			else
				m_Widget->m_VisualizationUnit3->SetRender3D(true);

			if(visualizationParams.size()==6)
			{
				ritk::ImagePayloadVisualizationConfig VisualizationConfig;
				VisualizationConfig.ValidFlag = true; // SVEN should be checked ...
				// 2. PayloadIdentifier is not needed for identification
				VisualizationConfig.PayloadIdentifier = "Not needed";
				// 3. PayloadId
				VisualizationConfig.PayloadId = visualizationParams.at(1).toInt();
				// 4. ImageType is not needed for identification
				VisualizationConfig.ImageType = 0;

				// 5. LUTs not yet supported
				VisualizationConfig.UseLUT = false;
				VisualizationConfig.LUTID = 0;
				VisualizationConfig.UseClamping = false;
				ritk::OpenGLRImageControlUnit* cUnit = m_Widget->m_RImageEntity3->GetRImageControlUnit();

				int clampLow = visualizationParams.at(2).toInt();

				int clampHigh = visualizationParams.at(3).toInt();

				if(clampLow != clampHigh)
				{
					VisualizationConfig.UseClamping = true;
					VisualizationConfig.ClampBounds[0] = clampLow;
					VisualizationConfig.ClampBounds[1] = clampHigh;
				}

				bool enableInterpolation = visualizationParams.at(4).toInt();

				bool validFlagsOnly = visualizationParams.at(5).toInt();

				cUnit->SetPayloadConfig(VisualizationConfig, validFlagsOnly, enableInterpolation);
			}
		}	
	}
}