using System;
using System.Collections.Generic;
using System.ComponentModel;
using TheGameExtreme.model;
using TheGameExtreme.model.card;
using TheGameExtreme.model.deck;
using TheGameExtreme.model.@event;
using TheGameExtreme.model.gameActions.classic;
using TheGameExtreme.model.manager;
using TheGameExtreme.model.piles;

namespace TheGameExtreme.viewmodel
{
    public class Main : INotifyPropertyChanged
    {
        public event EventHandler<EventArgs> EndGame;
        public event EventHandler<EventArgs> AlertChanged;
        public event PropertyChangedEventHandler PropertyChanged;
        private List<PlayerVM> players = new List<PlayerVM>();
        private string alert = "";
        public String Alert
        {
            get { return alert; }
            set
            {
                alert = value;
                OnAlertChanged();
            }
        }
        private string pseudo = "Pseudo";
        public String Pseudo
        {
            get { return pseudo; }
            set
            {
                pseudo = value;
                OnPropertyChanged("Pseudo");
            }
        }
        protected virtual void OnPropertyChanged(string info)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
        }
        protected virtual void OnAlertChanged()
        {
            AlertChanged?.Invoke(this, new EventArgs());
        }

        private GameManager gameManager;
        public List<CardVM> CurrentHand { get; set; } = new List<CardVM>();
        public List<Stack<CardVM>> listOrderedStacks = new List<Stack<CardVM>>();


        public Main(List<string> playersNames)
        {
            int nbPile = 8;
            playersNames.ForEach(name => players.Add(new PlayerVM(new Player(name))));
            Parametreur parametreur = new Parametreur(new GameMode(new ClassicPiles(nbPile), new ClassicDeck()));
            players.ForEach(player => parametreur.AddPlayer(player.View));
            gameManager = new SoloGameManager(parametreur);

            gameManager.EndGame += OnEndGame;

            gameManager.PlayerChanged += OnPlayerChanged;

            gameManager.CurrentHand.ForEach( card =>
            {
                CurrentHand.Add(new CardVM(card));
            });

            Pseudo = players[0].Pseudo;

            for (int i = 0; i < gameManager.getPiles().Size; i++)
            {
                Stack<Card> pileView = gameManager.getPiles().getStack(i);
                Stack<CardVM> pile = new Stack<CardVM>();
                Card[] cards = pileView.ToArray();
                for (int j = 0; j < cards.Length; j++)
                {
                    pile.Push(new CardVM(cards[j]));
                }
                listOrderedStacks.Add(pile);
            }
        }

        public IReadOnlyList<Stack<CardVM>> getListOrderedStacks()
        {
            return listOrderedStacks.AsReadOnly();
        }

        protected internal void OnPlayerChanged(object source, PlayerChangedEventArgs args)
        {
            CurrentHand.Clear();

            args.NewCurrentHand.ForEach( card =>
            {
                CurrentHand.Add(new CardVM(card));
            });
            Pseudo = args.Pseudo;
        }

        public void OnEndGame(object source, EventArgs args)
        {
            EndGame?.Invoke(source, args);
        }

        /**
         *
         *
         * @return booléen de fin de jeu
         */
        public bool played(int numStack, decimal valueCard)
        {
            if (!gameManager.joue(numStack, valueCard))
            {
                Alert = gameManager.EndMessage;
                return false;
            }
            else
            {
                foreach(CardVM card in CurrentHand)
                {
                    if (card.Value.CompareTo(valueCard) == 0)
                    {
                        CurrentHand.Remove(card);
                        break;
                    }
                }
            }
            listOrderedStacks[numStack].Push(new CardVM(gameManager.getPiles().getStack(numStack).Peek()));
            return true;
        }

        public bool endTurn()
        {
            bool isEnd = gameManager.endTurn();
            Alert = gameManager.EndMessage;
            return isEnd;
        }

    }
}
