using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using Windows.Storage;

namespace LocalFolderSample.DataModel
{
    public class ViewModel : INotifyPropertyChanged
    {
        IDataRepository _data;

        public ViewModel(IDataRepository data)
        {
            _data = data;
        }

        async public void Initialize()
        {
            Customers = await _data.Load();
        }

        private Customer selectedItem;
        public Customer SelectedItem
        {
            get { return this.selectedItem; }
            set
            {
                if (value != selectedItem)
                {
                    selectedItem = value;
                    RaisePropertyChanged();
                }
            }
        }

        private ObservableCollection<Customer> customers;
        public ObservableCollection<Customer> Customers
        {
            get { return customers; }
            set
            {
                customers = value;
                RaisePropertyChanged();
            }
        }

        internal void AddCustomer(Customer cust)
        {
            _data.Add(cust);
            RaisePropertyChanged();
        }

        internal void DeleteCustomer(Customer cust)
        {
            _data.Remove(cust);
            RaisePropertyChanged();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(
            [CallerMemberName] string caller = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(caller));
            }
        }
    }
}