using UnityEngine;
using System.Collections;

public class MathHelper
{

	public static float getVectorAngle(Vector2 vector)
	{
		if (vector == Vector2.zero)
			return 0f;
		else if (vector.x >= 0f)
			return Mathf.Atan(vector.y / vector.x);
		else
			return Mathf.Atan(vector.y / vector.x) + Mathf.PI;
	}

	public static Vector2 getVectorFromAngle(float angle, float magnitude)
	{
		return new Vector2(Mathf.Cos(angle) * magnitude, Mathf.Sin(angle) * magnitude);
	}

	public static Vector2 resizeVector(Vector2 vector, float magnitude)
	{
		float angle = getVectorAngle(vector);
		return new Vector2(Mathf.Cos(angle) * magnitude, Mathf.Sin(angle) * magnitude);
	}


	public static float getAngleDifference(float angle, float target)
	{
		angle *= 180f / Mathf.PI;
		target *= 180f / Mathf.PI;
		float dist = 0f;
		int direction;

		if (angle < 180f)
		{
			if (angle < target)
				if (target - 180f == angle)
				{
					dist = 180f;
					direction = 0;
				}
				else if (target - 180f < angle)
				{
					dist = target - angle;
					direction = 1;
				}
				else
				{
					dist = angle + (360f - target);
					direction = -1;
				}
			else
			{
				dist = angle - target;
				direction = -1;
			}
		}
		else
		{
			if (target < angle)
				if (angle - 180 == target)
				{
					dist = 180;
					direction = 0;
				}
				else if (angle - 180 < target)
				{
					dist = angle - target;
					direction = -1;
				}
				else
				{
					dist = target + (360 - angle);
					direction = 1;
				}
			else
			{
				dist = target - angle;
				direction = 1;
			}
		}
		return dist * (float)direction * Mathf.PI / 180f;
	}

	public static Vector2 get2DDifference(Vector3 position, Vector3 target)
	{
		return (Vector2)target - (Vector2)position;
	}

	public static bool approximately(float value1, float value2, float buffer)
	{
		return Mathf.Abs(value2 - value1) < buffer;
	}


	public static float get2DDistance(Vector3 position1, Vector3 position2)
	{
		return (new Vector2(position1.x, position1.y) - new Vector2(position2.x, position2.y)).magnitude;
	}


	public static Color DarkenColor(Color color, float lightness)
	{
		color.r *= lightness;
		color.g *= lightness;
		color.b *= lightness;
		return color;
	}

}
 