using System.ComponentModel;
using MusicMetaTagger.Core.Model;
using MusicMetaTagger.Core.Queries;

namespace MusicMetaTagger.UI
{
	public class TrackQueryStatus : TrackQuery, INotifyPropertyChanged
	{
		private int _percentProgress;
		private string _status;
		
		///<summary>
		///  Occurs when a property value changes.
		///</summary>
		public event PropertyChangedEventHandler PropertyChanged;

		/// <summary>
		///   Gets or sets the status.
		/// </summary>
		/// <value>The status.</value>
		public string Status
		{
			get { return _status; }
			set
			{
				if (_status == null && value == null)
				{
					return;
				}

				if ((_status == null) || !_status.Equals(value))
				{
					_status = value + " <- " + _status;

					SignalPropertyChanged("Status");
				}
			}
		}

		public void UpdateStatus(StatusEventArgs data)
		{
			Status = data.Status;
		}

		/// <summary>
		///   Gets or sets the progress. 0 to 100.
		/// </summary>
		/// <value>The progress.</value>
		public int PercentProgress
		{
			get { return _percentProgress; }
			set { CheckPropertyChangedValueType("PercentProgress", ref _percentProgress, ref value); }
		}

		
		/// <summary>
		///   Checks if the property (propertyName) has been updated or changed.
		/// </summary>
		/// <param name = "propertyName">Name of the property.</param>
		/// <param name = "oldValue">The old value.</param>
		/// <param name = "newValue">The new value.</param>
		/// <returns>True if the value was updated, false otherwise.</returns>
		protected bool CheckPropertyChangedValueType<T>(string propertyName, ref T oldValue, ref T newValue) where T : struct
		{
			if (!oldValue.Equals(newValue))
			{
				oldValue = newValue;

				SignalPropertyChanged(propertyName);

				return true;
			}

			return false;
		}

		/// <summary>
		///   Creates the event that a property has changed value
		/// </summary>
		/// <param name = "propertyName"></param>
		protected void SignalPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}
}