//Example for COP 3330
//Uses PlayerScore objects
//keps a list of them

import java.util.*;

public class PlayerScoreList {

	//class variables:
	private static Scanner stdIn = new Scanner(System.in);
	
	//instance variables:
	ArrayList <PlayerScore> pScores;//holds the playerScores
	
	public PlayerScoreList(){
		//creates an empty scorelist object: initializes variables
		this.pScores = new ArrayList<PlayerScore>();
	}
	
	//////////////////////////////////////////////////////
	//instance methods:
	
	private void sort(){
		//calls an Collections algorithm to sort the array
		//notice this is private: only used within the object - not
		//called from the outside
		Collections.sort(pScores);
		//an ArrayList is an AbstractCollection, but not a "Collections": so Collections must be used as the class name
	}
	
	private PlayerScore getPlayerScore (int i){
		//get a PLayerScore based on it's index
		if (i < pScores.size()){
			return pScores.get(i);
		}
		return null;
	}
	
	public PlayerScore getPlayerScore (String name){
		//get a PlayerScore based on a name
		for (PlayerScore ps: pScores){
			if (ps.getName().equals(name))
				return ps;
		}
		return null;
	}
	
	public void newPlayerScore(String name, int score){
		//creates a new playerscore object, and adds it to the list
		PlayerScore ps = new PlayerScore(name, score);
		pScores.add(ps);
		//sort when done
		this.sort();
	}
	
	public boolean changeScore(String name, int newScore){
		//changes the given player's score to newScore	
		PlayerScore ps = this.getPlayerScore(name);
		if (ps != null){
			ps.setScore(newScore);
			this.sort();//incase order changes
			return true;
		}
		return false; //if can't find player
	}
	
	public PlayerScore getTopPlayerScore(){
		//returns the top score as a playerscore object		
		return null;
	}
	
	public int getTopScore(){
		//returns an int of the top score
		return 0;
	}
	
	public String toString(){
		return pScores.toString();
	}
	
	////////////////////////////////////////////
	//static methods:
	
	public static void main(String [] args){
		//test method
		PlayerScoreList psl = new PlayerScoreList();//create an object
		
		psl.newPlayerScore(getNameFromUser(), getScoreFromUser());
		psl.newPlayerScore(getNameFromUser(), getScoreFromUser());
		psl.newPlayerScore(getNameFromUser(), getScoreFromUser());
		
		System.out.println(psl);
	}
	
	//other methods:
	public static String getNameFromUser(){
		//promp user for a name
		System.out.print("Enter the player name: ");
		String nm = PlayerScoreList.stdIn.nextLine();
		return nm;
	}
	public static int getScoreFromUser(){
		//prompts user for a score and clear input stream
		System.out.print("Enter score: ");
		int sc = PlayerScoreList.stdIn.nextInt();
		PlayerScoreList.stdIn.nextLine();//clears input
		return sc;
	}
}
