using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Hanna.Controls
{
	public partial class NumericUpDown : UserControl
	{	
		private int _min = 0;
		private int _max = 100;
		private int _value = 0;
		public event ValueChangedEventHandler ValueChanged;

		public NumericUpDown()
		{
			InitializeComponent();
		}

		#region managing numeric value 

		public int Minimum
		{
			get { return _min;}
			set 
			{ 
				_min = value;
				if (Value < _min)
					Value = _min;
			}
		}
		public int Maximum
		{
			get { return _max; }
			set 
			{ 
				_max = value;
				if (Value > _max)
					Value = _max;
			}
		}
		public int Value
		{
			get {return _value;}
			set 
			{
				if (value >= _min && value <= _max)
				{
					_value = value;
					//	during design time, everything can be null (annoying)
					if ( lblValue != null )
						lblValue.Text = value.ToString();
					if ( ValueChanged != null )
						ValueChanged(this, new ValueChangedEventArgs(value));
				}
				else
					throw new ArgumentOutOfRangeException(string.Format("Value ({0}) must be between Minimum ({1}) and Maxium ({2}).",
														  value,
														  _min,
														  _max));
			}
		}

		#endregion 

		#region making buttons look clicked

		private void button_MouseDown(object sender, MouseEventArgs e)
		{
			((Shape)sender).FillStyle = Shape.FillStyles.Sunken;
		}
		private void button_MouseLeave(object sender, EventArgs e)
		{
			((Shape)sender).FillStyle = Shape.FillStyles.Raised;
		}
		private void button_MouseUp(object sender, MouseEventArgs e)
		{
			((Shape)sender).FillStyle = Shape.FillStyles.Raised;
		}

		#endregion

		private void buttonRight_Click(object sender, EventArgs e)
		{
			if (Value < _max)
				Value += 1;
		}
		private void buttonLeft_Click(object sender, EventArgs e)
		{
			if (Value > _min)
				Value -= 1;
		}
	}

	#region event deletage and args class

	public class ValueChangedEventArgs : EventArgs
	{
		private int _value = 0;
		public ValueChangedEventArgs(int Value)
		{
			_value = Value;
		}
		public int Value
		{
			get { return _value; }
			set { _value = value; }
		}
	}
	public delegate void ValueChangedEventHandler(object sender, ValueChangedEventArgs e);

	#endregion
}
