package poker;

import java.awt.Image;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import javax.imageio.ImageIO;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class Poker {
	
	public static void main(String[] args) {

	BasicConfigurator.configure();
	
	readIcons();	
	
	boolean shuffled = true;
	getCards(shuffled);
	
	Player playerOne = new Player("Player One");
	playerOne.getCardFromDealer(getTopCard());
	playerOne.getCardFromDealer(getTopCard());
	System.out.println(playerOne);
	System.out.println(playerOne.hashCode());

	
	Player playerTwo = new Player("Player Two");
	playerTwo.getCardFromDealer(getTopCard());
	playerTwo.getCardFromDealer(getTopCard());
	_log.debug(playerTwo + ".");
	_log.debug(playerTwo.hashCode());
	
	Player playerThree = new Player("Player Three");
	playerThree.getCardFromDealer(getTopCard());
	playerThree.getCardFromDealer(getTopCard());
	System.out.println(playerThree + ".");
	
	Player playerFour = new Player("Player Four");
	playerFour.getCardFromDealer(getTopCard());
	playerFour.getCardFromDealer(getTopCard());
	System.out.println(playerFour + ".");
	
	Player playerFive = new Player("Player Five");
	playerFive.getCardFromDealer(getTopCard());
	playerFive.getCardFromDealer(getTopCard());
	System.out.println(playerFive + ".");
	
	for (int i = 0; i < deckOfCards.size(); i++) { System.out.println(deckOfCards.get(i));}
	}		
	private static void getCards (boolean _shuffled) {
		short iCount = 0;
		for (short suit = 1; suit <= MAX_SUITS_IN_DECK; suit++){
			for (short rank = 1; rank <= MAX_RANK_IN_DECK; rank++) {
				deckOfCards.add(new Card(suit, rank));
				iCount++;
			}}
		
		if (_shuffled == true){Collections.shuffle(deckOfCards, new Random());}
		
	}


	private static Card getTopCard() {
		Card _cardRemoved = deckOfCards.get(1);
		deckOfCards.remove(1);
		return _cardRemoved;
	}
	
	private static void readIcons(){

    	String _filename;

    	//read icons
    	Image _image = null;
    	File _file = null;
    	InputStream _is = null;
    	for (int i = 1; i < MAX_CARDS_IN_DECK + 1; i++) {
    	try {

    		/* Security information: filenames should not be altered manually by an Administrator, and,
    		 should be available within the same directory where the source code runs. */

    			if (i < 10) {_filename = "0" + Integer.toString(i);}
    			else {_filename = Integer.toString(i);}

    			String _temp = _filename;
    			_filename = _temp + ".GIF";

    			//TODO: Relative path might change when implementing?
    			_filename = System.getProperty("user.dir") + "\\img\\" + _filename;

    		_file = new File(_filename);

    		//read from an input stream
    		_is = new BufferedInputStream (new FileInputStream(_filename));

    		_image = ImageIO.read(_is);
    		if (_file.exists()) {
    		CardIcons.add(_image);
    		_log.debug(_filename + " loaded.");
    		}
    	}
    	catch (IOException e) { _log.debug(e.getMessage()); }
    	}
    }
	
	private static ArrayList<Image> CardIcons = new ArrayList<Image>();
	private static ArrayList<Card> deckOfCards = new ArrayList<Card>();
	private static final Logger _log = Logger.getLogger(Poker.class);
	
	public static final short MAX_CARDS_IN_DECK = 52;
	public static final short MAX_SUITS_IN_DECK = 4;
	public static final short MAX_RANK_IN_DECK = 13;
	public static final short MAX_PLAYERS_PER_TABLE = 5;
	public static final short MAX_GAMES_PER_SESSION = 1; 			//TODO: implement concurrent games and UI at a later stage.
	
	/* References
	 * ----------
	 * Information about poker at http://en.wikipedia.org/wiki/Poker.
	 * List of poker hands at http://en.wikipedia.org/wiki/Poker_hands.
	 * Rules for Texas Hold'Em at http://en.wikipedia.org/wiki/Texas_hold_'em.  This will be implemented at a later stage.
	 * Steve Badger, How to Play Poker at http://www.playwinningpoker.com/articles/how-to-play-poker.
	 * Graphics to be implemented at a later stage.
	 */
}

