package breakout;

import java.awt.Color;
import java.awt.Graphics; 
import java.awt.Point;
import java.awt.Dimension;
import java.util.Vector;

/*holds all instances of balls in a vector
 *ScreenCanvas's paintFrame() calls this paintballs method
 *creates instances of all the balls*/
public class BallRepository  
{
	private ScreenCanvas screen;
	private BrickRepository brickrepos;
	public Vector ballvect;
	private int playerCount;
	
	public int numberOfMyBalls = 0;
	public int numberOfGhostBalls = 0;
	
	public Updater updates; 
	
	//constructor
	public BallRepository (ScreenCanvas screen, BrickRepository brickrepos)
	{
		this.screen = screen;
		this.updates = new Updater(this.screen);
		this.brickrepos = brickrepos;
		this.ballvect = new Vector();
	} //end of constructor	
	
	
	
	//makes the first ball to be displayed
	public void makeInitialBall()
	{	
		double xVel = -((Math.random() + 1) * 3);
		double yVel = -((Math.random() + 1) * 3);
		int centerX = (this.screen.xMin+this.screen.xMax) / 2;
		int centerY = this.screen.yMax - 32;
		
		ClassicBall initialBall = new ClassicBall (this.screen, this.brickrepos, 
													this, centerX, centerY,xVel, 
													yVel, Constants.KEEP_BALL);
		this.addBall (initialBall);  //adds initial ball to the repository
	}
	
	//if a Brick100 is a multiBallTag DEL_BALL, makes 2 additionall balls which 
	//are deleted if hit bottom of screen  
	public void makeMultiBalls (ClassicBall boball)
	{
		int i;
		for (i=1; i<3; i++)
		{	
			//velocities are random but in same direction as ball in which they originated
			double newXvel = (-boball.xVel)*(Math.random()+1);
			double newYvel = (-boball.yVel)*(Math.random()+1);
			
			//creates instances of a multiball with center being the same as that 
			//of the ball which it originated
			ClassicBall manyball = new ClassicBall (this.screen, this.brickrepos,
												  this, boball.center.x, boball.center.y, 
												  newXvel, newYvel, Constants.DEL_BALL);
				
			this.addBall(manyball); //adds the newly created manyball to the ball vector
		}	
	}//end of makeMultiBalls method
	
	
	//creates an instance of a ghostball (balls of other players)
	public void makeGhostBall (int centerX, int centerY)
	{
		GhostBall ghost = new GhostBall(this.screen, this.brickrepos, this, 
										 centerX, centerY, Constants.KEEP_BALL);
		this.addBall(ghost);
	} 
	
	
	//adds a ball to the end of the ball vector
	public void addBall(BreakOutBall newball)
	{	
		ballvect.addElement(newball);
	}
	
	
	//deletes an instance of a ball from the ball vector
	//only called to delete balls with a DEL_BALL tag
	public void deleteBall (BreakOutBall ball)
	{
		try {
			boolean inVect = ballvect.removeElement (ball);
			this.screen.repaint();
		}
		catch (ArrayIndexOutOfBoundsException outBoundExcep) {
			System.out.println("Invalid index for adding brick to repository");
		}
	}
	
	
	
	//called by ScreenCanvas's paintFrame() method
	//calls the paint methods of each of the balls in the ball vector
	public void paintBalls (Graphics g)
	{   
		//System.out.println("Begin of paintballs");
		int i=0;
		BreakOutBall o;
		
		//depends  on number of players' field from screenCanvas to determine
		// whether paintclassic is called before paintghost or vice versa..
		
		for (i=0; i<ballvect.size(); i++) {
			o = (BreakOutBall)ballvect.get(i);
			
			if (this.screen.playerID == 1)
			{
				if ( o instanceof GhostBall)
				{
					((GhostBall)o).paintGhostBall(g);
				}
				
				
				if( o instanceof ClassicBall)
				{  
					((ClassicBall)o).paintClassicBall(g);
				}
			
			}// end of if player 1 loop
			
			else if (this.screen.playerID == 2)
			{
				if( o instanceof ClassicBall)
				{  
					((ClassicBall)o).paintClassicBall(g);
				}
					
				if ( o instanceof GhostBall)
				{
					((GhostBall)o).paintGhostBall(g);
				}
					
			}// end of if player 2 loop
		
		}// end of for loop going through vector
	
	}	//end of paintballs
	
	
}