package spaceObject.spaceShip;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;

import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.image.Image;
import spaceObject.SpaceObject;
import spaceObject.projectile.BlueProjectile;
import spaceObject.projectile.Projectile;
import spaceObject.projectile.RedProjectile;

public abstract class SpaceShip extends SpaceObject {

	private final ArrayList<? extends SpaceShip> _targets;
	private Point2D _movePoint2d = new Point2D(0, 0);
	private String _key = Projectile.KEY_RED;
	private double _speed = 1000, _minYPosition = 0, _maxYPosition = 580;
	private int _hitPoints = 1, _points = 0;

	public SpaceShip(Collection<? extends SpaceShip> collection, ArrayList<? extends SpaceShip> targets, Group group,
			Image image, String projectileKey, double height, double width, int hitPoints) {
		super(collection, group);

		setImage(image);
		setFitHeight(height);
		setFitWidth(width);

		_key = projectileKey;
		_targets = targets;
		_hitPoints = hitPoints;
	}

	public void move(double deltaT) {
		setX(Math.max(0, Math.min(620, getX() + _speed * deltaT * _movePoint2d.getX())));
		setY(Math.max(_minYPosition, Math.min(_maxYPosition, getY() + _speed * deltaT * _movePoint2d.getY())));
	}

	public void shoot(Point2D movePoint2d) {
		switch (_key) {
		case Projectile.KEY_RED: {
			try {
				Projectile.projectiles.add(new RedProjectile(this, movePoint2d));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			break;
		}
		case Projectile.KEY_BLUE: {
			try {
				Projectile.projectiles.add(new BlueProjectile(this, movePoint2d));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			break;
		}
		default:
			System.err.println("Unexpected value: " + _key);
		}
	}
	
	public void place(double x, double y) {
		setX(x - getFitWidth() / 2);
		setY(y - getFitHeight() / 2);
	}

	public int getHitPoints() {
		return _hitPoints;
	}

	public void setHitPoints(int hitPoints) {
		_hitPoints = hitPoints;
	}

	public Point2D getMovePoint2d() {
		return _movePoint2d;
	}

	public void setMovePoint2d(Point2D movePoint2d) {
		_movePoint2d = movePoint2d;
	}

	public ArrayList<? extends SpaceShip> getTargets() {
		return _targets;
	}

	public void setSpeed(double speed) {
		_speed = speed;
	}

	public int getPoints() {
		return _points;
	}

	public void setPoints(int _points) {
		this._points = _points;
	}

	public void setMinYPosition(double _minYPosition) {
		this._minYPosition = _minYPosition;
	}

	public void setMaxYPosition(double _maxYPosition) {
		this._maxYPosition = _maxYPosition;
	}
}
