package edu.stuy.starlorn.entities;

import edu.stuy.starlorn.upgrades.GunUpgrade;

import java.io.IOException;
import java.util.LinkedList;

import org.lwjgl.input.Keyboard;

import org.lwjgl.opengl.GL11;

public class Ship extends Entity {
    protected LinkedList<GunUpgrade> _gunupgrades;
    protected int _baseDamage, _baseShotSpeed, _health, _fullCooldown, _cooldown, _cooldownRate, _movementSpeed;
    protected double _baseAim;
    //protected Texture _texture;

    public Ship() {
        super();
        _gunupgrades = new LinkedList<GunUpgrade>();
        _baseDamage = 1;
        _baseShotSpeed = 1;
        _health = 10;
        _baseAim = 0; //Aim up by default
        _cooldown = 10;
        _fullCooldown = _cooldown+1;
        _cooldownRate = 1;
        _movementSpeed = 1;
        /*
		try {
			_texture = TextureLoader.getTexture("PNG",
					ResourceLoader.getResourceAsStream("res/spaceship.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
        */
    }

	public void draw() {
		render();
	}

	public void render() {
		int x = 1;
		int y = 2;
		if (Keyboard.isKeyDown(Keyboard.KEY_UP)){
			y = 1;
			_ycor--;
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
			x = 2;
			_ycor++;
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)){
			y = 0;
			_ycor++;
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
			x = 0;
			_xcor--;
		}
		
	}


    public void addUpgrade(GunUpgrade upgrade) {
        _gunupgrades.add(upgrade);
    }

    public boolean isHit(Bullet b) {
        return (b.getX() + b.getXvel() + b.getWidth() > _xcor + _xvel &&
                b.getX() + b.getXvel() < _xcor + _width + _xvel &&
                b.getY() + b.getYvel() + b.getHeight() > _ycor + _yvel &&
                b.getY() + b.getYvel() < _ycor + _height + _yvel);
    }
	
	/*
	 * Create the shots based on the available GunUpgrades
	 */
	public void shoot() {
		GunUpgrade topShot = _gunupgrades.get(0);
		int damage = _baseDamage;
		int shotSpeed = _baseShotSpeed;
		for (GunUpgrade up : _gunupgrades) {
			if (up.getNumShots() > topShot.getNumShots())
				topShot = up;
			damage = up.getDamage(damage);
			shotSpeed = up.getShotSpeed(shotSpeed);
		}
		// Create new shots, based on dem vars
		int numShots = topShot.getNumShots();
		for (int i = 0; i < numShots; i++) {
			Bullet b = new Bullet(_baseAim + topShot.getAimAngle(), damage,
					shotSpeed);
			b.setWorld(this.getWorld());
		}
	}
    @Override
    public void step() {
        //Only cooldown if we're below the rate, otherwise the ship hasn't tried to shoot
        if (_fullCooldown <= _cooldown) {
            if (_fullCooldown < 0) {
                this.shoot();
                _fullCooldown = _cooldown+1;
            } else {
                _fullCooldown -= _cooldownRate;
            }
        }
        super.step();
    }

}

@@ -343,28 +343,29 @@
 ealth, _
-fullC
+c
 ooldown
+Timer
 , _coold
@@ -431,16 +431,55 @@
 aseAim;%0A
+    protected boolean _shootRequested;%0A
     //pr
@@ -749,34 +749,25 @@
    _
-fullC
+c
 ooldown
- = _cooldown+1
+Timer = 0
 ;%0A  
@@ -811,32 +811,65 @@
 ementSpeed = 1;%0A
+        _shootRequested = false;%0A
         /*%0A%09%09try
@@ -2640,54 +2640,28 @@
  %3C= 
-_cooldown) %7B%0A            if (_fullCooldown %3C 0
+0 && _shootRequested
 ) %7B%0A
@@ -2672,20 +2672,16 @@
         
-    
 this.sho
@@ -2698,33 +2698,30 @@
         
-    _fullC
+_c
 ooldown
+Timer
  = _cool
@@ -2728,10 +2728,8 @@
 down
-+1
 ;%0A  
@@ -2734,20 +2734,16 @@
         
-    
 %7D else %7B
@@ -2759,25 +2759,22 @@
     
-  
-  _fullC
+_c
 ooldown
+Timer
  -= 
@@ -2792,22 +2792,8 @@
 te;%0A
-            %7D%0A
     

Refactor cooldown code in Ship