#include "testApp.h"





//--------------------------------------------------------------
void testApp::setup(){	
	
	ofSetVerticalSync(true);
	ofSetFrameRate(60);
	ofBackground(0, 0,0);
	water=false;
	count=0;
	for (int i = 0; i < 50; i++){
		particle myParticle;
		myParticle.setInitialCondition(ofRandom(320,500),ofRandom(100,ofGetHeight()-100),0,0);
		particles.push_back(myParticle);
		//arr[i]=false;
	}
	for (int j = 0; j < 50; j++){
		particle myParticle2;
		myParticle2.setInitialCondition(ofRandom(320,500),ofRandom(100,ofGetHeight()-100),0,0);
		particles2.push_back(myParticle2);
	}
	
	VF.setupField(60,40,ofGetWidth(), ofGetHeight());
	
	
	#ifdef _USE_LIVE_VIDEO
		vidGrabber.setVerbose(true);
		vidGrabber.initGrabber(320,240);
	#else
		vidPlayer.loadMovie("fingers.mov");
		vidPlayer.play();
	#endif
		
    colorImg.allocate(320,240);
	grayImage.allocate(320,240);
	grayBg.allocate(320,240);
	grayDiff.allocate(320,240);
	grayDiffSmall.allocate(60,40);
	
	bLearnBakground = true;
	threshold = 30;
	bDrawDiagnostic = true;
	bForceInward	= false; // do we push the particles away...
	
	count=0;
	
	mol1.loadImage("mol1.png");
	mol2.loadImage("mol2.png");
	
	back.loadImage("images.jpg");
	
	
}

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

	
	bool bNewFrame = false;
	
	#ifdef _USE_LIVE_VIDEO
		vidGrabber.grabFrame();
		bNewFrame = vidGrabber.isFrameNew();
	#else
		vidPlayer.idleMovie();
		bNewFrame = vidPlayer.isFrameNew();
	#endif
	
	if (bNewFrame){
		
		#ifdef _USE_LIVE_VIDEO
				colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
		#else
				colorImg.setFromPixels(vidPlayer.getPixels(), 320,240);
		#endif
		
        grayImage = colorImg;
		if (bLearnBakground == true){
			grayBg = grayImage;		// the = sign copys the pixels from grayImage into grayBg (operator overloading)
			bLearnBakground = false;
		}
		
		// take the abs value of the difference between background and incoming and then threshold:
		grayDiff.absDiff(grayBg, grayImage);
		grayDiff.threshold(threshold);
		grayDiffSmall.scaleIntoMe(grayDiff);
		grayDiffSmall.blur(5); // really blur the image alot!
		VF.setFromPixels(grayDiffSmall.getPixels(), bForceInward, 0.05f);
	}
	
	
	
	// on every frame 
	// we reset the forces
	// add in any forces on the particle
	// perfom damping and
	// then update
	
	
	for (int i = 0; i < particles.size(); i++){
		for(int j=0;j<200;j++){
		if(!arr[j]){
			
	 
	
		particles[i].resetForce();
		// get the force from the vector field: 
		ofxVec2f frc;
		frc = VF.getForceFromPos(particles[i].pos.x, particles[i].pos.y);
		particles[i].addForce(frc.x, frc.y);
		particles[i].addDampingForce();
		particles[i].bounceOffWalls();
		particles[i].update();
		particles2[i].resetForce();
		// get the force from the vector field: 
		ofxVec2f frc2;
		frc2 = VF.getForceFromPos(particles2[i].pos.x, particles2[i].pos.y);
		particles2[i].addForce(frc2.x/2, frc2.y/2);
		particles2[i].addDampingForce();
		particles2[i].bounceOffWalls();
		particles2[i].update();
			
		
		
		}
		else{
			printf("ahhhhhhhhhhhhhhh \n");
			
			//ofSetColor(200, 200, 200);
			
			particles[j].pos.x=-100;
			particles[j].pos.y=-100;
			// get the force from the vector field: 
			//ofxVec2f frc;
			//frc = VF.getForceFromPos(particles[i].pos.x, particles[i].pos.y);
			//particles[i].addForce(frc.x*0, frc.y*0);
			//particles[i].addDampingForce();
		//	particles[i].update();
			
	//	particles2[i].resetForce();

			//ofxVec2f frc2;
			//frc2 = VF.getForceFromPos(particles2[i].pos.x, particles2[i].pos.y);
			//particles2[i].addForce(frc2.x*0, frc2.y*0);
			//particles2[i].addDampingForce();
		//	particles2[i].update();
			//ofSetColor(255, 255, 255);
			//ofCircle(particles[i].pos.x, particles[i].pos.x, 300);
			
			
			
		}
	}
				
	}
	
	

}

