/////////////////////////////////////////////////////////////////////////////////////////////////
// GF - Debug
// various debug helpers
/////////////////////////////////////////////////////////////////////////////////////////////////
using UnityEngine;
using System.Collections;

public class GFDebug
{
    public static Vector2 ScreenToViewport(Vector2 p)
    {
        return new Vector2(1.0f - 2.0f * p.x / Screen.width, 1.0f - 2.0f * p.y / Screen.height);
    }

    public static Vector2 ViewportToScreen(Vector2 p)
    {
        return new Vector2((1.0f + p.x / 2.0f ) * Screen.width, (1.0f + p.y / 2.0f) * Screen.height);
    }

    public static Vector3 ViewportToWorld(Vector2 p, float depth = 0.5f)
    {
        return Camera.main.ViewportToWorldPoint(new Vector3(p.x, p.y, depth));
    }

    public static Vector2 WorldToViewport(Vector3 p)
    {
        Vector3 pp = Camera.main.WorldToViewportPoint(p);
        return new Vector2(pp.x, pp.y);
    }

    public static Vector3 ScreenToWorld(Vector2 p, float depth = 0.5f)
    {
        Vector3 pp = new Vector3(p.x, p.y, depth);
        return Camera.main.ScreenToWorldPoint(pp);
    }

    public static Vector2 WorldToScreen(Vector3 p)
    {
        Vector3 pp = Camera.main.WorldToScreenPoint(p);
        return new Vector2(pp.x, pp.y);
    }

    public static void Draw2DRect(Vector2 center, float w, float h, Color color)
	{
        float depth = Camera.main.nearClipPlane + 0.05f;

        Vector3 a1 = ScreenToWorld(center + new Vector2(-w / 2.0f, -h / 2.0f), depth);
        Vector3 a2 = ScreenToWorld(center + new Vector2( w / 2.0f, -h / 2.0f), depth);
        Vector3 a3 = ScreenToWorld(center + new Vector2( w / 2.0f,  h / 2.0f), depth);
        Vector3 a4 = ScreenToWorld(center + new Vector2(-w / 2.0f,  h / 2.0f), depth);

        Debug.DrawLine(a1, a2, color);
        Debug.DrawLine(a2, a3, color);
        Debug.DrawLine(a3, a4, color);
        Debug.DrawLine(a4, a1, color);
    }

    public static void Draw2DDot(Vector2 center, Color color)
    {
        float size = 4.0f;
        Draw2DRect(center, size, size, color);
    }

    public static void Draw2DRect(Rect rect, Color color)
    {
        Draw2DRect(rect.center, rect.width, rect.height, color);
    }

    public static void DrawDot(Vector3 center, Color color)
    {
        //Draw2DDot(WorldToScreen(center), color);
        const float size = 0.2f;
        Vector3 h = Camera.main.transform.right * size;
        Vector3 v = Camera.main.transform.up * size;
        Debug.DrawLine(center - h, center + h, color);
        Debug.DrawLine(center - v, center + v, color);
    }
}