import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class Shades extends Applet implements MouseListener{
	private int uR,uB,uG,score,highScore=0;;
	private Color uColor;
	private ArrayList blocks;
	private Thread t = new Thread();
	private Graphics dbg;
	private Image dbImage;
	private final int WIDTH = 20;
	private final int INC = 20;
	private int level;
	private int x=0,y=0;
	private boolean mDown = false;
	private Font smallFont = new Font ("Arial", Font.BOLD, 18);
	private Font smallerFont = new Font ("Arial", Font.BOLD, 14);
	private AudioClip laser, song;
	
	public void init() {
		setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
		setBackground(Color.lightGray);
		uR=100; uG=100; uB=100;
		uColor = new Color(uR,uG,uB);
		level = 0;
		blocks = new ArrayList();
		laser = getAudioClip (getCodeBase(), "LASER.wav");
		song = getAudioClip (getCodeBase(), "song.mid2");
		song.loop();
		addMouseListener(this);
		newLevel();
	}
	
	public void newLevel()
	{
		level++;
		for (int i = 0; i<level+4; i++)
		{
			int posX = (int)(Math.random()*(500-WIDTH));
			int posY = (int)(Math.random()*(350-WIDTH));
			int sX = ((((int)(Math.random()*2)+1)*2)-3)*(level+3);
			int sY = ((((int)(Math.random()*2)+1)*2)-3)*(level+3);
			blocks.add(new block(posX, posY, sX, sY, (int)(Math.random()*250),(int)(Math.random()*250),(int)(Math.random()*250)));
		}
		repaint();
	}
	
	public void paint(Graphics g) {
		//Back
		g.setColor(Color.red);
		g.fill3DRect(110,355,80,40,true);
		g.setColor(Color.green);
		g.fill3DRect(210,355,80,40,true);
		g.setColor(Color.blue);
		g.fill3DRect(310,355,80,40,true);
		g.setColor(Color.white);
		g.fill3DRect(410,355,80,40,true);
		g.setColor(Color.black);
		g.fill3DRect(10,355,80,40,true);
		
		//blocks
		for (int i = 0; i < blocks.size(); i++)	{
			block temp = (block)blocks.get(i);
			g.setColor(new Color(temp.r,temp.g,temp.b));
			g.fill3DRect(temp.x,temp.y,WIDTH,WIDTH,true); //Draws
			
			temp.x += temp.sX; temp.y +=temp.sY; //moves block
			if (temp.x <=0 || temp.x >= 500-WIDTH)
				 temp.sX*=-1;
			if (temp.y <= 0 || temp.y > 350-WIDTH)
				temp.sY*=-1;
				
		}
		
		//Shooter
		g.setColor(uColor);
		g.fillOval(225,300,50,50);
		if (mDown) {
			g.drawLine(250,325,x,y);
			g.fillOval(x-5,y-5,10,10);
		}
		g.setFont(smallerFont);
		g.drawString("HueHunter! by Eric Tedj", 310,345);
		
		//Runner
		try {t.sleep(50);}catch(InterruptedException e){}
		
		g.setColor(Color.red);
		g.setFont(smallFont);
		g.drawString("Level: " + level, 210, 18);
		
		
		if (blocks.size() == 0)
			newLevel();
		
		repaint();
	}
	public void mousePressed(MouseEvent e) {		
		x = e.getX(); 
	    y = e.getY();
		
		if (y > 350) {
			if (x<100) {
				if (uG>INC)
					uG-=INC;
				if (uB>INC)
					uB-=10;
				if (uR>INC)
					uR-=INC;
			} 
			else 
			if (x<200) 	{
				if (uR>255-INC)	{
					if (uG>INC)
						uG-=INC;
					if (uB>INC)
						uB-=INC;
				}
				else
					uR+=INC;
			}
			else
			if (x<300) 	{
				if (uG>255-INC)	{
					if (uR>INC)
						uR-=INC;
					if (uB>INC)
						uB-=INC;
				}
				else
					uG+=10;
			}
			else
			if (x<400)	{
				if (uB>255-INC)	{
					if (uG>INC)
						uG-=INC;
					if (uR>INC)
						uR-=INC;
				}
				else
					uB+=INC;
			}
			else {
				if (uG<255-INC)
					uG+=INC;
				if (uB<255-INC)
					uB+=INC;
				if (uR<255-INC)
					uR+=INC;
			}
		}
		else	{
			laser.play();
			mDown=true;
			
			for (int i = 0; i < blocks.size(); i++)	{
				block temp = (block)blocks.get(i);
				if (x<temp.x+WIDTH && x>temp.x && y<temp.y+WIDTH && y>temp.y)
				{
					System.out.println(Math.abs(uR-temp.r)+" "+Math.abs(uG-temp.g)+" "+Math.abs(uB-temp.b));
					
					if ((Math.abs(uR-temp.r)<50) && (Math.abs(uG-temp.g)<50) && (Math.abs(uB-temp.b)<50))
					{
						blocks.remove(i);
						score++;
						if (score>highScore)
							highScore=score;
					}
				}
			}
		}
		uColor = new Color(uR,uG,uB);
		repaint();
	}
	public void stop()	{
		song.stop();
		
	}
	public void start()	{
		song.loop();
	}
	
	public void update (Graphics g){
		if (dbImage == null){
			dbImage = createImage (this.getSize().width, this.getSize().height);
			dbg = dbImage.getGraphics ();
		}
		dbg.setColor (getBackground ());
		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
		dbg.setColor (getForeground());
		paint (dbg);
		g.drawImage (dbImage, 0, 0, this);
	}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e) {}
	public void mouseClicked(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {mDown=false;}
}
