using System;
using System.Collections.Generic;
using TheGameExtreme.model.card;
using TheGameExtreme.model.card.rapidCard;

namespace TheGameExtreme.model.gameActions.classic
{
    public class TerminerSonTour : GameAction
    {


        public TerminerSonTour(Piles ListOrderedStacks) : base(ListOrderedStacks)
        {
        }

        public bool end(List<Card> CurrentHand, int nbCardAtBeginOfTurn, List<Card> CurrentCardPlayed)
        {
            // Si carte qui fait piocher que d'un, vérifier la bonne pioche (penser si elle vient d'être recouverte)
            // Sinon
            if (nbCardAtBeginOfTurn == CurrentCardPlayed.Count || CurrentHand.Count == 0 || CurrentCardPlayed.Count >= 2)
            {   // Ne pas oublié de gérer les ThreeCard
                return Test(CurrentHand);
            }
            else
            {
                ErrorMessage = AppRessource.StrCardPlayedLessThanTwo;
                return false;
            }
            
        }

        public bool Test(List<Card> CurrentHand)
        {
            if (CurrentHand.Count != 0)
            {
                List<Card> playableCard = new List<Card>();
                tryToFindSoluce(playableCard, CurrentHand);
                return testEndGame(playableCard);
            }
            return false;
        }

        protected void tryToFindSoluce(List<Card> playableCard, List<Card> CurrentHand)
        {
            CurrentHand.ForEach(card =>
            {
                if (card.Value > ListOrderedStacks.getStack(0).Peek().Value || card.Value > ListOrderedStacks.getStack(1).Peek().Value || card.Value < ListOrderedStacks.getStack(2).Peek().Value || card.Value < ListOrderedStacks.getStack(3).Peek().Value)
                {
                    playableCard.Add(card);
                }
            });
        }

        protected bool testEndGame(List<Card> playableCard)
        {
            if (playableCard.Count == 2)
            {
                foreach (Card c in playableCard)
                {
                    if (Equals(c.getName(), ThreeCard.CARD_THREE))
                    {
                        return false;
                    }
                }
            }
            else if (playableCard.Count < 2)
            {
                return false;
            }
            return true;
        }
    }
}
