using UnityEngine;
//using System.Collections.Generic;

static public class ObjectExtensions
{
	static public bool ToBoolean(this object value)
	{
		if(value is bool)
		{
			return (bool)value;
		}
		
		string toParse = value.ToString().Trim();
		bool parsed;
		if(bool.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return false;
	}
	
	static public byte ToByte(this object value)
	{
		if(value is byte)
		{
			return (byte)value;
		}
		
		string toParse = value.ToString().Trim();
		byte parsed;
		if(byte.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return 0;
	}
	
	static public sbyte ToSByte(this object value)
	{
		if(value is sbyte)
		{
			return (sbyte)value;
		}
		
		string toParse = value.ToString().Trim();
		sbyte parsed;
		if(sbyte.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return 0;
	}
	
	static public char ToChar(this object value)
	{
		if(value is char)
		{
			return (char)value;
		}
		
		string toParse = value.ToString();
		char parsed;
		if(char.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return '\0';
	}
	
	static public short ToInt16(this object value)
	{
		if(value is short)
		{
			return (short)value;
		}
		
		string toParse = value.ToString().Trim();
		short parsed;
		if(short.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return 0;
	}
	
	static public ushort ToUInt16(this object value)
	{
		if(value is ushort)
		{
			return (ushort)value;
		}
		
		string toParse = value.ToString().Trim();
		ushort parsed;
		if(ushort.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return 0;
	}
	
	static public int ToInt32(this object value)
	{
		if(value is int)
		{
			return (int)value;
		}
		
		string toParse = value.ToString().Trim();

		int parsed;
		if(Int32Extensions.TryParseWithHex(toParse, out parsed))
		{
			return parsed;
		}
		
		return 0;
	}
	
	static public uint ToUInt32(this object value)
	{
		if(value is uint)
		{
			return (uint)value;
		}
		
		string toParse = value.ToString().Trim();
		uint parsed;
		if(uint.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return 0;
	}
	
	static public long ToInt64(this object value)
	{
		if(value is long || value is int || value is uint)
		{
			return (long)value;
		}
		
		string toParse = value.ToString().Trim();
		long parsed;
		if(long.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return 0;
	}
	
	static public ulong ToUInt64(this object value)
	{
		if(value is ulong || value is int || value is uint)
		{
			return (ulong)value;
		}
		
		string toParse = value.ToString().Trim();
		ulong parsed;
		if(ulong.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return 0;
	}
	
	static public float ToSingle(this object value)
	{
		if(value is float)
		{
			return (float)value;
		}
		
		string toParse = value.ToString().Trim();
		float parsed;
		if(float.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return 0;
	}
	
	static public decimal ToDecimal(this object value)
	{
		if(value is decimal)
		{
			return (decimal)value;
		}
		
		string toParse = value.ToString().Trim();
		decimal parsed;
		if(decimal.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return 0;
	}
	
	static public System.DateTime ToDate(this object value)
	{
		if(value is System.DateTime)
		{
			return ((System.DateTime)value).Date;
		}
		
		string toParse = value.ToString().Trim();
		System.DateTime parsed;
		if(System.DateTime.TryParse(toParse, out parsed))
		{
			return parsed.Date;
		}
		
		return System.DateTime.MinValue;
	}

	static public System.TimeSpan ToTimeSpan(this object value)
	{
		if(value is System.DateTime)
		{
			return (System.DateTime)value - ((System.DateTime)value).Date;
		}
		
		string toParse = value.ToString().Trim();
		System.DateTime parsed;
		if(System.DateTime.TryParse(toParse, out parsed))
		{
			return parsed - parsed.Date;
		}
		
		return System.TimeSpan.MinValue;
	}

	static public System.DateTime ToDateTime(this object value)
	{
		if(value is System.DateTime)
		{
			return (System.DateTime)value;
		}
		
		string toParse = value.ToString().Trim();
		System.DateTime parsed;
		if(System.DateTime.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return System.DateTime.MinValue;
	}
	
	static public Vector2 ToVector2(this object value)
	{
		if(value is Vector2)
		{
			return (Vector2) value;
		}
		
		string toParse = value.ToString().Trim();
		Vector2 parsed;
		if(Vector2Extensions.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return Vector2.zero;
	}
	
	static public Vector3 ToVector3(this object value)
	{
		if(value is Vector3)
		{
			return (Vector3) value;
		}
		
		string toParse = value.ToString().Trim();
		Vector3 parsed;
		if(Vector3Extensions.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		Debug.LogWarning("Could not convert to Vector3: " + (value??"null").ToString());
		return Vector3.zero;
	}
	
	static public Vector4 ToVector4(this object value)
	{
		if(value is Vector4)
		{
			return (Vector4) value;
		}
		
		string toParse = value.ToString().Trim();
		Vector4 parsed;
		if(Vector4Extensions.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return Vector4.zero;
	}
	
	static public Quaternion ToQuaternion(this object value)
	{
		if(value is Quaternion)
		{
			return (Quaternion) value;
		}
		
		string toParse = value.ToString().Trim();
		Quaternion parsed;
		if(QuaternionExtensions.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return Quaternion.identity;
	}
	
	static public Color ToColor(this object value)
	{
		if(value is Color || value is Color32 || value is ColorHsl || value is ColorHsv)
		{
			return (Color)value;
		}
		
		string toParse = value.ToString().Trim();
		Color parsed;
		if(ColorExtensions.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return Color.magenta;
	}
	
	static public Color ToColor32(this object value)
	{
		if(value is Color || value is Color32 || value is ColorHsl || value is ColorHsv)
		{
			return (Color32)value;
		}
		
		if(value is Color)
		{
			return (Color32)value;
		}
		
		string toParse = value.ToString();
		Color32 parsed;
		if(Color32Extensions.TryParse(toParse, out parsed))
		{
			return parsed;
		}
		
		return Color.magenta;
	}

		//	static internal IntVector3 ToIntVector3(this object value)
	//	{
	//		if(value is IntVector3)
	//		{
	//			return (IntVector3) value;
	//		}
	//		
	//		return new IntVector3();
	//	}
}
