using UnityEngine;
using System.Collections;

public struct GridCoord
{
    public int x;
    public int y;

    public GridCoord( int newX, int newY )
    {
        x = newX;
        y = newY;
    }

    public static bool operator ==(GridCoord one, GridCoord two)
    {
        return (one.x == two.x) && (one.y == two.y);
    }
    public static bool operator !=(GridCoord one, GridCoord two)
    {
        return (one.x != two.x) || (one.y != two.y);
    }

    public override bool Equals(object o)
    {
        return (x == ((GridCoord)o).x) && (y == ((GridCoord)o).y);
    }

    public override int GetHashCode()
    {
        return x + ( y * GridManager.MAP_SIZE_X );
    }

    public Vector3 GetUnityPos()
    {
        // TODO: Precalc these
        float cornerXPos = -((GridManager.UNITY_GRID_SIZE * GridManager.MAP_SIZE_X) / 2.0f) + (GridManager.UNITY_GRID_SIZE / 2.0f);
        float cornerYPos = -((GridManager.UNITY_GRID_SIZE * GridManager.MAP_SIZE_Y) / 2.0f) + (GridManager.UNITY_GRID_SIZE / 2.0f);

        return new Vector3(
            cornerXPos + (x * GridManager.UNITY_GRID_SIZE),
            GridManager.UNITY_BOARD_DEPTH / 2.0f + GridManager.UNITY_CHARACTER_DEPTH / 2.0f,
            cornerYPos + (y * GridManager.UNITY_GRID_SIZE)
            );
    }

    public Vector2 ToVector()
    {
        return new Vector2(x, y);
    }

    static public float GetStraightDistanceBetween( GridCoord one, GridCoord two )
    {
        return Vector2.Distance( one.ToVector(), two.ToVector());
    }

    static public int GetStepsBetween(GridCoord one, GridCoord two)
    {
        int steps = 0;

        while ( steps < 100 )
        {
            if ( one == two )
            {
                return steps;
            }

            int xDelta = (one.x - two.x);
            int yDelta = (one.y - two.y);

            if (Mathf.Abs(xDelta) > Mathf.Abs(yDelta))
            {
                if (one.x < two.x)
                {
                    one.x++;
                }
                else
                {
                    one.x--;
                }
            }
            else
            {
                if (one.y < two.y)
                {
                    one.y++;
                }
                else
                {
                    one.y--;
                }
            }
            steps++;
        }
        return steps;
    }

}