//--------------------------------------------------------------
void testApp::draw(){
	
		
		ofEnableAlphaBlending();
		ofSetColor(255,255,255, 50);
		colorImg.draw(0,0,ofGetWidth(), ofGetHeight());	
		ofSetColor(0,130,130, 200);
//	VF.draw();
		ofSetColor(255,0,0);
		
	ofEnableAlphaBlending();
	ofSetColor(255,255,255);
	back.draw(0, 0, ofGetWidth(), ofGetHeight());

		for (int i = 0; i < particles.size(); i++){
			particles[i].draw2(&mol1);
		}
		//ofSetColor(0,255,0);
		for (int i = 0; i < particles2.size(); i++){
			particles2[i].draw2(&mol2);
		}
				
	ofDisableAlphaBlending();
	
	
	for (int i = 0; i < particles.size(); i++){
		for (int j = 0; j < particles2.size(); j++){
			
			
			if(((int)(particles[i].pos.x)) ==  ((int)(particles2[j].pos.x))  && ((int)(particles[i].pos.y)) ==  ((int)(particles2[j].pos.y)))
				{
				
				water=true;
				count2++;
					//			
			//ofSetColor(0, 0, 0);
				//ofRectangle(,50,50,50);
			//	ofSetColor(0, 0, 0);
				
			//ofDrawBitmapString("CO", 50, 50);
				particles[i].pos.x=ofRandom(0,2);
				particles[i].pos.y=ofRandom(0,2);
				particles2[i].pos.x=ofRandom(2,4);
				particles2[i].pos.y=ofRandom(2,4);
				arr[i]=true;
					
					
					
					//ofRect(180,ofGetHeight()-150,700,200);
				}
			else{
				
				arr[i]=false;	
				}
			
			}
		}
	
			
		
	
	
	//ofSetColor(0,130,130, 200);
//	ofRect(10,550,330,130);
//	ofSetColor(0,0,0);
//	ofDrawBitmapString("press ' ' (space) to snap background\npress 'd' to toggle diagnostic\ndrag mouse to add particles\npress 'r' to reset particles\npress 't' to toggle force direction", 20, 600);
//	
}

//--------------------------------------------------------------
void testApp::keyPressed  (int key){ 
	
	if (key == ' ' ){
		bLearnBakground = true;
	} else if (key == 'd'){	
		bDrawDiagnostic = !bDrawDiagnostic;
	} else if (key == 'r'){
		for (int i = 0; i < particles.size(); i++){
			particles[i].setInitialCondition(ofRandom(0,ofGetWidth()), ofRandom(0,ofGetHeight()), 0,0);
			particles2[i].setInitialCondition(ofRandom(0,ofGetWidth()), ofRandom(0,ofGetHeight()), 0,0);

		}
	} else if (key == 't'){
		bForceInward = !bForceInward;
	}
}

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

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

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
	//particles.erase(particles.begin());
//	particle pt;
//	pt.setInitialCondition(x, y, 0, 0);
//	particles.push_back(pt);
}

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

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