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

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

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

        public bool end(List<Card> CurrentHand, 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 (CurrentHand.Count == 0 || CurrentCardPlayed.Count >= 2)
            {   // Ne pas oublié de gérer les ThreeCard
                return true;
            }
            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 =>
            {
                for (int i = 0; i < ListOrderedStacks.Size; i++)
                {
                    if (i < (ListOrderedStacks.Size * 0.5))
                    {
                        if (card.Value > ListOrderedStacks.getStack(i).Peek().Value)
                        {
                            playableCard.Add(card);
                        }
                    }
                    else
                    {
                        if (card.Value < ListOrderedStacks.getStack(i).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;
        }
    }
}
