using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Animation;

namespace com.superscene.ui {
	/// <summary>Behavior</summary>
	public static class BehaviorBox {
		static Dictionary<DependencyObject, ListBehavior> mapBhv = new Dictionary<DependencyObject, ListBehavior>();
		static ListBehavior _Behaviors = new ListBehavior();

		public static ListBehavior GetList(DependencyObject obj) {
			if(!mapBhv.ContainsKey(obj)) {
				ListBehavior lst = new ListBehavior();
				lst.obj = obj;
				mapBhv[obj] = lst;
			}
			return mapBhv[obj];

			//ListBehavior lst = (ListBehavior)obj.GetValue(DListProperty);
			//if(lst == null) {
			//	lst = new ListBehavior();
			//	lst.obj = obj;
			//	obj.SetValue(DListProperty, lst);
			//}
			//return lst;
		}

		public static readonly DependencyProperty DListProperty = DependencyProperty.RegisterAttached("DList", typeof(ListBehavior), typeof(BehaviorBox), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnDListChanged)));
		public static ListBehavior GetDList(DependencyObject obj, ListBehavior value) { return (ListBehavior)obj.GetValue(DListProperty); }
		public static void SetDList(DependencyObject obj, ListBehavior value) {
			obj.SetCurrentValue(DListProperty, value);
			//GetList(obj);
		}

		static void OnDListChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
			ListBehavior value = e.NewValue as ListBehavior;
			if(value == null) {
				return;
			}
			//Debug.WriteLine(value.Count);
			var lst = GetList(obj);
			for(int i = 0; i < value.Count; ++i) {
				lst.Add(value[i]);
			}
		}
	}

	public class ListBehavior : FreezableCollection<BaseBehavior>, INotifyCollectionChanged {
		public DependencyObject obj = null;

		public ListBehavior() {
			((INotifyCollectionChanged)this).CollectionChanged += ListBehavior_CollectionChanged;
		}

		public new void Clear() {
			while(Count > 0) {
				RemoveAt(Count - 1);
			}
		}

		private void ListBehavior_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
			IList lstItem = null;
			//Debug.WriteLine("abc:" + e.Action + "," + obj);
			if(obj == null) {
				return;
			}

			switch(e.Action) {
			case NotifyCollectionChangedAction.Add: lstItem = e.NewItems as IList;break;
			case NotifyCollectionChangedAction.Remove: lstItem = e.OldItems as IList;break;
			}

			if(lstItem == null) {
				return;
			}

			for(int i = 0; i < lstItem.Count; ++i) {
				BaseBehavior bhv = lstItem[i] as BaseBehavior;
				//Debug.WriteLine("abc:" + obj + "," + bhv);
				if(bhv == null) {
					continue;
				}

				switch(e.Action) {
				case NotifyCollectionChangedAction.Add: bhv._element = obj; bhv.onAttached(); break;
				case NotifyCollectionChangedAction.Remove: bhv.onDetaching(); break;
				}
			}
			
		}

	}
}
