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

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

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

        override public bool end(List<Card> CurrentHand, List<Card> CurrentCardPlayed)
        {
            if (CurrentHand.Count == 0 || CurrentCardPlayed.Count >= 2)
            {
                return true;
            }
            else
            {
                ErrorMessage = AppResources.StrCardPlayedLessThanTwo;
                return false;
            }
            
        }

        override protected void tryToFindSoluce(List<Card> playableCard, List<Card> CurrentHand)
        {
            int findDownCard = 0;
            int findUpCard = 0;
            List<Card> hand = new List<Card>();
            List<Card> playableDownCard = new List<Card>();
            List<Card> playableUpCard = new List<Card>();

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


            hand.ForEach(card =>
            {
                for (int i = 0; i < ListOrderedStacks.Size; i++)
                {
                    if (card.Value.CompareTo(ListOrderedStacks.getStack(i).Peek().Value + 10) == 0)
                    {
                        playableDownCard.Add(card);
                    }
                    else if (card.Value.CompareTo(ListOrderedStacks.getStack(i).Peek().Value - 10) == 0)
                    {
                        playableUpCard.Add(card);
                    }
                }
            });

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

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


            while ((playableDownCard.Count > findDownCard || playableUpCard.Count > findUpCard) && hand.Count > 0)
            {
                findDownCard = playableDownCard.Count;
                findUpCard = playableUpCard.Count;

                hand.ForEach(card =>
                {
                    for (int i = 0; i < playableDownCard.Count; i++)
                    {
                        if (card.Value.CompareTo(ListOrderedStacks.getStack(i).Peek().Value + 10) == 0)
                        {
                            playableDownCard.Add(card);
                        }
                    }
                    for (int i = 0; i < playableUpCard.Count; i++)
                    {
                        if (card.Value.CompareTo(ListOrderedStacks.getStack(i).Peek().Value - 10) == 0)
                        {
                            playableUpCard.Add(card);
                        }
                    }
                });

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

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

            playableDownCard.ForEach(card => playableCard.Add(card));
            playableUpCard.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 < playableDownCard.Count; i++)
                    {
                        if (card.Value > playableDownCard[i].Value)
                        {
                            playableCard.Add(card);
                            played = true;
                            break;
                        }
                    }
                }
                
                if (!played)
                {
                    for (int i = 0; i < playableUpCard.Count; i++)
                    {
                        if (card.Value < playableUpCard[i].Value)
                        {
                            playableCard.Add(card);
                            played = true;
                            break;
                        }
                    }
                }
                played = false;
            });
        }

        override 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;
        }
    }
}
