using System;
using System.Collections.Generic;
using TheGameExtreme.model.card;
using TheGameExtreme.model.card.cardType;
using TheGameExtreme.model.gameActions.abstractRules;
using TheGameExtreme.model.piles;
using TheGameExtreme.Resx;

namespace TheGameExtreme.model.gameActions.fraction
{
    public class TerminerSonTourFraction : TerminerSonTour
    {
        public TerminerSonTourFraction(Piles ListOrderedStacks) : base(ListOrderedStacks)
        {
        }

        public override bool end(List<Card> CurrentHand, List<Card> CurrentCardPlayed)
        {
            if (CurrentHand.Count == 0 || CurrentCardPlayed.Count >= 2)
            {
                // Vérifier la fin du jeu
                return true;
            }
            else
            {
                ErrorMessage = AppResources.StrCardPlayedLessThanTwo;
                return false;
            }
        }

        protected override bool testEndGame(List<Card> playableCard)
        {
            if (playableCard.Count < 2)
            {
                return false;
            }
            return true;
        }

        protected override void tryToFindSoluce(List<Card> playableCard, List<Card> CurrentHand)
        {
            int findMultipleCard = 0;
            List<Card> hand = new List<Card>();
            List<Card> playableMultipleCard = new List<Card>();

            CurrentHand.ForEach(card => hand.Add(card));


            hand.ForEach(card =>
            {
                for (int i = 0; i < ListOrderedStacks.Size; i++)
                {
                    if (((FractionCard)card).Fraction.isMultiple(((FractionCard)ListOrderedStacks.getStack(i).Peek()).Fraction))
                    {
                        playableMultipleCard.Add(card);
                    }
                }
            });

            playableMultipleCard.ForEach(card =>
            {
                hand.Remove(card);
            });


            while (playableMultipleCard.Count > findMultipleCard && hand.Count > 0)
            {
                findMultipleCard = playableMultipleCard.Count;

                hand.ForEach(card =>
                {
                    for (int i = 0; i < playableMultipleCard.Count; i++)
                    {
                        if (((FractionCard)card).Fraction.isMultiple(((FractionCard)ListOrderedStacks.getStack(i).Peek()).Fraction))
                        {
                            playableMultipleCard.Add(card);
                        }
                    }
                });

                for (int i = findMultipleCard; i < playableMultipleCard.Count; i++)
                {
                    hand.Remove(playableMultipleCard[i]);
                }
            }

            playableMultipleCard.ForEach(card => playableCard.Add(card));

            bool played = false;
            hand.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);
                            played = true;
                            break;
                        }
                    }
                    else if (card.Value < ListOrderedStacks.getStack(i).Peek().Value)
                    {
                        playableCard.Add(card);
                        played = true;
                        break;
                    }
                }

                if (!played)
                {
                    for (int i = 0; i < playableMultipleCard.Count; i++)
                    {
                        if (card.Value > playableMultipleCard[i].Value)
                        {
                            playableCard.Add(card);
                            played = true;
                            break;
                        }
                    }
                }

                played = false;
            });
        }
    }
}
