using System;
using System.ComponentModel;

using BS.Shared.Core;

namespace BeWo.ViewModel
{
	public abstract class AbstractBaseVM : INotifyPropertyChanged
	{
		private bool _IsDirty;

		public virtual bool IsDirty
		{
			get { return _IsDirty; }
		}

		public Type Type
		{
			get { return GetType(); }
		}

		public event PropertyChangedEventHandler PropertyChanged;

		public virtual void SetDirty(bool newDirty)
		{
			_IsDirty = newDirty;
		}

		protected bool AreDifferent(object pMember, object pNewValue)
		{
			if (pNewValue is string && pNewValue != null)
			{
				pNewValue = (pNewValue as string).Trim();
			}

			if (pNewValue is string && ((string) pNewValue) == string.Empty)
			{
				return pMember != null && ((string) pMember) != string.Empty;
			}

			return (pMember == null && pNewValue != null) || (pMember != null && !pMember.Equals(pNewValue));
		}

		protected bool AreEqual(object x, object y)
		{
			NullCompareResult lResult = Utils.NullCompare(x, y);
			return lResult.BothNotNull
				? x.Equals(y)
				: !lResult.Different;
		}

		protected virtual void FirePropertyChanged(string pPropertyName)
		{
			_IsDirty = true;
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(pPropertyName));
			}
		}
	}
}