using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using QwirkleClassLibrary.Boards;
using QwirkleClassLibrary.Tiles;

namespace QwirkleClassLibrary.Players
{
    [DataContract]
    public class Player : INotifyPropertyChanged
    {
        [DataMember]

        private ObservableCollection<Tile> tiles = new ObservableCollection<Tile>();

        public ObservableCollection<Tile> Tiles
        {
            get { return tiles; }
            set
            {
                tiles = value;
                OnPropertyChanged(nameof(Tiles));
            }
        }


        
        public event PropertyChangedEventHandler? PropertyChanged;
        
        void OnPropertyChanged([CallerMemberName] string? propertyName = null)
            => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

        /// <summary>
        /// Creates a player with a name
        /// </summary>
        /// <param name="name"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public Player(string name)
        {
            if (name == null || string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(name);
            }

            NameTag = name;
            Tiles = [];
        }

        [DataMember]
        public string NameTag { get; set; }

        [DataMember]
        public bool IsPlaying { get; set; } = false;
        
        /// <summary>
        /// Adds a tile to the player's hand
        /// </summary>
        /// <param name="tile"></param>
        public void AddTileToPlayer(Tile tile)
        {
            Tiles.Add(tile);
            OnPropertyChanged(nameof(Tiles));
        }

        /// <summary>
        /// Removes a tile from the player's hand
        /// </summary>
        /// <param name="tile"></param>
        /// <returns>bool</returns>
        public bool RemoveTileToPlayer(Tile tile)
        {
            if (Tiles.Remove(tile))
            {
                OnPropertyChanged(nameof(Tiles));
                return true;
            }
            return false;
        }
    }
}
