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

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

        public Card OldCard { get; set; }


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

        public bool play(int valueCard, List<Card> CurrentHand, int orderedStackSelected, Player player, List<Card> CurrentCardPlayed)
        {
            foreach (Card card in CurrentHand)
            {
                if (valueCard == card.Value)
                {
                    if (orderedStackSelected >= 0 && orderedStackSelected < 4)
                    {
                        if (orderedStackSelected < 2)
                        {
                            ErrorMessage = null;
                            return rule(card, ListOrderedStacks.getStack(orderedStackSelected), true, player, CurrentCardPlayed);
                        }
                        else
                        {
                            ErrorMessage = null;
                            return rule(card, ListOrderedStacks.getStack(orderedStackSelected), false, player, CurrentCardPlayed);
                        }
                    }
                    else
                    {
                        ErrorMessage = AppRessource.StrCantGetStack;
                    }
                }
            }
            return false;
        }

        private bool rule(Card card, Stack<Card> stack, bool bottomUp, Player player, List<Card> CurrentCardPlayed)
        {
            if ((bottomUp && card.Value > stack.Peek().Value) || (!bottomUp && card.Value < stack.Peek().Value) || card.Value == stack.Peek().Value - 10 || card.Value == stack.Peek().Value + 10)
            {
                OldCard = stack.Peek();
                player.joue(card);
                CurrentCardPlayed.Add(card);
                stack.Push(card);

                return true;
                //OnTopRangeChanged(new TopRangeChangedEventArgs(card, oldCard, ListOrderedStacks.IndexOf(orderedStack)));
            }
            else
            {
                ErrorMessage = AppRessource.StrWrongStack;
                return false;
            }
        }
    }
}
