import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileReader;

//Jason Kim
//CSE 680 Su06
//Due 8/17/06
//Compile javac BaconNumbers.java
//Java Dependencies Graph.java and BFS.java
//
//6 Degrees of Kevin Bacon:
//Instructions:
//Run from Command Line: java BaconNumbers movies.txt
//Program will prompt you to give an actor name in form of "Last name, First name"
//Program then will connect them to Kevin Bacon

//First creates Graph Object
//Then reads in Movie File and adds movie and names as edges and nodes.
//after graph is built BFS search is run on graph
//program reads input from User and shows shortest path from actor to kevin bacon

//================Main Class Declartion ====================
public class BaconNumbers 
{  
    public static void main(String[] args) 
	{
	
	 //Buffer Reader
	 BufferedReader buffr;
	 //Try To Read File from Command Line
	 try
	 {
	    System.out.println("Jason Kim" + "\n" + "CSE 680" + "\n" + "Summer 2006");
	    System.out.println("Please wait a moment as this may take awhile to load file!");
	 	FileReader fr = new FileReader(args[0]);// file = commandline arguement
	 	buffr = new BufferedReader(fr);// 

	 } catch(IOException ioe){ throw new RuntimeException("Could not open file");}// Could not open file
        

//=============GRAPH Creation and File Stream Read In (see Graph.java)=============================== 
		//Make Graph Nodes
		//Nodes are Actors, Edges are Actors connected by starring in the same movie
		Graph G = new Graph();//declare new graph object
        try
		{
	 		String line;
			while ((line = buffr.readLine()) != null) 
			{ //reads entire file
				String[] names = line.split("/"); //get actor names
				String movie = names[0]; // get movie name
				for (int i = 1; i < names.length; i++) 
					G.addEdge(movie, names[i]);//adds nodes + edges
			}
		} catch(IOException ioe){throw new RuntimeException("Could not Read Line");}//if can't read file line

//============BFS Search (see BFS.java)=============
        //Run BFS (Breadth First Search) on Graph G
        BFS breadth = new BFS(G, "Bacon, Kevin"); //Goal is Kevin Bacon


        
//==============Input from User==================
	 InputStreamReader isr = new InputStreamReader(System.in);
     buffr = new BufferedReader(isr);//input from console
	 try
	 {
			String actor,s1;
			int go=1;
			while (go != 0) 
			{  
				//Ask User to Input Actor Name
				System.out.println("Enter the name of an actor in form of Last Name, First Name (ie- Bacon, Kevin)");
				actor = buffr.readLine(); //readline from console for actor source
				System.out.println(""); // print empty line for neatness
				breadth.showPath(actor);//Show Path of BFS
				System.out.println(actor + "'s Bacon Number is: " + breadth.pathLength(actor)/2);//show bacon number
				System.out.println("Go Again? 1 for yes, 0 for no");//Quit or Keep Going
				s1 = buffr.readLine();
				go = Integer.parseInt(s1);
			}
			
        
	  } catch(IOException ioe){throw new RuntimeException("Could not get line from input");}
    }
}


