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

namespace TheGameExtreme.viewmodel
{
    public class Main : INotifyPropertyChanged
    {

        //public event EventHandler<PlayerChangedEventArgs> PlayerChanged;
        public event EventHandler<TopRangeChangedEventArgs> BindingChanged;
        public event PropertyChangedEventHandler PropertyChanged;
        private List<PlayerVM> players = new List<PlayerVM>();
        private string alert = "";
        public String Alert
        {
            get { return alert; }
            set
            {
                alert = value;
                OnPropertyChanged("Alert");
            }
        }
        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));
        }

        private GameManager gameManager;
        public List<Card> CurrentHand { get; set; }
        //private ObservableCollection<Stack<Card>> ListOrderedStacks;


        public Main()
        {
            List<string> playersNames = new List<string> { "Clément", "Baptiste" };
            playersNames.ForEach(name => players.Add(new PlayerVM(new Player(name))));
            Parametreur parametreur = new Parametreur(new GameMode());
            players.ForEach(player => parametreur.AddPlayer(player.View));
            gameManager = new SoloGameManager(parametreur) ;  // Donner le nom des joueurs

            gameManager.TopRangeChanged += OnBindingChanged;

            gameManager.PlayerChanged += OnPlayerChanged;

            CurrentHand = gameManager.CurrentHand;

            Pseudo = players[0].Pseudo;

            //ListOrderedStacks = new ObservableCollection<Stack<Card>>(gameManager.ListOrderedStacks);
        }

        protected internal void OnPlayerChanged(object source, PlayerChangedEventArgs args)
        {
            CurrentHand = args.NewCurrentHand;
            Pseudo = args.Pseudo;
            //PlayerChanged?.Invoke(this, args);
        }

        public void OnBindingChanged(object source, TopRangeChangedEventArgs args)
        {
            BindingChanged?.Invoke(this, args);
        }








        /**
         *
         *
         * @return booléen de fin de jeu
         */
        public bool played(int numStack, int valueCard)
        {
            //try
            //{
                gameManager.joue(valueCard, numStack);

                Alert = gameManager.EndMessage;

                //if (gameManager.EndMessage != null)
                //{
                //    Alert = gameManager.EndMessage;
                //    return true;
                //}
                return false;
            //}
            //catch (Exception e)
            //{
            //    // Faire un toast
            //    Alert = e.Message;
            //}
            //return false;




            //if (gameManager.isCanPlay())
            //{
            //    return playOneCard(numStack, valueCard);
            //}
            //else
            //{
            //    Alert = "Le joueur n'a plus de carte dans sa main!";
            //    // Faire un toast
            //    return false;
            //}
        }

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




        ///**
        // *
        // * 
        // * @return booléen de fin de jeu
        // */
        //private bool playOneCard(int numStack, int valueCard)
        //{
        //    try
        //    {
        //        gameManager.joue(valueCard, numStack);
        //        if (gameManager.EndMessage != null)
        //        {
        //            Alert = gameManager.EndMessage;
        //            return true;
        //        }
        //    }
        //    catch(Exception e)
        //    {
        //        // Faire un toast
        //        Alert = e.Message;
        //    }
        //    return false;
        //}

        //public bool endTurn()
        //{
        //    try
        //    {
        //        bool isEnd = gameManager.endTurn();
        //        Alert = gameManager.EndMessage;
        //        return isEnd;
        //    }
        //    catch(Exception e)
        //    {
        //        Alert = e.Message;
        //        return false;
        //    }
        //}

    }
}
