#include "sceneManager.h"

//----------------------------	
sceneManager::sceneManager(){
	currentCards.clear();
	allCards.clear();
	scenes.clear();
	whichScene = 0;
}

				
//----------------------------
void sceneManager::setup(int camW, int camH, int screenW, int screenH){
	ofKeyEvents.addListener(this);

	scenes.push_back( (baseCardScene*)&bouncer );
	scenes.push_back( (baseCardScene*)&sonar );
	scenes.push_back( (baseCardScene*)&colisioner );

	scenes.push_back( (baseCardScene*)&tester );
	scenes.push_back( (baseCardScene*)&arPlaneGui );

	//should always be the last scene	
	scenes.push_back( (baseCardScene*)&diagnostic );
	
	//now setup the scenes
	for(int i = 0; i < scenes.size(); i++){
		//should always be the last scene
		scenes[i]->setSceneInfo(camW, camH, screenW, screenH);
		scenes[i]->setDebug(true);
	}
	
	
	//we set the plane size to be 30cm
	//so you need to use a piece of paper that is 30cm square to calibrate.
	//the bigger the size of the paper the more accurate the results 
	singlePlane.setup(camW, camH, 300, true);
	
	//tell all our scenes about plane
	for(int i = 0; i < scenes.size(); i++){
		scenes[i]->setPlanePtr( &singlePlane );
	}
	
	whichScene = 0;
	soloScene(whichScene);
	
	//update our plane from xml
	arPlaneGui.updatePlane();
}
	
		
//---------------------------
void sceneManager::keyPressed(int key){
	
	if( scenes.size() > 0 ){
		if( key == OF_KEY_RIGHT){
			whichScene++;
			whichScene %= scenes.size();
			soloScene(whichScene);			
		}
		if( key == OF_KEY_LEFT){
			whichScene--;
			if(whichScene < 0) whichScene += scenes.size();
			soloScene(whichScene);
		}
		if( key == 'd'){
			toggleDiagnoisticScene();
		}
	}
	
}		
		
//---------------------------
void sceneManager::keyReleased(int key){

}
		
//----------------------------
void sceneManager::passCurrentCards(vector <card> cardsIn){
	currentCards.clear();
	currentCards = cardsIn;
	
	for(int i = 0; i < scenes.size(); i++){
		scenes[i]->passCurrentCards(currentCards);
	}
}

//----------------------------
void sceneManager::passAllCards(vector <card> cardsIn){
	allCards.clear();
	allCards = cardsIn;
	
	for(int i = 0; i < scenes.size(); i++){
		scenes[i]->passAllCards(allCards);
	}
}

//----------------------------
void sceneManager::showScene(int theScene){
	if( theScene < 0 || theScene >= scenes.size() )return;
	whichScene = theScene;
	scenes[whichScene]->setHide(false);
}	

//----------------------------
void sceneManager::hideScene(int theScene){
	if( theScene < 0 || theScene >= scenes.size() )return;
	whichScene = theScene;
	scenes[whichScene]->setHide(true);
}
		
//----------------------------
void sceneManager::pauseScene(int theScene){
	if( theScene < 0 || theScene >= scenes.size() )return;
	whichScene = theScene;
	scenes[whichScene]->setPause(true);
}	

//----------------------------
void sceneManager::playScene(int theScene){
	if( theScene < 0 || theScene >= scenes.size() )return;
	whichScene = theScene;
	scenes[whichScene]->setPause(false);
}
	
//----------------------------
void sceneManager::soloScene(int theScene){
	if( theScene < 0 || theScene >= scenes.size() )return;
	whichScene = theScene;
	
	for(int i = 0; i < scenes.size(); i++){
		if( i == whichScene ){
			scenes[i]->setPause(false);
			scenes[i]->setHide(false);
		}else{
			scenes[i]->setPause(true);
			scenes[i]->setHide(true);
		}
	}
}
	
//----------------------------
void sceneManager::toggleDiagnoisticScene(){
	bool paused = diagnostic.pause;
	bool hidden = diagnostic.hide;
	
	if( paused || hidden ){
		diagnostic.setPause(false);
		diagnostic.setHide(false);
	}else{
		diagnostic.setPause(true);
		diagnostic.setHide(true);	
	}
}		







		
	