using System;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace BusinessObjectsBinding
{
    public class AppointmentsBO
    {
        AppointmentBOList _list = null;
        
        public AppointmentsBO()
        {
            _list = new AppointmentBOList();
            _list.Add(new AppointmentBORow());
        }

        public AppointmentBOList Appointments
        {
            get
            {
                return _list;
            }
            set
            {
                _list = value;
            }
        }
    }

    // Business object should implement INotifyPropertyChanged interface.
    public class AppointmentBORow : INotifyPropertyChanged
    {
        private string _subject = "";
        private string _body = "";
        private string _location = "";
        private string _properties = "";
        private DateTime _start = DateTime.Today;
        private DateTime _end = DateTime.Today + TimeSpan.FromDays(1);
        private Guid _id = Guid.NewGuid();
        private bool _isDeleted = false;

        public AppointmentBORow()
        {
        }

        public string Subject
        {
            get { return _subject; }
            set
            {
                if (_subject != value)
                {
                    _subject = value;
                    OnPropertyChanged("Subject");
                }
            }
        }

        public DateTime Start
        {
            get { return _start; }
            set
            {
                if (_start != value)
                {
                    _start = value;
                    OnPropertyChanged("Start");
                }
            }
        }

        public DateTime End
        {
            get { return _end; }
            set
            {
                if (_end != value)
                {
                    _end = value;
                    OnPropertyChanged("End");
                }
            }
        }

        public string Body
        {
            get { return _body; }
            set
            {
                if (_body != value)
                {
                    _body = value;
                    OnPropertyChanged("Body");
                }
            }
        }

        public string Location
        {
            get { return _location; }
            set
            {
                if (_location != value)
                {
                    _location = value;
                    OnPropertyChanged("Location");
                }
            }
        }

        public string Properties
        {
            get { return _properties; }
            set
            {
                if (_properties != value)
                {
                    _properties = value;
                    OnPropertyChanged("Properties");
                }
            }
        }

        public Guid Id
        {
            get { return _id; }
            set
            {
                if (_id != value)
                {
                    _id = value;
                    OnPropertyChanged("Id");
                }
            }
        }

        public bool IsDeleted
        {
            get { return _isDeleted; }
            set
            {
                if (_isDeleted != value)
                {
                    _isDeleted = value;
                    OnPropertyChanged("IsDeleted");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }

    // Business objects collection should implement INotifyCollectionChanged interface.
    public class AppointmentBOList : ObservableCollection<AppointmentBORow>
    {
        protected override void RemoveItem(int index)
        {
            AppointmentBORow item = this[index];
            if (item != null)
            {
                item.IsDeleted = true;
            }
            base.RemoveItem(index);
        }
    }
}