using System.Collections.ObjectModel;
using Model;

namespace ViewModel
{
    public class ChampionManagerVM : BaseToolkit
    {
        public ReadOnlyObservableCollection<ChampionVM> ChampionVMs { get; private set; }
        private ObservableCollection<ChampionVM> _championVMs { get; set; } = new ObservableCollection<ChampionVM>();

        public IDataManager DataManager
        {
            get => _dataManager;
            set
            {
                if (_dataManager == value) return;
                _dataManager = value;
                OnPropertyChanged();
                LoadChampions();
            }
        }
        private IDataManager _dataManager;

        public ChampionManagerVM(IDataManager dataManager)
        {
            DataManager = dataManager;
            ChampionVMs = new ReadOnlyObservableCollection<ChampionVM>(_championVMs);
            PropertyChanged += ChampionManagerVM_PropertyChanged;
        }

        public int Index
        {
            get => _index;
            set
            {
                if (_index == value) return;
                _index = value;
            }

        }
        private int _index;

        public int Count { get; set; } = 5;

        private async void ChampionManagerVM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Index))
                await LoadChampions();
        }

        private async Task LoadChampions()
        {
            _championVMs.Clear();
            var champions = await DataManager.ChampionsMgr.GetItems(0, Count);
            foreach (var champion in champions)
            {
                _championVMs.Add(new ChampionVM(champion));
            }
        }
    }
}