using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Telerik.Windows.Data;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Collections;

namespace SelectedDataRecordsBinding_Wpf_2009_Q11
{
	public class WrapperCollection : IList, INotifyCollectionChanged, INotifyPropertyChanged
	{
		private SelectedRecordsCollection collection;

		public WrapperCollection(SelectedRecordsCollection collection)
		{
			this.collection = collection;
			collection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(collection_CollectionChanged);
			collection.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(collection_PropertyChanged);
		}

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

		private void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
		{
			if (this.CollectionChanged != null)
				this.CollectionChanged(this, args);
		}

		void collection_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			this.OnPropertyChanged(e.PropertyName);
		}

		void collection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			NotifyCollectionChangedEventArgs args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);

			this.OnCollectionChanged(args);
		}

		private DataRecord GetRecordFromData(object data)
		{
			var dataRecord =
				(from DataRecord record in this.collection
				 where record.Data == data
				 select record).FirstOrDefault<DataRecord>();

			return dataRecord;
		}

		#region INotifyCollectionChanged Members

		public event NotifyCollectionChangedEventHandler CollectionChanged;

		#endregion

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		#endregion

		#region IList Members

		int IList.Add(object value)
		{
			throw new NotImplementedException();
		}

		void IList.Clear()
		{
			this.collection.Clear();
		}

		bool IList.Contains(object value)
		{
			var dataRecord = this.GetRecordFromData(value);

			return (dataRecord != null);
		}

		int IList.IndexOf(object value)
		{
			var dataRecord = this.GetRecordFromData(value);

			return this.collection.IndexOf(dataRecord);
		}

		void IList.Insert(int index, object value)
		{
			throw new NotImplementedException();
		}

		bool IList.IsFixedSize
		{
			get
			{
				IList list = this.collection as IList;

				if (list != null)
					return list.IsFixedSize;
				else
					return true;
			}
		}

		bool IList.IsReadOnly
		{
			get
			{
				return true;
			}
		}

		void IList.Remove(object value)
		{
			var dataRecord = this.GetRecordFromData(value);

			this.collection.Remove(dataRecord);
		}

		void IList.RemoveAt(int index)
		{
			this.collection.RemoveAt(0);
		}

		object IList.this[int index]
		{
			get
			{
				DataRecord record = (DataRecord)this.collection[index];
				return record.Data;
			}
			set
			{
				throw new NotImplementedException();
			}
		}

		#endregion

		#region ICollection Members

		void ICollection.CopyTo(Array array, int index)
		{
			throw new NotImplementedException();
		}

		int ICollection.Count
		{
			get
			{
				ICollection list = this.collection as ICollection;

				if (list != null)
					return list.Count;
				else
					return 0;
			}
		}

		bool ICollection.IsSynchronized
		{
			get
			{
				ICollection list = this.collection as ICollection;

				if (list != null)
					return list.IsSynchronized;
				else
					return false;
			}
		}

		object ICollection.SyncRoot
		{
			get
			{
				ICollection list = this.collection as ICollection;

				if (list != null)
					return list.SyncRoot;
				else
					return this.collection;
			}
		}

		#endregion

		#region IEnumerable Members

		IEnumerator IEnumerable.GetEnumerator()
		{
			return new WrapperEnumerator(this.collection);
		}

		#endregion
	}
}
