#include "testScene.h"
#include "testApp.h"

//---------------------------------------
testScene::testScene(){

}

//---------------------------------------
void testScene::setupScene(){
	sceneName = "my test scene!";
	addAppListener();
	addMouseListener();
	addKeyboardListener();
	setAutoRender(true);
		
	
	fakeMarker.setup(camW, camH, 60, true);
	
	
	mx = 0;
	my = 0;
	
	//this is for setting the points with your mouse
	whichPt		 = 0;
	recordingPts = false;
	
}

//---------------------------------------
void testScene::render(){
	if( hide || pause ) return;
	
	//draw a circle for each card found
	if( planePtr->isMarkerSet()){
		planePtr->loadARMatrix();
		
			ofSetColor(0xFF0000);
		
			/*
			if( currentCards.size() > 0){
				for(int i = 0; i < currentCards.size(); i++){

					fakeMarker.setMarkerPoints(currentCards[i].sqPts[0], currentCards[i].sqPts[1], currentCards[i].sqPts[2], currentCards[i].sqPts[3]);
					if( fakeMarker.isMarkerSet()){
						fakeMarker.loadARMatrix();
						
						
						ofEnableAlphaBlending();
						ofSetColor(255, 255,255);
						
						glPushMatrix();
						int pictureId = currentCards[i].pictureId;
						if (pictureId != -1){
							ofSetRectMode(OF_RECTMODE_CENTER);
							//glScalef(1, -1, 1);
							ofSetColor(255, 255, 255, currentCards[i].energySmoothed*180);
							ofRect(0,0, 45, 88);
							
							for (int j = 0; j < theApp->cardMan.currentlyTrackedImages[pictureId].nPlanes; j++){
								glPushMatrix();
								glTranslatef(0, 0, currentCards[i].energySmoothed*20 * (0.5 + 0.5f*sin(ofGetElapsedTimef()*3)));
								glBlendFunc(GL_DST_COLOR, GL_ZERO);
								theApp->cardMan.currentlyTrackedImages[pictureId].colorImagesPlanes[j].draw(0,-2, 60, 83);
								glPopMatrix();
							}
							
							
							
							ofSetRectMode(OF_RECTMODE_CORNER);
						}
						glPopMatrix();
						ofDisableAlphaBlending();
						
						fakeMarker.unloadARMatrix();
					}	
				}
			}
		*/
		
		if( allCards.size() > 0){
			for(int i = 0; i < allCards.size(); i++){
				
				fakeMarker.setMarkerPoints(allCards[i].sqPts[0], allCards[i].sqPts[1], allCards[i].sqPts[2], allCards[i].sqPts[3]);
				if( fakeMarker.isMarkerSet()){
					fakeMarker.loadARMatrix();
					
					
					ofEnableAlphaBlending();
					ofSetColor(255, 255,255);
					
					glPushMatrix();
					int pictureId = allCards[i].pictureId;
					if (pictureId != -1){
						ofSetRectMode(OF_RECTMODE_CENTER);
						//glScalef(1, -1, 1);
						ofSetColor(255, 255, 255, allCards[i].energySmoothed*180);
						ofRect(0,0, 45, 88);
						
						for (int j = 0; j < theApp->cardMan.currentlyTrackedImages[pictureId].nPlanes; j++){
							glPushMatrix();
							glTranslatef(0, 0, allCards[i].energySmoothed*20 * (0.5 + 0.5f*sin(ofGetElapsedTimef()*3)));
							glBlendFunc(GL_DST_COLOR, GL_ZERO);
							theApp->cardMan.currentlyTrackedImages[pictureId].colorImagesPlanes[j].draw(0,-2, 60, 83);
							glPopMatrix();
						}
						
						
						
						ofSetRectMode(OF_RECTMODE_CORNER);
					}
					glPopMatrix();
					ofDisableAlphaBlending();
					
					fakeMarker.unloadARMatrix();
				}	
			}
		}
			
		/*
			// want to see all the cards! 
			if( allCards.size() > 0){
				for(int i = 0; i < allCards.size(); i++){
					
					
					
					glPushMatrix();
					glScalef(1, -1, 1);
					ofEnableAlphaBlending();
					glTranslatef(0, 0, allCards[i].energySmoothed * 100);
					ofSetColor(255,0,0, allCards[i].energySmoothed * 100);
					ofBeginShape();
					for (int j = 0; j < 4; j++){
						ofPoint pt = planePtr->screenToMarker(allCards[i].c[j].x, allCards[i].c[j].y);
						ofVertex(pt.x, pt.y);
					}
					ofEndShape();
					ofDisableAlphaBlending();
					glPopMatrix();
					
					
					
				}
			}*/
		
		planePtr->unloadARMatrix();
	}
	
	//show the plane points we are marking out
	if(recordingPts){
		ofNoFill();
			ofSetColor(255, 0, 130);
			ofBeginShape();
				for(int i = 0; i < whichPt; i++){
					ofVertex(planePts[i].x, planePts[i].y);
				}
			ofEndShape(false);
			for(int i = 0; i < whichPt; i++){
				ofCircle(planePts[i].x, planePts[i].y, 4);
			}
		ofFill();
	}
	
	//show our debug text
	if(debug){
		//drawTxtMsg("scene is: "+sceneName+" r to record points - s to save settings", 0, camH - 40);
		//drawDebugTxt(0, camH - 20);	
	}

}

//---------------------------------------
void testScene::update(){
	if( pause ) return;

}

//---------------------------------------		
void testScene::keyPressed( int key ){
	if( pause ) return;
}

//---------------------------------------
void testScene::keyReleased( int key ){
	if( pause ) return;

}
	
//---------------------------------------
void testScene::mouseMoved(int x, int y ){
	if( pause ) return;

	mx = x;
	my = y;
}

//---------------------------------------
void testScene::mouseDragged( int x, int y, int button ){
}

//---------------------------------------
void testScene::mousePressed( int x, int y, int button ){
	if( pause ) return;


}

//---------------------------------------
void testScene::mouseReleased(int x, int y, int button ){
	if( pause ) return;

}	

