//
//  HumanPlayer.java
//  cribbage
//
//  Created by Nathan Haug on Tue Apr 06 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

public class HumanPlayer extends Player {

	public HumanPlayer (String name,int playerID) {
		super(name,playerID);
	}
	
	public void addScore(int pointsScored) {
		this.setScore(getScore() + pointsScored);
	}

	public void putCard(Card aCard) {
		this.getHand().putCard(aCard);
	}

	public int getDiscardCard (Cribbage gameManager) {
		// The player must decide which card is best to discard to the crib
		Hand myHand = this.getHand();
		int discardNumber;
		
		System.out.println("\nPlease choose a discard:");
		
		/* TUI Contents:
		EasyIn easy = new EasyIn();

		for (int i = 0; i < myHand.numCards(); i++) {
			System.out.println("Card "+(i+1)+": "+myHand.getCard(i).display());
		}
		
		// Get a valid number from the user
		discardNumber = 0;
		
		boolean valid = false;
		while (!valid) {
			System.out.print("Which card would you like to discard? ");
			discardNumber = easy.readInt();
			
			if (discardNumber <= myHand.numCards() && (discardNumber > 0))
				valid=true;
			else
				System.out.print("You have entered an invalid card...");
		}
		
		discardNumber--;
		*/
		
		// Lets do a GUI solution
		// Tell the gameManager to chill out, the HandPanel class will return a card for us
		discardNumber = gameManager.waitForSelection();
		
		System.out.println(this.getName()+" discards the "+getHand().getCard(discardNumber).display()+".");
		
		// Now we'll also make our copy of the this.getHand() for "The Show" later
		Hand myPeggingHand = new Hand(this.getHand());
		setPeggingHand(myPeggingHand);
		getPeggingHand().deleteCard(discardNumber);
		
		return discardNumber;
		
	}
	
	public int getPeggingCard (CurrentPlay cardsInPlay, Cribbage gameManager) {
		// The player chooses the card he wishes to play in pegging
		// if he has no playable cards, -1 is returned
		
		Hand myPeggingHand = this.getPeggingHand();
		Card peggingCard;
		int remainingPoints;
		int pointsScored;
		int pegCardRank;
		int peggingNumber;
		
		// See if the human has any playable cards
		if (myPeggingHand.numCards() == 0) {
			return -1;
		}
			
		
		remainingPoints = (31 - cardsInPlay.getTotal());		
		for (int i=0; i < myPeggingHand.numCards(); i++) {
			// Change royalty into 10
			pegCardRank = myPeggingHand.getCard(i).getRank();
			if (pegCardRank >= 10)
				pegCardRank = 10;
			
			if (pegCardRank <= remainingPoints) {
				break;
			}
			if (i == myPeggingHand.numCards()-1) { // Then we have gone through all of them
				System.out.println("You do not have any playable cards. Click to continue.");		
				return -1;
			}
		}
		
		System.out.println("\nPlease choose a pegging card:");
		
		/* TUI Contents:
		EasyIn easy = new EasyIn();
		
		for (int i = 0; i < myPeggingHand.numCards(); i++) {
			System.out.println("Card "+(i+1)+": "+myPeggingHand.getCard(i).display());
		}
		
		// Get a valid number from the user
		peggingNumber = 0;
		
		boolean valid = false;
		while (!valid) {
		
			// Get a card from the user
			System.out.print("Which card would you like to play? ");
			peggingNumber = easy.readInt();
			
			// Check to see if the card was valid			
			if (peggingNumber <= myPeggingHand.numCards() && peggingNumber > 0) {
				pegCardRank = myPeggingHand.getCard(peggingNumber-1).getRank();
				if (pegCardRank >= 10)
					pegCardRank = 10;
				
				if (pegCardRank <= remainingPoints)		
					valid=true;
				}
			else
				System.out.print("You have entered an invalid card...");
		}
		
		peggingNumber--;
		*/
		
		// Lets do a GUI solution
		// Tell the gameManager to chill out, the HandPanel class will return a card for us
		boolean invalidCard = true;
		peggingNumber = 0;
		
		while (invalidCard) {
			peggingNumber = gameManager.waitForSelection();
			pegCardRank = getPeggingHand().getCard(peggingNumber).getRank();
			if (pegCardRank > 10)
				pegCardRank = 10;
			if (pegCardRank <= remainingPoints)
				invalidCard = false;
		}
		
		System.out.println(this.getName()+" plays the "+getHand().getCard(peggingNumber).display()+".");
		
		return peggingNumber;
		
	}

}
