using UnityEngine;
using System;

public class Conversion 
{
	static public bool TryConvertToBool(object toConvert, out bool converted)
	{
		if(toConvert is bool)
		{
			converted = (bool)toConvert;
			return true;
		}
		
		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return bool.TryParse(toParse, out converted);
	}

	static public bool TryConvertToSByte(object toConvert, out sbyte converted)
	{
		if(toConvert is sbyte)
		{
			converted = (sbyte)toConvert;
			return true;
		}
		
		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return sbyte.TryParse(toParse, out converted);
	}

	static public bool TryConvertToByte(object toConvert, out byte converted)
	{
		if(toConvert is byte)
		{
			converted = (byte)toConvert;
			return true;
		}
		
		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return byte.TryParse(toParse, out converted);
	}

	static public bool TryConvertToChar(object toConvert, out char converted)
	{
		if(toConvert is char)
		{
			converted = (char)toConvert;
			return true;
		}
		
		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return char.TryParse(toParse, out converted);
	}
	
	static public bool TryConvertToInt16(object toConvert, out short converted)
	{
		if(toConvert is short)
		{
			converted = (short)toConvert;
			return true;
		}
		
		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return short.TryParse(toParse, out converted);
	}

	static public bool TryConvertToUInt16(object toConvert, out ushort converted)
	{
		if(toConvert is ushort)
		{
			converted = (ushort)toConvert;
			return true;
		}
		
		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return ushort.TryParse(toParse, out converted);
	}
	
	static public bool TryConvertToInt32(object toConvert, out int converted)
	{
		if(toConvert is int)
		{
			converted = (int)toConvert;
			return true;
		}
		
		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return int.TryParse(toParse, out converted);
	}	

	static public bool TryConvertToUInt32(object toConvert, out uint converted)
	{
		if(toConvert is uint)
		{
			converted = (uint)toConvert;
			return true;
		}
		
		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return uint.TryParse(toParse, out converted);
	}	

	static public bool TryConvertToInt64(object toConvert, out long converted)
	{
		if(toConvert is long)
		{
			converted = (long)toConvert;
			return true;
		}
		
		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return long.TryParse(toParse, out converted);
	}	
	
	static public bool TryConvertToUInt64(object toConvert, out ulong converted)
	{
		if(toConvert is ulong)
		{
			converted = (ulong)toConvert;
			return true;
		}
		
		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return ulong.TryParse(toParse, out converted);
	}
	
	static public bool TryConvertToSingle(object toConvert, out float converted)
	{
		if(toConvert is float)
		{
			converted = (float)toConvert;
			return true;
		}
		
		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return float.TryParse(toParse, out converted);
	}
	
	static public bool TryConvertToDecimal(object toConvert, out decimal converted)
	{
		if(toConvert is decimal)
		{
			converted = (decimal)toConvert;
			return true;
		}
		
		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return decimal.TryParse(toParse, out converted);
	}

	static public bool TryConvertToDateTime(object toConvert, out DateTime converted)
	{
		if(toConvert is DateTime)
		{
			converted = (DateTime)toConvert;
			return true;
		}
		
		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return DateTime.TryParse(toParse, out converted);
	}

	static public bool TryConvertToVector2(object toConvert, out Vector2 converted)
	{
		if(toConvert is Vector2)
		{
			converted = (Vector2)toConvert;
			return true;
		}
		
		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return Vector2Extensions.TryParse(toParse, out converted);
	}

	static public bool TryConvertToVector3(object toConvert, out Vector3 converted)
	{
		if(toConvert is Vector3)
		{
			converted = (Vector3)toConvert;
			return true;
		}
		
		if(toConvert is Vector2)
		{
			converted = (Vector3)toConvert;
			return true;
		}

		//Last resort - convert to string and parse
		string toParse = toConvert.ToString();
		return Vector3Extensions.TryParse(toParse, out converted);
	}
}
