import java.util.ArrayList;



public class BigTwo implements CardGame {
    private int numOfPlayers ;
    private Deck deck ;
    private ArrayList<CardGamePlayer> playerList= new ArrayList<CardGamePlayer>();
    private ArrayList<Hand> handsOnTable= new ArrayList<Hand>();
    private int currentPlayerIdx;
    private BigTwoUI ui;
    
    BigTwo(){
        //i) create 4 players and add them to the player list; and 
        //(ii) create a BigTwoUI object for providing the user interface.
        for (int i = 0; i < 4; i++) {
            //
            playerList.add(new CardGamePlayer());
        }
        ui = new BigTwoUI(this);


    }
    public static void main(String[] args){
        // It should (i)
        //create a Big Two card game, (ii) create and shuffle a deck of cards, and (iii) start the
        //game with the deck of cards.
        BigTwo game= new BigTwo();
        BigTwoDeck deck= new BigTwoDeck();
        deck.shuffle();
        game.start(deck);
    }
    
 
/**
 * Returns a valid hand from the specified list of cards of the player.
 * Returns null if no valid hand can be composed.
 *
 * @param player The player attempting the hand.
 * @param cards The cards proposed for forming a hand.
 * @return The valid hand or null if the hand is invalid.
 */

public static Hand composeHand(CardGamePlayer player, CardList cards) {
    if (cards.size() == 1) {
        Hand single = new Single(player, cards);
        if (single.isValid()) return single;
    } else if (cards.size() == 2) {
        Hand pair = new Pair(player, cards);
        if (pair.isValid()) return pair;
    } else if (cards.size() == 3) {
        Hand triple = new Triple(player, cards);
        if (triple.isValid()) return triple;
    } else if (cards.size() == 5) {
        Hand straightFlush = new StraightFlush(player, cards);
        if (straightFlush.isValid()) return straightFlush;

        Hand quad = new Quad(player, cards);
        if (quad.isValid()) return quad;

        Hand fullHouse = new FullHouse(player, cards);
        if (fullHouse.isValid()) return fullHouse;

        Hand flush = new Flush(player, cards);
        if (flush.isValid()) return flush;

        Hand straight = new Straight(player, cards);
        if (straight.isValid()) return straight;
    }
    return null; // No valid hand
}


    public int getNumOfPlayers(){
        return numOfPlayers;
    }
    public Deck getDeck(){
        return deck;
    }
    public ArrayList<CardGamePlayer> getPlayerList(){
        return playerList;
    }
    public ArrayList<Hand> getHandsOnTable(){
        return handsOnTable;
    }
    public int getCurrentPlayerIdx(){
        return currentPlayerIdx;
    }

    public void start(Deck deck ){
        //i remove all the cards from the players as well a from the table;
        this.deck=(BigTwoDeck) deck;
        for (CardGamePlayer player : playerList){
            player.removeAllCards();
        }
        handsOnTable.clear();
        deck.shuffle();

        // ii) distribute the cards to the players
        // we first loop through each player and for each player we give them 13 cards from the deck
        for (CardGamePlayer player: playerList){
            for(int i=0; i<13;i++){
                player.addCard(deck.getCard(0));
            }
        }
        // we need to sort the cards in the hand of each player
        for (CardGamePlayer player: playerList){
            player.getCardsInHand().sort();
        }
        //iii) finds out the player witht the three of diamonds and make him the current player idx and the active player
        for (CardGamePlayer player: playerList){
            // for each player
            // we uss the getCard method to get the player card on hand 
            // we then loop through the fetched cardslist and see whether any of them match the rank and suit of the three of diamonds
            CardList cardsOnHand= player.getCardsInHand();
            if (cardsOnHand.contains(new BigTwoCard(0,2))){// three of diamond to see whether it contains the card
                currentPlayerIdx= playerList.indexOf(player);
                ui.setActivePlayer(currentPlayerIdx);
                break;
            }
        }
        //call the repaint() method of the BigTwoUI object to show thecards on the table; and 
        ui.repaint();

        //(vi) call the promptActivePlayer() method of the BigTwoUI object to prompt user to select cards a
        ui.promptActivePlayer();// this will call the make move method afterwards 

    }

    public void makeMove(int playerIdx, int[] cardIdx){
        checkMove(playerIdx, cardIdx);
    }

    public void checkMove(int playerIdx, int[] cardIdx){

        CardGamePlayer currentP= playerList.get(playerIdx);
        CardList cardsInHand= currentP.getCardsInHand();
        CardList selectedCards = new CardList();
        

        for (int i : cardIdx){
            selectedCards.addCard(cardsInHand.getCard(i));
        }

        Hand compHand= composeHand(currentP, selectedCards);
        if (compHand!=null){
            // if there are no hands on table meaning it is the first move and if they have no 3 of diamonds 
            if(handsOnTable.size()==0 && ! selectedCards.contains(new BigTwoCard(0,2))){
                System.err.println("Not a legal move!!!");
                // prompt them again 
                ui.promptActivePlayer();
            }
            // else if this does not beat the last player's hand of card 
            Hand lastRoundHand = handsOnTable.get(handsOnTable.size()-1);
            else if (!(compHand.beats(lastRoundHand)))

        } where is the error in comphand