using Gamelogic.Grids;
using UnityEngine;

public class LayerTest : GLMonoBehaviour
{
	public GameObject cellPrefab;

	private LayeredGrid<GameObject, PointyHexPoint> grid;
	private SimpleLayeredMap<PointyHexPoint> map;

	public void Start()
	{
		map = new SimpleLayeredMap<PointyHexPoint>(new PointyHexMap(new Vector2(69, 80)*5f), 200, 0);

		var shapes = new []
		{
			PointyHexGrid<GameObject>.BeginShape().Hexagon(6),
			PointyHexGrid<GameObject>.BeginShape().Hexagon(5),
			PointyHexGrid<GameObject>.BeginShape().Hexagon(4),
			PointyHexGrid<GameObject>.BeginShape().Hexagon(3),
			PointyHexGrid<GameObject>.BeginShape().Hexagon(2),
			PointyHexGrid<GameObject>.BeginShape().Hexagon(1)
		};

		grid = LayeredGrid<GameObject, PointyHexPoint>.Make<
			PointyHexShapeInfo<GameObject>, 
			PointyHexGrid<GameObject>, 
			PointyHexPoint, PointyHexPoint, PointyHexOp<GameObject>>(shapes);

		foreach (LayeredPoint<PointyHexPoint> point in grid)
		{
			var cell = Instantiate(cellPrefab);

			cell.transform.parent = transform;
			cell.transform.localPosition = map[point];
			
			var color = ExampleUtils.colors[(point.point.GetColor2_4()) + 4];
			cell.renderer.material.color = color;

			grid[point] = cell;
		}
	}

	public void Update()
	{
		if (Input.GetMouseButtonDown(0))
		{
			var mousePosition = Input.mousePosition;
			var ray = Camera.main.ScreenPointToRay(mousePosition);

			RaycastHit hitInfo;

			if (Physics.Raycast(ray, out hitInfo))
			{
				var worldPoint = hitInfo.point;
				var gridPoint = map[worldPoint];

				if (grid.Contains(gridPoint))
				{
					grid[gridPoint].renderer.material.color = ExampleUtils.colors[7];
				}
			}
		}
	}
}
