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

public static class Helper
{
	public static Vector2[] GetStandardUVMapping (IList<Vector2> planeVerticies, Material material)
	{
		float textureHeight = 1;
		float textureWidth = 1;
		
		float xTiling = material.mainTextureScale.x;
		float yTiling = material.mainTextureScale.y;
		
		// The uv tiling also has to factor in the height and width of the textures we are using
		if (material.mainTexture != null) {	        
			textureHeight = material.mainTexture.height;
			textureWidth = material.mainTexture.width;
		}
		
		// Tile the texture as needed
		textureWidth = textureWidth / xTiling;
		textureHeight = textureHeight / yTiling;
		
		// Track how far along we are on the top texture mapping
		float currentTopTextureX = 0;
		
		Vector2[] uvs = new Vector2[planeVerticies.Count];
		for (int i = 0; i < planeVerticies.Count; i++) {
			Vector3 vertex = planeVerticies [i];
			Vector3 previousVertex = Vector3.zero;
			if (i > 0) {
				previousVertex = planeVerticies [i - 1];
			}
			
			// Our standard uv mapping is just our point in space divided by the width and the height of our texture (assuming an x/y plane)
			float xMapping = vertex.x / textureWidth;
			float yMapping = vertex.y / textureHeight;
						
			// Finally set the actual uv mapping
			Vector2 uv = new Vector2 (xMapping, yMapping);
			
			uvs [i] = uv;
		}
		
		return uvs;
	}

	public static Vector2[] GetStandardUVMapping (IList<Vector3> planeVerticies, Material material)
	{
		float textureHeight = 1;
		float textureWidth = 1;
		
		float xTiling = material.mainTextureScale.x;
		float yTiling = material.mainTextureScale.y;
		
		// The uv tiling also has to factor in the height and width of the textures we are using
		if (material.mainTexture != null) {	        
			textureHeight = material.mainTexture.height;
			textureWidth = material.mainTexture.width;
		}
		
		// Tile the texture as needed
		textureWidth = textureWidth / xTiling;
		textureHeight = textureHeight / yTiling;
		
		// Track how far along we are on the top texture mapping
		float currentTopTextureX = 0;
		
		Vector2[] uvs = new Vector2[planeVerticies.Count];
		for (int i = 0; i < planeVerticies.Count; i++) {
			Vector3 vertex = planeVerticies [i];
			Vector3 previousVertex = Vector3.zero;
			if (i > 0) {
				previousVertex = planeVerticies [i - 1];
			}
			
			// Our standard uv mapping is just our point in space divided by the width and the height of our texture (assuming an x/y plane)
			float xMapping = vertex.x / textureWidth;
			float yMapping = vertex.y / textureHeight;
			
			// Finally set the actual uv mapping
			Vector2 uv = new Vector2 (xMapping, yMapping);
			
			uvs [i] = uv;
		}
		
		return uvs;
	}

	public static float TriangleArea (Vector2 p1, Vector2 p2, Vector2 p3)
	{
		float result = (p1.x * p2.y + p3.x * p1.y + p2.x * p3.y - p2.x * p1.y - p3.x * p2.y - p1.x * p3.y) / 2f;
		return result;
	}

	public static float AngleBetween (Vector2 v1, Vector2 v2)
	{
		var angle = Vector2.Angle (v1, v2);
		if (TriangleArea (Vector2.zero, v1, v1 + v2) < 0f) {
			angle = -angle;
		}
		return angle;
	}

	public static Vector2 GetPositionInQuadByRatios (float horizontalRatio, float verticalRatio, Vector2 downStart, Vector2 downEnd, Vector2 upStart, Vector2 upEnd)
	{
		var down = downStart + (downEnd - downStart) * horizontalRatio;
		var up = upStart + (upEnd - upStart) * horizontalRatio;
		var result = down + (up - down) * verticalRatio;
		return result;
	}

	public static Color32 ColorFromRgbaHex (string hex)
	{
		var startIndex = 0;
		if (hex.StartsWith ("#") == true) {
			startIndex = 1;
		} else if (hex.StartsWith ("0x") == true) {
			startIndex = 2;
		}

		byte r = byte.Parse (hex.Substring (startIndex + 0, 2), System.Globalization.NumberStyles.HexNumber);
		byte g = byte.Parse (hex.Substring (startIndex + 2, 2), System.Globalization.NumberStyles.HexNumber);
		byte b = byte.Parse (hex.Substring (startIndex + 4, 2), System.Globalization.NumberStyles.HexNumber);

		byte a = 255;
		if (hex.Length == startIndex + 8) {
			a = byte.Parse (hex.Substring (startIndex + 6, 2), System.Globalization.NumberStyles.HexNumber);
		}

		return new Color32 (r, g, b, a);
	}
}
