import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

public class AppleTree  {
    private ArrayList<Point> applePlaces = new ArrayList<Point>();
    private static final int APPLESPERSECOND = 1;
    private Random randomGenerator = new Random();

    public void growApples(DrawTree treePanel) {
	int x, y;
	int xCenter = treePanel.getCenterX();
	int yCenter = treePanel.getCenterY();
	int treeRadius = treePanel.getRadius();
		
	// this is called every second.  add the right nbr of apples
	for (int i=0;i<APPLESPERSECOND;i++) {
	    Point p = new Point();
	    Point center = new Point(xCenter,yCenter);
	    // while point is the rectangular space outside the circle
	    // choose another point.
	    // that is, point must be within the tree's radius
	    // from the center of the circle
	    while (p.distance(center) > treeRadius) {
		x = randomGenerator.nextInt(treeRadius*2)
		    + treePanel.getTreeX() 
		    - treePanel.getAppleWidth()/2; 
		y = randomGenerator.nextInt(treeRadius*2)
		    + treePanel.getAppleWidth()/2;
		p = new Point(x, y);
	    }
	    add(p);
	    // line below prints where the apple was added.
	    // can be checked later to make sure all adds are in arraylist
	    // System.out.println("point added " + p.toString());
	}
	// show the newly added apples on the tree
	treePanel.repaint();
			
    }
	
    private void add(Point p) {
	applePlaces.add(p);
    }
	
    private Point remove() {
	if (applePlaces.size() > 0)
	{
	    int index = randomGenerator.nextInt(applePlaces.size());
	    Point p = new Point(applePlaces.get(index));
	    applePlaces.remove(index);
	    return p;
	}
	else
	    return null;
    }
	
	
    // only one object at a time can go in here
    public void pickApples(DrawTree treePanel) {
	Point p = remove();
	if (p != null) 
	    treePanel.erase(p);
				 
    }
    public int getTreeSize() {
	return applePlaces.size();
    }
	
	
    public Point getApple(int index) {
	if ((index < applePlaces.size()) && (index >= 0)) 
	    return applePlaces.get(index);
	else
	    return new Point(0,0);
    }	
	
    public String toString() {
	String s = "";
	for (Point p : applePlaces) {
	    s += "(" + p.x + ", " + p.y + "); ";
	}
	return s;
    }
	

}
