#include "testApp.h"


//--------------------------------------------------------------
void testApp::setup(){	 
	
	
	//initializeQuicktime();
	ofSetFrameRate(60);
	
	rotSmooth 		= 0;
	angleTarget 	= 0;
	
	bRideLine 		= true;
	bAdjustRotation	= true;
	
	ofHideCursor();
	
	
	nBoxes = 0;
	
	
	LM[nBoxes] = new graphicsLineModule();
	((graphicsLineModule *)LM[nBoxes++])->module = new gravityModule();
	LM[nBoxes] = new graphicsLineModule();
	((graphicsLineModule *)LM[nBoxes++])->module = new sinWaveModule();
	LM[nBoxes] = new graphicsLineModule();
	((graphicsLineModule *)LM[nBoxes++])->module = new oldMaedaModule();
	LM[nBoxes] = new graphicsLineModule();
	((graphicsLineModule *)LM[nBoxes++])->module = new loopModule();
	LM[nBoxes] = new graphicsLineModule();
	((graphicsLineModule *)LM[nBoxes++])->module = new lineWaveModule();
	LM[nBoxes] = new graphicsLineModule();
	((graphicsLineModule *)LM[nBoxes++])->module = new lineWalkModule();
	
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("brunch2.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("espalda_NEW.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("flowerline.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("flowerline_long.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("hand.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("laser001.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("laser002.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("laser003.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("laser004.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("line3.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("linea_verde.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("linie5.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("OFFF_03.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("OFFF_05.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("pink_paddle.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("pyongyang.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("Reimbow.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("saltline.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("sandline.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("sgr_offf.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("spermyLine.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("Sven_line.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("veggie1.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("veggie2.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("SANY0009.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("SANY0061.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("SANY0071.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("ag_line_web.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("chris_line0.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("chris_line1.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("coffeeline.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("colorful_line1.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("domino_1.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("eg_puppet.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("flugzeug_1.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("LIN03.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("LIN04.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("LIN05.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("LIN08.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("line.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("line2.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("line4.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("line5.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("line_2.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("LINEA.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("lineDV.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("lineRider.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("lineRider2.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("lines_.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("Lines_bw.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("ManaloLine.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("mia_line_take_only_this_one.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("mo.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("Mun15.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("Mun16.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("Mun17.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("nintendo-ds.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("phl.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("rad_1.mov");
	
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("rad_1.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("rad_2.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("rad_3.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("rad_1.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("rasen_1.mov");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("SnowTracks_a_1.mov");
			
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("manBike.MOV");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("VCLP0204.MOV");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("VCLP0210.MOV");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("VCLP0215.MOV");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("VCLP0237.MOV");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("VCLP0242.MOV");
	
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("admissionLine.MOV");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("closeRockLine.MOV");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("closeRockLine2.MOV");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("stickDirtLine.MOV");
	LM[nBoxes] = new videoLineModule();
	((videoLineModule *)LM[nBoxes++])->setMovie("traingleRockLine.MOV");

	//LM[nBoxes] = new graphicsLineModule();
	//((graphicsLineModule *)LM[nBoxes++])->module = new handDrawnModule();

	
	orderShuffled = new int[nBoxes];
	for (int i = 0; i < nBoxes; i++){
		orderShuffled[i] = i;
	}
	
	for (int i=0; i<(nBoxes-1); i++) {
    	int r = i + (rand() % (nBoxes-i)); // random remaining position.
        int temp = orderShuffled[i]; orderShuffled[i] = orderShuffled[r]; orderShuffled[r] = temp;
    }
	
	moduleCounter = 0;
	
	
	for (int i = 0; i < nBoxes; i++){
		LM[i]->setup();
	}
	
	currentModule = orderShuffled[moduleCounter];
	prevModule = -1;
	LM[currentModule]->box->position.set(500,500,0);
	LM[currentModule]->reset();
	
}

//--------------------------------------------------------------
void testApp::update(){
	
	ofBackground(0,0,0);	
	
	LM[currentModule]->scrubSpeed = 1;
	LM[currentModule]->movieSpeed = 1;
	LM[currentModule]->update();
	
	if (LM[currentModule]->bAmIDone() == true){
		
		moduleCounter++;
		moduleCounter %= nBoxes;
		int newModule = orderShuffled[moduleCounter];
		
		LM[newModule]->reset();
		LM[newModule]->box->matchMyStartToPointAngle(LR.getCurrentPoint(), LR.getCurrentAngle());
		prevModule = currentModule;
		currentModule = newModule;
		LM[currentModule]->update();
	}
	
	
	if (LM[currentModule]->bHaveNewPoint()){
		LR.addPoint(LM[currentModule]->getNewPoint());
	}
	
	
}


//--------------------------------------------------------------
void testApp::draw(){
	
	//----------------------------------------- 
	// offscreen
	// pbuffer would be better, I think...
	
	bool bDidIDrawOffscreen = false;
	
	// (a) draw anything
	if (LM[currentModule]->bNeedToDrawOffscreen()){
		LM[currentModule]->drawOffscreen();
		bDidIDrawOffscreen = true;
	}	
	
	if (prevModule != -1){
		if (LM[prevModule]->bNeedToDrawOffscreen()){
			LM[prevModule]->drawOffscreen();
			bDidIDrawOffscreen = true;
		}	
	}
	
	// (b) clear
	if (bDidIDrawOffscreen == true){
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	}
	//----------------------------------------- 
	
	ofVec3f pos;
	float rot;
	pos.set(0,0,0);
	
	if (bRideLine){
		if (LR.nPts>0){
			pos = LR.getCurrentPoint();
		}
	} else {
		pos = LM[currentModule]->box->position;
	}
	rot = -LM[currentModule]->box->rotation;
	
	if (rot != rotationOfBox){
		float diffAngle = rot - rotationOfBox;
		while(diffAngle < -PI) 	diffAngle += TWO_PI;
		while(diffAngle > PI) 	diffAngle -= TWO_PI;
		angleTarget += diffAngle;
		while(angleTarget < -PI) 	angleTarget += TWO_PI;
		while(angleTarget > PI) 	angleTarget -= TWO_PI;
	}
	
	rotationOfBox 	= rot;
	posSmooth 		= (0.97) * posSmooth + (1-(0.97)) * pos; 
	rotSmooth 		= (0.97) * rotSmooth + (1-(0.97)) * ((1.0f)*angleTarget);

	

	glPushMatrix();
		glTranslatef(1024/2,768/2, 0);
		glRotatef(rotSmooth * (180/PI), 0,0,1);
		glScalef(1 + 0.25f * 1.0f, 1 + 0.25f * 1.0f, 1);
		
		if (prevModule != -1){
			LM[prevModule]->draw(posSmooth.x, posSmooth.y);
		}
		LM[currentModule]->draw(posSmooth.x, posSmooth.y);
		
		//LR.draw(posSmooth.x, posSmooth.y);
		
	glPopMatrix();
	
	

}


//--------------------------------------------------------------
void testApp::keyPressed  (int key){ 
	
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
	
}

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

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	
}

//--------------------------------------------------------------
void testApp::mouseReleased(){

}
