using UnityEngine;
using System.Collections;

public static class QuaternionExtensions
{
	static public string ToString2(this Quaternion value)
	{
		return string.Format("({0:0.######}, {1:0.######}, {2:0.######}, {3:0.######})", value.x, value.y, value.z, value.w);
	}
	
	static public bool Approximately(this Quaternion value, Quaternion other)
	{
		return Mathf.Approximately(value.x, other.x) &&
			Mathf.Approximately(value.y, other.y) &&
			Mathf.Approximately(value.z, other.z) &&
			Mathf.Approximately(value.w, other.w);
	}
	
	static public bool TryParse(string toParse, out Quaternion parsed)
	{
		parsed = default(Quaternion);
		
		if(!toParse.StartsWith("(") || !toParse.EndsWith(")"))
		{
			return false;
		}
		var trimmedToken = toParse.Substring(1, toParse.Length - 2);
		
		var vectorComponentTokens = trimmedToken.Split(',');
		if(vectorComponentTokens.Length != 4)
		{
			return false;
		}
		
		float x;
		float y;
		float z;
		float w;
		bool success = true;
		success &= float.TryParse(vectorComponentTokens[0], out x);
		success &= float.TryParse(vectorComponentTokens[1], out y);
		success &= float.TryParse(vectorComponentTokens[2], out z);
		success &= float.TryParse(vectorComponentTokens[3], out w);
		if(!success)
		{
			return false;
		}
		
		parsed = new Quaternion(x, y, z, w);
		return true;
	}
}
