using UnityEngine;
using System.Collections;

public enum SpriteOrientation
{
    TopLeft,
    MiddleCenter
}

[ExecuteInEditMode]
public class Sprite2D : MonoBehaviour 
{
    public Camera sourceCamera;
	public Vector2 spriteSize;
    public Vector2 spriteLowerLeft;
    public string textureName;
    public float defCameraPixels = 768;
    public SpriteOrientation spriteOrientation = SpriteOrientation.MiddleCenter;

    protected Mesh _mesh;
    protected MeshRenderer _meshRenderer;

    protected float texWidth;
    protected float texHeight;
	
	private float pixelPerWorldUnit;

    void Awake()
    {
		if(sourceCamera == null)
			sourceCamera = Camera.mainCamera;
		
        MeshFilter _meshFilter = gameObject.GetComponent<MeshFilter>();
        if (_meshFilter == null)
            _meshFilter = gameObject.AddComponent<MeshFilter>();

        _meshRenderer = gameObject.GetComponent<MeshRenderer>();
        if (_meshRenderer == null)
            _meshRenderer = gameObject.AddComponent<MeshRenderer>();

        _meshRenderer.material = new Material(Shader.Find("Unlit/Transparent"));
        _meshRenderer.sharedMaterial.mainTexture = (Texture2D)Resources.Load("Textures/" + textureName);

        texWidth = _meshRenderer.sharedMaterial.mainTexture.width;
        texHeight = _meshRenderer.sharedMaterial.mainTexture.height;
        pixelPerWorldUnit = sourceCamera.orthographicSize * 2 / defCameraPixels;

        Mesh mesh = new Mesh();

        if (spriteOrientation == SpriteOrientation.MiddleCenter)
        {
            mesh.vertices = new Vector3[] {
                new Vector3(-spriteSize.x, -spriteSize.y) * 0.5f * pixelPerWorldUnit,
                new Vector3(-spriteSize.x, spriteSize.y) * 0.5f * pixelPerWorldUnit,
                new Vector3(spriteSize.x, -spriteSize.y) * 0.5f * pixelPerWorldUnit,
                new Vector3(spriteSize.x, spriteSize.y) * 0.5f * pixelPerWorldUnit
            };
        }

        else if (spriteOrientation == SpriteOrientation.TopLeft)
        {
            mesh.vertices = new Vector3[] {
                new Vector3(0f, -spriteSize.y) * pixelPerWorldUnit,
                new Vector3(0f, 0f) * pixelPerWorldUnit,
                new Vector3(spriteSize.x, -spriteSize.y) * pixelPerWorldUnit,
                new Vector3(spriteSize.x, 0f) * pixelPerWorldUnit
            };
        }

        mesh.triangles = new int[] { 0, 1, 3, 0, 3, 2 };
        mesh.uv = new Vector2[]{
            new Vector2(1f / texWidth * spriteLowerLeft.x, 1f - 1f / texHeight * spriteLowerLeft.y),
            new Vector2(1f / texWidth * spriteLowerLeft.x, 1f - 1f / texHeight * (spriteLowerLeft.y - spriteSize.y)),
            new Vector2(1f / texWidth * (spriteLowerLeft.x + spriteSize.x), 1f - 1f / texHeight * spriteLowerLeft.y),
            new Vector2(1f / texWidth * (spriteLowerLeft.x + spriteSize.x), 1f - 1f / texHeight * (spriteLowerLeft.y - spriteSize.y))
        };

        mesh.Optimize();
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
        _meshFilter.sharedMesh = mesh;
        _mesh = mesh;
    }

    void OnDrawGizmos()
    {
        if (_mesh != null)
        {
            Gizmos.color = new Color(0f, 0f, 1f, 0.3f);
            Bounds bound = _mesh.bounds;
            Gizmos.DrawCube(transform.position + bound.center, bound.size);
        }
    }
}