using System;
using System.Globalization;

namespace Mox.Shared.UI
{
	public enum EUnit
	{
		Pixel,
		Star,
		Percent
	};

	public class Measure : IEquatable<Measure>
	{
		public Measure(double _dValue, EUnit _eUnit)
		{
			Value = _dValue;
			Unit = _eUnit;
		}

		public override string ToString()
		{
			return $"{Value.ToString(CultureInfo.InvariantCulture)} {(Unit == EUnit.Percent ? "%" : Unit == EUnit.Pixel ? "px" : "*")}";
		}

		public int GetPixels(int _iSize, double _dWeight = 1)
		{
			if (Unit == EUnit.Pixel)
			{
				return (int) Value;
			}

			if (Unit == EUnit.Star)
			{
				return _dWeight <= double.Epsilon ? 0 : (int) (_iSize * Value / _dWeight);
			}

			return (int) (Value * _iSize); // percent
		}

		public double Value;
		public EUnit Unit;

		public bool Equals(Measure other)
		{
			if (ReferenceEquals(null, other))
			{
				return false;
			}

			if (ReferenceEquals(this, other))
			{
				return true;
			}

			return Value.Equals(other.Value) && Unit == other.Unit;
		}

		public override bool Equals(object obj)
		{
			if (ReferenceEquals(null, obj))
			{
				return false;
			}

			if (ReferenceEquals(this, obj))
			{
				return true;
			}

			if (obj.GetType() != GetType())
			{
				return false;
			}

			return Equals((Measure) obj);
		}

		public override int GetHashCode()
		{
			unchecked
			{
				return (Value.GetHashCode() * 397) ^ (int) Unit;
			}
		}
	}
}
