using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public static class HelperFunctions {

	public static void setTextureColor(Texture2D t, Color c) {
		t.wrapMode = TextureWrapMode.Clamp;
		t.SetPixel(0,0, c);
		t.Apply();	
	}

	public static Texture2D[] getHealthTextures() {
		Texture2D[] tex = new Texture2D[4];
		for (int i = 0; i < tex.Length; i++) {
			Color c = new Color(1.0f*(3.0f-i)/3.0f, 1.0f*i/3.0f, 0.3f);
			tex[i] = new Texture2D(1,1);
			setTextureColor(tex[i], c);
		}
		return tex;
	}

	public static Texture2D[] getOtherTextures() {
		Texture2D[] tex = new Texture2D[3];
		for (int i = 0; i < 3; i++) {
			tex[i] = new Texture2D(1,1);
		}
		Color c  = new Color(0.3f, 0.3f, 1.0f);
		setTextureColor (tex [0], c);
		c = new Color(0.9f, 0.9f, 0.2f);
		setTextureColor (tex [1], c);
		c = new Color (1.0f, 0.1f, 0.1f, 0.4f);
		setTextureColor (tex [2], c);
		return tex;
	}

	public static float GetRotationAngle (Vector3 source, Vector3 dest)
	{
		return GetRotationAngle (new Vector2 (source.z, source.x), new Vector2 (dest.z, dest.x));
	}

	public static float GetRotationAngle (Vector2 source, Vector2 dest)
	{
		Vector2 diff = dest - source;
		diff.Normalize ();
		float angle = Mathf.Atan2 (diff.y, diff.x) * 180f / Mathf.PI;
		
		return ClampAngle (angle);
	}

	public static float ClampAngle (float angle)
	{
		if (angle > 180) {
			angle -= 360;
		} else if (angle < -180) {
			angle += 360;
		}
		return angle;
	}

	static public GameObject Instantiate (string path)
	{
		GameObject obj = GameObject.Instantiate (Resources.Load (path)) as GameObject;
		return obj;
	}

	static public float getScaledRandom(float min, float max) {
		return min + (max - min) * (float) Rand.r.NextDouble();
	}

	static public Vector2 GetRandomPosition() {
		return new Vector2(getScaledRandom(-10f, 10f), 2);
	}	
}