using System;
using System.Collections.Generic;
using TheGameExtreme.model.card;
using TheGameExtreme.model.deck;
using TheGameExtreme.model.gameActions.abstractRules;
using TheGameExtreme.model.gameActions.classic;
using TheGameExtreme.model.piles;

namespace TheGameExtreme.model.gameActions.decimals
{
    public class GameModeDecimal : GameMode
    {
        public GameModeDecimal(Piles piles, Deck deck) : base(piles, deck)
        {
        }

        override public void load(int nbPlayer, List<Player> players)
        {
            gameActions.Add(new PiocherClassic(Piles));
            gameActions.Add(new JouerUneCarteDecimal(Piles));
            gameActions.Add(new TerminerSonTourDecimal(Piles));

            defineNbMaxCard(nbPlayer);
            distribueCard(players);
        }

        override public void pioche(List<Card> currentHand, Player player)
        {
            Message = null;
            ((PiocherClassic)gameActions[0]).pioche(currentHand, deck, player, nbMaxCard);
            quickSort(currentHand, 0, currentHand.Count - 1);
        }

        override public bool playCard(decimal valueCard, List<Card> currentHand, int orderedStackSelected, Player player, List<Card> CurrentCardPlayed)
        {
            Message = null;
            if (((JouerUneCarteDecimal)gameActions[1]).play(valueCard, currentHand, orderedStackSelected, player, CurrentCardPlayed))
            {
                return true;
            }
            else
            {
                if (!end)
                {
                    Message = ((JouerUneCarteDecimal)gameActions[1]).ErrorMessage;
                }
                return false;
            }
        }

        override public bool endTurn(List<Card> currentHand, List<Card> CurrentCardPlayed, Player player)
        {
            Message = null;
            if (((TerminerSonTourDecimal)gameActions[2]).end(currentHand, CurrentCardPlayed))
            {
                pioche(currentHand, player);
                CurrentCardPlayed.Clear();
                OnPlayerChanged(null);
                return end;
            }
            else
            {
                Message = ((TerminerSonTourDecimal)gameActions[2]).ErrorMessage;
                return false;
            }
        }

        override public void TestEndGame(List<Card> currentHand)
        {
            if (((TerminerSonTourDecimal)gameActions[2]).Test(currentHand))
            {
                end = false;
            }
            else
            {
                OnEndGame(new EventArgs());
                end = true;
            }
        }
    }
}
