using UnityEngine;
using System.Collections;

public static class Vector2Extensions 
{
	static public string ToString2(this Vector2 value)
	{
		return string.Format("({0:0.######}, {1:0.######})", value.x, value.y);
	}
	
	static public bool Approximately(this Vector2 value, Vector2 other)
	{
		return Mathf.Approximately(value.x, other.x) &&
			Mathf.Approximately(value.y, other.y);
	}
	
	static public bool TryParse(string toParse, out Vector2 parsed)
	{
		parsed = default(Vector2);

		if(!toParse.StartsWith("(") || !toParse.EndsWith(")"))
		{
			return false;
		}
		var trimmedToken = toParse.Substring(1, toParse.Length - 2);
		
		var vectorComponentTokens = trimmedToken.Split(',');
		if(vectorComponentTokens.Length != 2)
		{
			return false;
		}
		
		float x;
		float y;
		bool success = true;
		success &= float.TryParse(vectorComponentTokens[0], out x);
		success &= float.TryParse(vectorComponentTokens[1], out y);
		if(!success)
		{
			return false;
		}
		
		parsed = new Vector2(x, y);
		return true;
	}
	
}
