#include "RenderSwitcher.h"

const bool USE_GL_RENDER = true;

ModelViewerApp* RenderSwitcher::getModelViewerApp()
{
	return this->modelViewerApp;
}
MapViewerApp* RenderSwitcher::getMapViewerApp()
{
	return this->mapViewerApp;
}
PlanetViewerApp* RenderSwitcher::getPlanetViewerApp()
{
	return this->planetViewerApp;
}
RenderSwitcher::~RenderSwitcher()
{
	this->mapViewerActive= false;
	this->planetViewerActive = false;
	this->modelViewerActive= false;

	delete this->mapViewerApp;
	delete this->planetViewerApp;
	delete this->modelViewerApp;
	delete this->sharedData;
}
RenderSwitcher::RenderSwitcher( MainWindow &mainWindow, int argc, char **argv, Environment &environment ): mainWindow( mainWindow )
{
	this->sharedData = new OpenGlSharedData (  argv[0], InputSpace::getResourcePath( argv[2] ), environment );

	mapViewerApp = new MapViewerApp( "MapViewerApp", * mainWindow.renderWidgetBig->getContext(), *sharedData );
	mapViewerApp->setResolution( mainWindow.renderWidgetBig->width(), mainWindow.renderWidgetBig->height() );

	planetViewerApp = new PlanetViewerApp( "PlanetViewerApp", * mainWindow.renderWidgetSmall->getContext(), *sharedData );
	planetViewerApp->setResolution(  mainWindow.renderWidgetSmall->width(), mainWindow.renderWidgetSmall->height() );		

	modelViewerApp = new ModelViewerApp( "ModelViewerApp", * mainWindow.renderWidgetSmall->getContext(), *sharedData );
	modelViewerApp->setResolution(  mainWindow.renderWidgetSmall->width(), mainWindow.renderWidgetSmall->height() );

	bool ok = true;


	if ( USE_GL_RENDER )
	{
		ok = ok && QObject::connect( &mainWindow, SIGNAL(showShipsTableSignal()), this, SLOT(activateModelViewerSlot()) );
		ok = ok && QObject::connect( &mainWindow, SIGNAL(showBasesTableSignal()), this, SLOT(activatePlanetViewerSlot()) );
		ok = ok && QObject::connect( &mainWindow, SIGNAL(showSpaceMapSignal()), this, SLOT(activateMapViewerSlot()) );
	}


	assert( ok && "Connection failed!" );
}
void RenderSwitcher::activateMapViewerSlot(void)
{
	mainWindow.renderWidgetBig->setApp( mapViewerApp );
	mainWindow.renderWidgetSmall->setApp( NULL );

	if ( ! this->mainWindow.renderWidgetBig->isInitialized() )
	{
		mainWindow.renderWidgetBig->initializeGL();
	}

	this->modelViewerActive = false;
	this->mapViewerActive	= true;
	this->planetViewerActive= false;
}
void RenderSwitcher::activateModelViewerSlot(void)
{
	mainWindow.renderWidgetBig->setApp( NULL );
	mainWindow.renderWidgetSmall->setApp( this->modelViewerApp );
	mainWindow.renderWidgetSmall->initializeGL();
		
	this->modelViewerActive = true;
	this->mapViewerActive	= false;
	this->planetViewerActive= false;
}
void RenderSwitcher::activatePlanetViewerSlot(void)
{
	mainWindow.renderWidgetBig->setApp( NULL );
	mainWindow.renderWidgetSmall->setApp( this->planetViewerApp );
	mainWindow.renderWidgetSmall->initializeGL();

	this->modelViewerActive = false;
	this->mapViewerActive	= false;
	this->planetViewerActive= true;
}
void RenderSwitcher::render(void)
{
	if ( this->mapViewerActive )
	{
		mainWindow.renderWidgetBig->updateGL();
	}
	
	if ( this->planetViewerActive || this->modelViewerActive )
	{
		mainWindow.renderWidgetSmall->updateGL();
	}
}
void RenderSwitcher::simulate( float dt )
{
	if ( this->modelViewerActive )
	{
		this->modelViewerApp->simulate( dt );
	}

	if ( this->mapViewerActive )
	{
		this->mapViewerApp->simulate( dt );
		//this->mapViewerApp->removePlanetsOlderThan( 500 );
		//this->mapViewerApp->removeShipsOlderThan( 500 );
		//this->mapViewerApp->removeMissilesOlderThan( 500 );
	}

	if ( this->planetViewerActive )
	{
		this->planetViewerApp->simulate( dt );
	}
}


