using System;
using System.Collections.Generic;
using System.Text;
using Vortex.Drawing;

namespace Vortex.SceneFramework.Gui {
	/// <summary>
	/// Range widget is abstract control for determining 
	/// </summary>
	public class RangeWidget : ViewNode {
		protected int _minValue = 0;
		protected int _maxValue = 100;
		protected int _value = 0;
		private bool _isReversed;

		#region Properties

		/// <summary>
		/// Gets or sets the minimal value of slider.
		/// </summary>
		/// <value>The minimal value.</value>
		public virtual int MinValue {
			get { return _minValue; }
			set {
				_minValue = value;
				if (_minValue > _value) {
					Value = _minValue;
				}
			}
		}


		/// <summary>
		/// Gets or sets the maximal value of slider.
		/// </summary>
		/// <value>The maximal value.</value>
		public virtual int MaxValue {
			get { return _maxValue; }
			set {
				_maxValue = value;
				if (_maxValue < _value) {
					Value = _maxValue;
				}
			}
		}

		/// <summary>
		/// Gets or sets the slider's value.
		/// </summary>
		/// <value>The sliders' value.</value>
		public virtual int Value {
			get { return _value; }
			set {
				int newValue = Math.Max(Math.Min(value, _maxValue), _minValue);
				bool changed = _value != newValue;
				_value = newValue;
				if (changed && null != OnChange) {
					OnChange(this);
				}
			}
		}

		/// <summary>
		/// Gets the value ratio (0..1).
		/// </summary>
		/// <value>The value ratio.</value>
		public float ValueRatio {
			get { return (float)(Value - MinValue) / (float)(Math.Max(MaxValue - MinValue, 1)); }
		}

		/// <summary>
		/// Gets the orientation of widget in the space.
		/// </summary>
		/// <value>The orientation of widget.</value>
		public virtual Orientation Orientation {
			get { Vector2 size = Size; return size.X > size.Y ? Orientation.Horizontal : Orientation.Vertical; }
		}

		/// <summary>
		/// Gets or sets a value indicating whether this widget is reversed.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this widget is reversed; otherwise, <c>false</c>.
		/// </value>
		public bool IsReversed {
			get { return _isReversed; }
			set { _isReversed = value; }
		}

		#endregion


		#region Events

		/// <summary>
		/// Occurs when slider position value is changed in any way.
		/// </summary>
		public event GraphEventHandler OnChange;

		#endregion
	}
}
