using ControlPanel;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace ControlPanel_WPF
{
    public partial class MainWindow : Window
    {
        private MyViewModel viewModel;
        public MainWindow()
        {
            InitializeComponent();

            this.viewModel = new MyViewModel();
            this.DataContext = this.viewModel;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                while (true)
                {
                    var random = new Random();

                    for (int i = 0; i < 500; i++)
                    {
                        var clubIndex = random.Next(0, 5000);
                        var club = this.viewModel.Clubs[clubIndex];

                        var newGroup = club.Group + 1 > 4 ? 1 : club.Group + 1;
                        var newClub = new Club() { Name = club.Name, Established = club.Established, StadiumCapacity = club.StadiumCapacity, Group = newGroup };
                        
                        App.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            this.viewModel.Clubs[clubIndex] = newClub;
                        }));
                    }

                    Thread.Sleep(1000);
                }
            });

            
        }
    }
}
