using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp22
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public clsViewModel clsViewModel;

        public MainWindow()
        {
            InitializeComponent();

            clsViewModel = new clsViewModel();

            this.DataContext = clsViewModel;
        }


        //private void RadButton_Click(object sender, RoutedEventArgs e)
        //{
        //    worker.RunWorkerAsync();
        //}
    }

    public class CardInfo
    {
        public string Header { get; set; }
        public string Name { get; set; }
        public int Number { get; set; }
    }

    public class clsViewModel
    {
        public ObservableCollection<CardInfo> source { get; set; }

        BackgroundWorker worker = new BackgroundWorker();

        public ICommand AktualisierenCommand { get; set; }

        public clsViewModel()
        {
            worker.DoWork += Worker_DoWork;

            AktualisierenCommand = new clsRelayCommand(Aktualisieren);

            source = new ObservableCollection<CardInfo>();
            //for (int i = 0; i < 6; i++)
            //{
            //    source.Add(new CardInfo() { Header = "Card " + i, Name = "Name " + i, Number = i });
            //}


        }

        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            Application.Current.Dispatcher.Invoke(() => source.Add(new CardInfo() { Header = "New Card" }));
        }

        public void Aktualisieren()
        {
            if (!worker.IsBusy)
            {
                Application.Current.Dispatcher.Invoke(() => source.Clear());

                worker.RunWorkerAsync();
            }
        }

    }

    class clsRelayCommand : ICommand
    {
        private Action mAction;

        public event EventHandler CanExecuteChanged = (sender, e) => { };

        public clsRelayCommand(Action action)
        {
            mAction = action;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            mAction();
        }
    }

}
