package ludumdare.game;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.Queue;

import org.crusty.math.Vec2double;
import org.crusty.math.Vec2int;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

public class Player extends Entity {

	
	boolean alive = true;
	Queue<Vec2double> trail = new LinkedList<Vec2double>();
	long lastTimeTrail = 0;
	long timeCrashed;
	long timeWon = -1;
	
	
	public Player(Sprite sprite) {
		super(sprite);
		
	}
	
	public void logic(double dt) {
		
		if (timeWon != -1) {
			if (System.currentTimeMillis() > timeCrashed + 3000) {
				MessageManager.changeAndIncMessage("You win!");
			}
			return;
		}
		
		if (!alive) {
			if (System.currentTimeMillis() > timeCrashed + 3000) {
				ScreenManager.goToScreen(new Vec2int(0, 0));
				alive = true;
				vel.x = 0;
				vel.y = 0;
				acc.x = 0;
				acc.y = 0;
				rotation = 0;
			}
			return;
		}
		
		acc.x = 0;
		acc.y = 0;
		if (Game.keys[KeyEvent.VK_SPACE] || Game.keys[KeyEvent.VK_W]) { 
			
			//this.acc.y = -0.1;
			Vec2double up = new Vec2double(0, 1);
			up = up.rotate(new Vec2double(0, 0), rotation);
			
			acc.x += up.x;
			acc.y += up.y;
			
			Vec2double firePos = new Vec2double(pos.x + rotatePoint.x - 10, pos.y + rotatePoint.y - 10);
			ParticleManager.addParticle(1, SpriteManager.getSprite("jet.png"), firePos, "capsuleJet", acc);
			
		}
		if (Game.keys[KeyEvent.VK_A]) {
			rotation -= 0.01f * dt/1000000;
		}
		if (Game.keys[KeyEvent.VK_D]) {
			rotation += 0.01f * dt/1000000;
		}
		
		// Trail
		if (System.currentTimeMillis() > lastTimeTrail + 300) {
			trail.poll();
			while (trail.size() < 100)
				trail.add(pos.clone().add(new Vec2double(9, 9)));
		}
		
		// Collision with objects
		
		
		// Planet gravity
//		for (int i = 0; i < Game.entities.size(); i++) {
		Object[] ents = Game.entities.toArray();
		for (int i = 0; i < ents.length; i++) {
			if (ents[i] instanceof SpaceBody) {
				
				Vec2double pos = ((SpaceBody) ents[i]).pos.add(new Vec2double(((SpaceBody) ents[i]).diameter/2, 
																			((SpaceBody) ents[i]).diameter/2));
				Vec2double vector = pos.sub(this.pos.add(new Vec2double(9, 9)));
				Vec2double dir = vector.normalise();
				double length = vector.length();
				
				this.acc.x += dir.x * 1/length * 100;
				this.acc.y += dir.y * 1/length * 100;
				
				if (pos.sub(this.pos.add(new Vec2double(9, 9))).length() < ((SpaceBody) ents[i]).diameter/2) {
//					if (!(((SpaceBody) ents[i]) instanceof BlackHole)) {
					
					if ((((SpaceBody) ents[i]) instanceof BlackHole)) {
						if (((BlackHole) ((SpaceBody) ents[i])).wormhole) {
								
							ParticleManager.addParticle(20, SpriteManager.getSprite("wormhole.png"), pos.clone().add(new Vec2double(10, 10)), "wormhole");
							
							timeWon = System.currentTimeMillis();
							
							InputStream in;
							try {
								in = new FileInputStream("wormhole.wav");
								AudioStream as = new AudioStream(in);       
								AudioPlayer.player.start(as);
							} catch (FileNotFoundException e) {
								e.printStackTrace();
							} catch (IOException e) {
								e.printStackTrace();
							}
							
							return;
						}
					}
					
					// Crash
					alive = false;
					timeCrashed = System.currentTimeMillis();
					
					if (((SpaceBody) ents[i]) instanceof Star) {
						InputStream in;
						try {
							in = new FileInputStream("suncrash.wav");
							AudioStream as = new AudioStream(in);       
							AudioPlayer.player.start(as);
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					} else {
						InputStream in;
						try {
							in = new FileInputStream("crash.wav");
							AudioStream as = new AudioStream(in);       
							AudioPlayer.player.start(as);
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					
					ParticleManager.addParticle(10, SpriteManager.getSprite("fire.png"), pos.clone().add(new Vec2double(10, 10)), "crash");
					ParticleManager.addParticle(5, SpriteManager.getSprite("smoke.png"), pos.clone().add(new Vec2double(10, 10)), "crash");
//					} else {
//						
//					}
				}
			}
		}
		
		super.logic(dt);
		
		if (pos.x > 500 || pos.x < 0 || pos.y > 500 || pos.y < 0)
			ScreenManager.notifyBorderCross(pos.clone());
	}
	
	public void draw(Graphics2D g) {
		Object[] trailArray = trail.toArray();
		
		for (int i = 0; i < trailArray.length - 1; i++) {
			Vec2double v1 = (Vec2double) trailArray[i];
			Vec2double v2 = (Vec2double) trailArray[i + 1];
			g.setColor(new Color(255, 255, 255, 10 + i));
			if (v1.sub(v2).length() < 50)
				g.drawLine((int) v1.x, (int) v1.y, (int) v2.x, (int) v2.y);
		}
		
		super.draw(g);
	}
	
}
