package WheelOfFortune.game.wheel;


// Source file: U:\\cse308\\asn8\\src\\game\\wheel\\Wheel.java

import java.util.*;
import java.io.*;



public class Wheel implements Serializable{
	private Vector spaces; //for now, no lifting spaces, so just a Vector is OK
	private int currentIndex;
	private int size;
	public static final String WHEEL = "WheelOfFortune\\game\\wheel.txt";
	/**
	 * @param fileName
	 * @roseuid 418923B001EE
	 */
	public Wheel(Wheel w, int totalSpaces)throws FileNotFoundException, IOException 
	{
		FileReader f = new FileReader(WHEEL);
		BufferedReader br = new BufferedReader(f);
		spaces = new Vector();
		for(int i = 0; i < w.getNumSpaces(); i++)
			spaces.add(w.getSpaceAt(i));
		String s;
		StringTokenizer st;
		Space tempSpace;
		int amount,code;
		
		s = br.readLine();
		String name;

		while(spaces.size() < 12)
		{	
			st = new StringTokenizer(s,",",false);
			name = st.nextToken();
			code = Integer.parseInt(st.nextToken());    
			if(code==Space.NORMAL_SPACE)
			{
				amount = Integer.parseInt(st.nextToken());
				tempSpace = new Space(name,code,amount);
			}
			else
				tempSpace = new Space(name,code,0);
			spaces.add(tempSpace);
			s = br.readLine();
		}
		
		currentIndex = 2;
			
	}
	public Wheel(String fileName, int totalSpaces) throws FileNotFoundException, IOException {
		spaces = new Vector();
		currentIndex = 0;  
		int ct = 0;
		FileReader f = new FileReader(fileName);
		BufferedReader br = new BufferedReader(f);
		
		String s;
		StringTokenizer st;
		Space tempSpace;
		int amount,code;
		
		s = br.readLine();
		String name;
		while(s != null && ct < totalSpaces) {
			st = new StringTokenizer(s,",",false);
			name = st.nextToken();
			code = Integer.parseInt(st.nextToken());    
			if(code==Space.NORMAL_SPACE)
			{
				amount = Integer.parseInt(st.nextToken());
				tempSpace = new Space(name,code,amount);
			}
			else
				tempSpace = new Space(name,code,0);
			spaces.add(tempSpace);
			s = br.readLine();
			ct++;
		}
		currentIndex = 2;
		
		
	}
	
	public Wheel(String fileName) throws FileNotFoundException, IOException {
		spaces = new Vector();
		currentIndex = 0;  
		
		FileReader f = new FileReader(fileName);
		BufferedReader br = new BufferedReader(f);
		
		String s;
		StringTokenizer st;
		Space tempSpace;
		int amount,code;
		
		s = br.readLine();
		String name;
		while(s != null) {
			st = new StringTokenizer(s,",",false);
			name = st.nextToken();
			code = Integer.parseInt(st.nextToken());    
			if(code==Space.NORMAL_SPACE)
			{
				amount = Integer.parseInt(st.nextToken());
				tempSpace = new Space(name,code,amount);
			}
			else
				tempSpace = new Space(name,code,0);
			spaces.add(tempSpace);

			s = br.readLine();
		}
		currentIndex = 2;
	}
	
	public Wheel(Vector v) {
	 spaces = v;
	 currentIndex = 2;

	}
	
	/**
	 * @param int i
	 * @return Space
	 * @roseuid 418923B20381
	 */
	public Space getSpaceAt(int i) {
		if(i >= 0 && i < spaces.size()) return (Space)spaces.elementAt(i);
		return null;
	}
	
	/**
	 * @return int
	 * @roseuid 418923CB0391
	 */
	public int getNumSpaces() {
		return spaces.size();
	}
	
	/**
	 * @return int
	 * @roseuid 418923D30072
	 */
	public int getCurrentIndex() {
		return currentIndex;
	}
	
	/**
	 * @return Space
	 * @roseuid 418923DC02C4
	 */
	public Space getCurrentSpace() {
		return getSpaceAt(currentIndex);
	}
	
	/**
	 * @roseuid 418923E501CC
	 */
	public void liftCurrentSpace() {}
	
	/**
	 * @return Space
	 * @roseuid 418923F00038
	 */
	public int spin() {
		int oldCI = currentIndex;
		int advance = (int)(Math.random() * spaces.size());
		currentIndex = (currentIndex + advance)%spaces.size();		
		return advance+spaces.size();
	}
	
	public String toString() {
		String s = "";
		int i;
		for(i = 0;i < spaces.size() - 1;++i) {
			s = s + spaces.elementAt(i).toString() + "\n";
		}
		s = s + spaces.elementAt(i).toString();
		return s;
	}
}
