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 JouerUneCarteFraction : JouerUneCarte
    {
        public JouerUneCarteFraction(Piles ListOrderedStacks) : base(ListOrderedStacks)
        {
        }

        override public bool play(decimal valueCard, List<Card> CurrentHand, int orderedStackSelected, Player player, List<Card> CurrentCardPlayed)
        {
            foreach (Card card in CurrentHand)
            {
                if (valueCard.CompareTo(card.Value) == 0)
                {
                    if (orderedStackSelected >= 0 && orderedStackSelected < ListOrderedStacks.Size)
                    {
                        bool success;
                        if (orderedStackSelected < (ListOrderedStacks.Size * 0.5))
                        {
                            ErrorMessage = null;
                            success = Rule(card, ListOrderedStacks.getStack(orderedStackSelected), true, player, CurrentCardPlayed);
                        }
                        else
                        {
                            ErrorMessage = null;
                            success = Rule(card, ListOrderedStacks.getStack(orderedStackSelected), false, player, CurrentCardPlayed);
                        }
                        if (success)
                        {
                            CurrentHand.Remove(card);
                        }
                        return success;
                    }
                    else
                    {
                        ErrorMessage = AppResources.StrCantGetStack;
                    }
                }
            }
            ErrorMessage = AppResources.StrCardDoesntExist;
            return false;
        }

        override protected 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))
            {
                OldCard = stack.Peek();
                player.joue(card);
                CurrentCardPlayed.Add(card);
                stack.Push(card);
                return true;
            }
            else if (((FractionCard)card).Fraction.isMultiple(((FractionCard)stack.Peek()).Fraction))
            {
                OldCard = stack.Peek();
                player.joue(card);
                CurrentCardPlayed.Add(card);
                stack.Push(card);
                return true;
            }
            else
            {
                ErrorMessage = AppResources.StrWrongStack;
                return false;
            }
        }
    }
}
