package breakout;

import java.awt.event.*;
import java.awt.*;
import cs101.io.*;				// Necessary  import statements
import cs101.awt.*;
import cs101.net.*;
import java.lang.*;
import java.util.Vector;
import java.io.*;

public class ScrollArea extends TextArea implements Runnable
{
	//necessary fields for ScrollArea
	private boolean isScroll;
	private Vector linesVect;
	private String line=null;
	private int numberStrings = 15;
	
	//constructor which sets properties. Contains the string that is used on the introduction of the game. 
	public ScrollArea()
	{
		
		setBackground(Color.black);
		setForeground(Color.cyan);
		this.setFont(new Font("Serif",Font.BOLD|Font.ITALIC, 28));
		//String which is the introduction to the game		
		String s="\n" +
				"                WELCOME TO BREAKOUT!!!!! \n"+
				"First, understand the story that is behind this great game...\n" +
				"Once upon a time, long long ago, there lived a race of Balls \n"+
		 		"The Ball people were very peaceful and never had problems. \n "+
		 		"Until one day....The one day, when the Bricks came...and turned \n"+
		 		"the Balls' peaceful world upside down!!!The Bricks took over the \n"+
		 		"Balls and made them their slaves.Only one ball managed to \n"+
		 		"escape...his name was Katakana...he was just your average ball.  \n"+
		 		"But now in order to save his people, he has to put up a huge  \n"+
		 		"fight. One day, when Katakana was trying to make sense of his  \n"+
		 		"predicament,he stumbled over a Paddle. The Paddles had also  \n"+
		 		"been conquered by the Bricks. The Paddle decided to help Katakana  \n"+ 
		 		"in his effort. Your mission...help Katakana and Paddle defeat the \n"+
		 		"BRICKS, free the Balls and help them regain their land.\n"+
		 		"                     GOOD LUCK!!!!!! \n\n\n\n\n\n\n\n";
		
		this.linesVect= new Vector();//creation of a new Vector
		BufferedReader br= new BufferedReader(new StringReader(s));//new BufferedReader, takes a StringReader for args
																  //(to read the one String)
		
		
		if (this.line ==null)//when the String is null
		try
		{
			this.line=br.readLine();//reading the first line of the String
			System.out.println("Reading the first line '" + line + "'");
			//this.linesVect.addElement();
		}
		catch(IOException e)//catches IOException 
		{
		    System.out.println("I caught an IOException at ScrollArea constructor");
			this.line=null;
		}
					
		if(this.line != null)//when the String is not null
		{ int i;
			for(i=0; i<numberStrings; i++)//when i is less than the number of lines of the String
			{ 
				try
				{
					this.line=br.readLine();//read the next lines
					System.out.println("reading the next lines '" + line + "'");
					this.linesVect.addElement(this.line);//add the elements to the screen. 
				}
				catch(IOException e)
				{
					System.out.println("I caught an IOException at ScrollArea constructor");
					this.line=null;
					//this.stopScrolling();
				}
			}
		}
		setText("\n");//giving some space before the text repeats
		
	}
			
	





	//method which starts the scrolling of the text
	public void startScrolling()
	{
		if(!isScroll)
		{
			Thread runner=new Thread(this);
			isScroll=true;
			runner.start();//starting the method
		}
	}
	
	
	//method which ends the scrolling
	public void stopScrolling()
	{
		//Thread runner=new Thread(this);
		isScroll=false;
		//runner.stop();
	}

	
	
	
	//run method, called over again
	public void run()
	{
		int index=0;
		while(isScroll)//while the text is scrolling
		{
			int times;
			for(times = 0; times < 3; times++)
			{
				index= 0;
				int i;
				for (i=0; i<(this.linesVect.size()); i++)
				{
					//System.out.println("in while loop...");
					//adds the string until it has repeated 3 times. 
					this.insert((String)linesVect.elementAt(index) + "\n", this.getText().length()-1);
					index++;
				
					try
					{
						Thread.sleep(2000);//amount of time given before next text appears
					}
					
					catch(InterruptedException e) 
					{
					System.out.println("I caught an InterruptedException at run method");
					}
				}			
			
			
			
			
			
			}
				if(times == 4)//if the text has scrolled more than 3 times, it stops
				{
					this.stopScrolling();
				}	
				
				//System.out.println("in if statement..");
				index=0;
				setText("\n\n\n\n\n\n\n\n\n");
			
			
		}
	
	}
	
	
	/**public void paint(Graphics g)
	{
		int x;
		int y;
		setColor(Color.green);
		g.fillRect(x, y, 800, 700);
	}
	**/

}

	
   