using System.Windows;
using System.Windows.Controls;

namespace BeWo.Controls
{
	public class PopUpEdit : TextBox
	{
		public static readonly DependencyProperty DeleteButtonVisibilityProperty;

		public static readonly RoutedEvent DeleteClickEvent;

		public static readonly DependencyProperty NewButtonVisibilityProperty;

		public static readonly RoutedEvent NewClickEvent;

		public static readonly RoutedEvent PopUpClickEvent;

		static PopUpEdit()
		{
			PopUpClickEvent = EventManager.RegisterRoutedEvent("PopUpClick", RoutingStrategy.Bubble, typeof (RoutedEventHandler), typeof (PopUpEdit));

			DeleteClickEvent = EventManager.RegisterRoutedEvent("DeleteClick", RoutingStrategy.Bubble, typeof (RoutedEventHandler), typeof (PopUpEdit));

			NewClickEvent = EventManager.RegisterRoutedEvent("NewClick", RoutingStrategy.Bubble, typeof (RoutedEventHandler), typeof (PopUpEdit));

			DeleteButtonVisibilityProperty = DependencyProperty.Register("DeleteButtonVisibility", typeof (Visibility), typeof (PopUpEdit));

			NewButtonVisibilityProperty = DependencyProperty.Register("NewButtonVisibility", typeof (Visibility), typeof (PopUpEdit), new PropertyMetadata(Visibility.Collapsed));

			DefaultStyleKeyProperty.OverrideMetadata(typeof (PopUpEdit), new FrameworkPropertyMetadata(typeof (PopUpEdit)));
		}

		public Visibility DeleteButtonVisibility
		{
			get { return (Visibility) GetValue(DeleteButtonVisibilityProperty); }

			set { SetValue(DeleteButtonVisibilityProperty, value); }
		}

		public Visibility NewButtonVisibility
		{
			get { return (Visibility) GetValue(NewButtonVisibilityProperty); }

			set { SetValue(NewButtonVisibilityProperty, value); }
		}

		public event RoutedEventHandler DeleteClick
		{
			add { AddHandler(DeleteClickEvent, value); }

			remove { RemoveHandler(DeleteClickEvent, value); }
		}

		public event RoutedEventHandler NewClick
		{
			add { AddHandler(NewClickEvent, value); }

			remove { RemoveHandler(NewClickEvent, value); }
		}

		public event RoutedEventHandler PopUpClick
		{
			add { AddHandler(PopUpClickEvent, value); }

			remove { RemoveHandler(PopUpClickEvent, value); }
		}

		public override void OnApplyTemplate()
		{
			var lPopUpButton = GetTemplateChild("PART_PopUpButton") as Button;
			if (lPopUpButton != null)
			{
				lPopUpButton.Click += lPopUpButton_Click;
			}

			var lDelteButton = GetTemplateChild("PART_DeleteButton") as Button;
			if (lDelteButton != null)
			{
				lDelteButton.Click += lDelteButton_Click;
			}

			var lNewButton = GetTemplateChild("PART_NewButton") as Button;
			if (lNewButton != null)
			{
				lNewButton.Click += lNewButton_Click;
			}

			base.OnApplyTemplate();
		}

		private void lDelteButton_Click(object sender, RoutedEventArgs e)
		{
			e.RoutedEvent = DeleteClickEvent;
			RaiseEvent(e);
		}

		private void lNewButton_Click(object sender, RoutedEventArgs e)
		{
			e.RoutedEvent = NewClickEvent;
			RaiseEvent(e);
		}

		private void lPopUpButton_Click(object sender, RoutedEventArgs e)
		{
			e.RoutedEvent = PopUpClickEvent;
			RaiseEvent(e);
		}
	}
}