﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class PersistentGridGizmo : MonoBehaviour
{
    public bool alwaysDraw = true;
    public Grid grid;
    public int min;
    public int max;
    private void OnDrawGizmos()
    {
        if (!alwaysDraw || grid == null)
            return;

        //TODO: support the other types and swizzles
        if (grid.cellLayout != GridLayout.CellLayout.Hexagon)
            return;

        if (grid.cellSwizzle != GridLayout.CellSwizzle.YZX)
            return;

        for (int x = min; x < max; x++)
        {
            for (int y = min; y < max; y++)
            {
                DrawHexagon(x, y, 0);
            }
        }
    }

    void DrawHexagon(int x, int y, int z)
    {
        float width = grid.cellSize.y;
        float height = grid.cellSize.x;

#if UNITY_EDITOR
        Vector3[] corners = new Vector3[7];
        corners[0] = Vector3.right * (width / 2);
        corners[1] = new Vector3(width / 4, 0, -height / 2);
        corners[2] = new Vector3(-width / 4, 0, -height / 2);
        corners[3] = Vector3.right * (width / -2);
        corners[4] = new Vector3(-width / 4, 0, height / 2);
        corners[5] = new Vector3(width / 4, 0, height / 2);
        corners[6] = corners[0];

        for (int i = 0; i < corners.Length; i++)
            corners[i] = grid.transform.TransformPoint(grid.CellToLocal(new Vector3Int(x, y, z)) + corners[i]);

        Handles.color = new Color(1, 1, 1, 0.1f);
        Handles.DrawPolyLine(corners);

#endif
    }
}
