/**
 * Weight.java
 *
 * Caitlin Ross
 * 100735219
 */
 
import java.util.*;
import java.awt.*;
import java.io.*;

public class Weight {
	
	private int	radius = 10;
	
	private Color color;
    boolean moving = false;
    private double x;
    private double y;
    private Point  location = new Point(0,0);
    private double vx;
    private double vy;
    private boolean selected;
    private boolean isInPlay;

    public Weight(Point aPoint, Color aColor) {
    	color = aColor;
		location = aPoint;
		x = location.getX();
		y = location.getY();
        vx = 0.0;
        vy = 0.0;
        moving = false;
        isInPlay = true;
    }
    
    public Color getColour() {return color;}
	public Point getLocation() {return location;}
	public int getLocationX() {return location.x;}
	public int getLocationY() {return location.y;}
    public int getRadius() {return radius;}
    public boolean isSelected() {return selected;}
    public void setSelected(boolean state) {selected = state;} 
    public void toggleSelected() {selected = !selected;}
    public void setColor(Color newColor) { color = newColor; }
	public void outOfBounds(){isInPlay = false;}
	public boolean isInBounds(){return isInPlay;}
    
    public void setLocation(Point aPoint) { 
	     location.x = aPoint.x;
	     location.y = aPoint.y;
	      x = aPoint.getX(); y = aPoint.getY();
	 }
    
    public void setLocation(int theX, int theY) { 
        location.x = theX;
        location.y = theY;
        x = location.getX(); 
        y = location.getY();
    }
    
    public void setMoving(boolean isMoving) { moving = isMoving; }
    public boolean isMoving() {return moving; }
    
    public double getvx() {return vx;} 
    public double getvy() {return vy;}
    public void setvx( double v ) {vx = v; moving = ((vx != 0.0) || (vy != 0.0)); }
    public void setvy( double v ) {vy = v; moving = ((vx != 0.0) || (vy != 0.0));}
    
    public void stop() { vx=0; vy = 0; moving = false; }
    
    public double velocity() {return Math.sqrt ((vx * vx) + (vy * vy));}
    
	public void advance(int milliseconds, int friction) {
		if(moving) {
			x = x + (vx*milliseconds);
			y = y + (vy*milliseconds);
			location.x = (int) x;
			location.y = (int) y;
  			if(friction == 0){
  				vx = vx*0.7;
  				vy = vy*0.7;
  			}
  			else if(friction ==1){
  				vx = vx*0.8;
  				vy = vy*0.8;
  			}
  			else if (friction == 2){
  				vx = vx*0.9;
  				vy = vy*0.9;
  			}
  			else{
  				vx = vx*0.8;
  				vy = vy*0.8;
  			}
  			
			if( (Math.abs(vx) < 0.12) && (Math.abs(vy) < 0.12) ) {
				moving = false;
				vx = 0.0;
				vy = 0.0;
			}
		}
	}
	private double distanceBetween(Weight n1, Weight n2) {
		return Math.sqrt ((double) ((n2.getLocation().x - n1.getLocation().x)*
			(n2.getLocation().x - n1.getLocation().x) +
			(n2.getLocation().y - n1.getLocation().y) * 
			(n2.getLocation().y - n1.getLocation().y)));
    }
    
    private double distanceBetween(Point p1, Point p2) {
		return Math.sqrt ((double) ((p2.x - p1.x) *  (p2.x - p1.x) +
			(p2.y - p1.y) * 
			(p2.y - p1.y)));
    }
    
	public void drawWith(Graphics aPen) {
		if(isInPlay)
			aPen.setColor(Color.gray);
		else
			aPen.setColor(Color.darkGray);
		aPen.fillOval(location.x - radius, location.y - radius, 
			radius * 2, radius * 2);
		if(isInPlay)
			aPen.setColor(color);
		else
			aPen.setColor(color.darker());
		aPen.fillOval(location.x - radius/2, location.y - radius/2, radius, radius);
		aPen.setColor(Color.black);
		aPen.drawOval(location.x - radius, location.y - radius, 
			radius * 2, radius * 2);
		aPen.drawOval(location.x - radius/2, location.y - radius/2, radius, radius);
	}
	
}