using UnityEngine;
using System.Collections;

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