using System.Collections.Generic;
using System.Linq;
using Machinations.UserComponents;

namespace Machinations.Status
{
    public class CardStatus : IElementStatus
    {
        private readonly int m_step;
        private Dictionary<UserComponent, bool> m_takenCards;
        private readonly Cards m_cards;


        public CardStatus(Cards cards, int step)
        {
            m_step = step;
            m_cards = cards;
            m_takenCards = new Dictionary<UserComponent, bool>(m_cards.TakenCards);
        }

        public int Step
        {
            get { return m_step; }
        }

        public void Restore()
        {
            //kartam se meni postupne false na true. Pokud se hodnoty lisi, a ulozena hodnota je true, musi se jit dopredu, a tedy v balicku hloubeji
            bool increasing = false;
            foreach (var pair in m_takenCards)
            {
                if (m_cards.TakenCards[pair.Key] != pair.Value)
                {
                    m_cards.TakenCards[pair.Key] = pair.Value;
                    increasing = pair.Value;
                }
            }
            m_cards.CardChanged(increasing);
        }

        public bool NeedNewStatus()
        {
            return m_cards.TakenCards.Any(pair => m_takenCards[pair.Key] != pair.Value);
        }
    }
}