package server.match.objects;

import java.awt.Color;
import java.awt.geom.Ellipse2D;

import main.Window;

public class Ball {
	private Color color;
	private int size, bounceheight, direction;
	private Ellipse2D.Double bal;

	double rx, ry; // position
	double vx, vy; // velocity

	// Contains the logic of a ball
	public Ball(int size, int bounceheight, Color color, int x, int y, int direction, double velocity) {
		this.color = color;
		this.size = size * 20;
		this.bounceheight = bounceheight;
		this.direction = direction;
		this.vx = direction * 3;
		this.vy = velocity;
		setDirection(direction);
		rx = x;
		ry = y;
		bal = new Ellipse2D.Double(x, y, this.size, this.size);
	}

	// Collision
	public boolean hitLine(ShootingLine l) {
		return bal.intersects(l.getX(), l.getY() - l.getHeight(), l.getWidth(), l.getHeight());
	}

	public boolean hitPlayer(Player p) {
		return bal.intersects(p.getX(), p.getY(), p.getWidth(), p.getHeigth());
	}

	@Override
	public Ball clone() {
		return new Ball(getSize(), getBounceHeight(), getColor(), getX(), getY(), direction, vy);
	}

	// ** Calculating **//

	public void update() {
		if (rx >= (Window.WIDTH - size - 10))
			vx = -3;
		else if (rx <= 10)
			vx = 3;

		vy += 0.5;

		if (ry + size < 600 - bounceheight && vy < 0) {
			vy += 0.5;
		} else if (ry + size < 600 - bounceheight && vy > 0) {
			vy -= 0.2;
		}

		if (ry >= (600 - size))
			vy *= -1;

		rx += vx;
		ry += vy;
		setX((int) rx);
		setY((int) ry);
	}

	// ** Getters and Setters **//

	public int getSize() {
		return size / 20;
	}

	public int getBounceHeight() {
		return bounceheight;
	}

	public int getX() {
		return (int) bal.getX();
	}

	public int getY() {
		return (int) bal.getY();
	}

	public int getWidth() {
		return (int) bal.getWidth();
	}

	public int getHeight() {
		return (int) bal.getHeight();
	}

	public Color getColor() {
		return color;
	}

	public void setX(int x) {
		bal.setFrame(x, getY(), getWidth(), getHeight());
	}

	public void setY(int y) {
		bal.setFrame(getX(), y, getWidth(), getHeight());
	}

	public void setDirection(int d) {
		if (d == -1 || d == 1) {
			direction = d;
			vx = d * 3;
		}
	}

	public double getYSpeed() {
		return vy;
	}

}
