using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

using System.ComponentModel;

namespace EQNetwork.Aspects
{
    [Serializable]
    public class PropertyChangeTracker : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members        
#pragma warning disable 0067 // remove "this event is never used" warning (used via reflection)
        public event PropertyChangedEventHandler PropertyChanged;
#pragma warning restore 0067
        #endregion

        private List<string> PropertyChanges = null;

        public PropertyChangeTracker()
        {
            PropertyChanged += new PropertyChangedEventHandler(PropertyChangeTracker_PropertyChanged);
        }

        void PropertyChangeTracker_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            AddPropertyChange(e.PropertyName);
        }

        public void ClearPropertyChanges()
        {
            PropertyChanges = null;
        }

        public void AddPropertyChange(string name)
        {
            if (PropertyChanges == null)
                PropertyChanges = new List<string>();
            if (!PropertyChanges.Contains(name))
                PropertyChanges.Add(name);
        }

        public Dictionary<string, PropertyInfo> GetChangedProperties()
        {
            Dictionary<string, PropertyInfo> dict = new Dictionary<string, PropertyInfo>();

            if (PropertyChanges != null)
            {
                var props = this.GetType().GetProperties();
                foreach (PropertyInfo prop in props)
                    if (PropertyChanges.Contains(prop.Name))
                        dict.Add(prop.Name, prop);
            }
            return dict;
        }
    }
}
