import processing.core.*; 
import processing.xml.*; 

import fisica.*; 

import java.applet.*; 
import java.awt.Dimension; 
import java.awt.Frame; 
import java.awt.event.MouseEvent; 
import java.awt.event.KeyEvent; 
import java.awt.event.FocusEvent; 
import java.awt.Image; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 
import java.util.regex.*; 

public class kef2_6_help extends PApplet {

/* @pjs preload="background.jpg,background_win.jpg,wood_tex1.jpg,wood_tex2.jpg,wood_tex3.jpg,wood_tex4.jpg,red_wand.png,green_wand.png,blue_wand.png" */



FWorld world;
FPoly poly;
FBox box,boxLeft,boxRight,boxObstacleLeft,boxObstacleRight;
FCircle circle;
FPoly basket1,basket2,basket3,basket4;
int cx,cy,col,radius,status;
boolean createBubble;
PImage imgBackground, imgBackground_win, imgWood, imgWood2,imgWood3,imgWood4,blueWand,greenWand,redWand;


public void setup() {

  size(800, 550);
  smooth();

  imgBackground = loadImage("background.jpg");
  imgBackground_win = loadImage("background_win.jpg");  
  imgWood = loadImage("wood_tex1.jpg");
  imgWood2 = loadImage("wood_tex2.jpg");
  imgWood3 = loadImage("wood_tex3.jpg");
  imgWood4 = loadImage("wood_tex4.jpg");
  blueWand = loadImage("blue_wand.png");
  redWand = loadImage("red_wand.png");
  greenWand = loadImage("green_wand.png");
 

  Fisica.init(this);

  world = new FWorld();
  world.setGravity(0, -100);
  world.setEdges();   
  world.remove(world.top);
  world.remove(world.bottom);
  world.remove(world.left);
  world.remove(world.right);
  world.setEdgesRestitution(0.5f);
  createBubble = false;

  basket1 = new FPoly();
  basket1.vertex(10,0);
  basket1.vertex(70,0);
  basket1.vertex(70,10);
  basket1.vertex(10,10);
  basket1.setStatic(true);
  basket1.setFill(195,92,17);  
  world.add(basket1);

  basket3 = new FPoly();
  basket3.vertex(70,0);
  basket3.vertex(80,0);
  basket3.vertex(80,80);
  basket3.vertex(70,80);
  basket3.setStatic(true);
  basket3.setFill(195,92,17); 
  world.add(basket3);

  basket2 = new FPoly();
  basket2.vertex(710+20,0);
  basket2.vertex(710+80,0);
  basket2.vertex(710+80,10);
  basket2.vertex(710+20,10);
  basket2.setStatic(true);
  basket2.setFill(195,92,17);  
  world.add(basket2);

  basket4 = new FPoly();
  basket4.vertex(710+10,0);
  basket4.vertex(710+20,0);
  basket4.vertex(710+20,80);
  basket4.vertex(710+10,80);
  basket4.setStatic(true);
  basket4.setFill(195,92,17); 
  world.add(basket4);

  box = new FBox(500,30);
  box.setPosition(400,200);  
  box.attachImage(imgWood);
  box.setGrabbable(false);  
  world.add(box);

  circle = new FCircle(5);
  circle.setPosition(400,200);
  circle.setStaticBody(true);
  circle.setGrabbable(false);
  world.add(circle);

  FRevoluteJoint f = new FRevoluteJoint(box,circle,400,200);    
  f.setCollideConnected(false);
  world.add(f);

  boxLeft = new FBox(10,500);
  boxLeft.setPosition(5,250);
  boxLeft.setGrabbable(false);
  boxLeft.setStaticBody(true);
  boxLeft.attachImage(imgWood2);
  world.add(boxLeft);

  boxRight = new FBox(10,500);
  boxRight.setPosition(795,250);
  boxRight.setGrabbable(false);
  boxRight.setStaticBody(true);
  boxRight.attachImage(imgWood2);
  world.add(boxRight);

  boxObstacleLeft = new FBox(150,30);
  boxObstacleLeft.setPosition(75,515);
  boxObstacleLeft.setGrabbable(false);
  boxObstacleLeft.setStaticBody(true);
  boxObstacleLeft.attachImage(imgWood3);
  world.add(boxObstacleLeft);

  boxObstacleRight = new FBox(150,30);
  boxObstacleRight.setPosition(725,515);
  boxObstacleRight.setGrabbable(false);
  boxObstacleRight.setStaticBody(true);
  boxObstacleRight.attachImage(imgWood3);
  world.add(boxObstacleRight);

  noCursor();
  col = 1;
  status = 0;

}


public void draw() {


	

  if(status == 0) {
	image(imgBackground,0,0);
  } else {	
	image(imgBackground_win,0,0);	
  }  	

  if(createBubble == true) {
	stroke(200);
    strokeWeight(1);    
	switch(col) {
		case 1:
			fill(250,0,0,150);
		break;
		case 2:
			fill(0,250,0,150);
		break;
		case 3:
			fill(0,0,250,150);
		break;
	}
	radius = abs(cx-mouseX);
	if(radius > 60) radius = 60;
        if(radius < 2) radius = 10;
	ellipse(cx,cy,radius,radius);
  }

  world.step();
  world.draw(this);    



	if(mouseY > 550) {
		cursor(ARROW);
	}
	else {
		noCursor();
	
		switch(col) {
			case 1:
				image(redWand,mouseX-15,mouseY-20);
			break;
			case 2:
				image(greenWand,mouseX-15,mouseY-20);
			break;
			case 3:
				image(blueWand,mouseX-15,mouseY-20);
			break;
		}
	}

}


public void mousePressed() {

	if(mouseY < 550 && mouseY > 450 && mouseX > 150 && mouseX < 650 && createBubble == false && status == 0) {	
		createBubble = true;	
		cx = mouseX;
		cy = mouseY;	
	}

	if(mouseY > 550 && mouseY < 600 && status == 1) {
		setup();		
	}

}


public void mouseDragged() {
	
}

public void mouseReleased() {
  	
	if(createBubble == true) {
		switch(col) {
			case 1:
				createBlob(cx,cy,250,0,0,1);
			break;
			case 2:
				createBlob(cx,cy,0,250,0,10);
			break;
			case 3:
				createBlob(cx,cy,0,0,250,100);
			break;
		}
	}
}


public void createBlob(int x, int y,int r, int g, int b,float d) {
	FBlob bl = new FBlob();
    bl.setAsCircle(x,y,radius,30);	
    bl.setStroke(200);
    bl.setStrokeWeight(1);
    bl.setFill(r,g,b,150);	
	bl.setDensity(d);	
	world.add(bl);
	createBubble = false;
}


public void Win() {
	//boxLeft.setStaticBody(false);
	//boxRight.setStaticBody(false);
	//boxObstacleLeft.setStaticBody(false);
	//boxObstacleRight.setStaticBody(false);
	//basket1.setStatic(false);
	//basket2.setStatic(false);
	//circle.setStaticBody(false);
	//world.remove(boxLeft);
	//world.remove(boxRight);
	//world.remove(boxObstacleLeft);
	//world.remove(boxObstacleRight);	
}



public void contactEnded(FContact c) {
  FBody bubble = null;

  if(status == 0) {
	  if ((c.getBody1() == basket1 || c.getBody1() == basket2) && (c.getBody2() != boxLeft && c.getBody2() != boxRight && c.getBody2() != boxObstacleLeft && c.getBody2() != boxObstacleRight && c.getBody2() != circle))  
	  {
		bubble = c.getBody2();
	  } 
  

	  if ((c.getBody2() == basket1 || c.getBody2() == basket2) && (c.getBody1() != boxLeft && c.getBody1() != boxRight && c.getBody1() != boxObstacleLeft && c.getBody1() != boxObstacleRight && c.getBody1() != circle))  
	  {
		bubble = c.getBody1();
	  } 


	  if (bubble == null) {
		return;
	  }
	  else
	  {
		status = 1;	
		Win();
	  }
  }
      
}



public void keyPressed() {

	if(key == 'r') {
		col = 1;		
	}
	if(key == 'g') {
		col = 2;		
	}
	if(key == 'b') {
		col = 3;		
	}	

}
  static public void main(String args[]) {
    PApplet.main(new String[] { "--bgcolor=#F0F0F0", "kef2_6_help" });
  }
}
