using System;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace EnumValuesInCombo
{
	public class EnumModel : INotifyPropertyChanged
	{
		private Type enumType;
		private object selectedValue;

		public IEnumerable Values { get; private set; }

		[TypeConverter(typeof(TypeTypeConverter))]
		public Type EnumType
		{
			get
			{
				return this.enumType;
			}
			set
			{
				this.enumType = value;
				this.InitNames();
			}
		}


		public object SelectedValue
		{
			get
			{
				return this.selectedValue;
			}
			set
			{
				if (this.selectedValue != value)
				{
					this.selectedValue = value;
					this.OnPropertyChanged("SelectedValue");
				}
			}
		}

		private void InitNames()
		{
			this.Values = this.EnumType.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
							.Select<FieldInfo, object>((FieldInfo x) => x.GetValue(this.EnumType));
		}

		public event PropertyChangedEventHandler PropertyChanged;

		private void OnPropertyChanged(string propertyName)
		{
			if (this.PropertyChanged != null)
			{
				this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}
}