using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GridManager : MonoBehaviour
{
    [SerializeField] private int gridWidth, gridHeight, xOffset, yOffset;
    [SerializeField] private Tile tilePrefab;
    [SerializeField] private new Transform camera;
    private Dictionary<Vector2, Tile> tiles;

    void Start()
    {
        GenerateGrid();
    }
    void GenerateGrid()
    {
        tiles = new Dictionary<Vector2, Tile>();
        for (int i = 0; i < gridWidth; i++)
        {
            for (int j = 0; j < gridHeight; j++)
            {
                var spawnedTile = Instantiate(tilePrefab, new Vector3(i + xOffset, j + yOffset), Quaternion.identity);
                spawnedTile.name = $"Tile ({i}, {j})";

                var gridColourVary = ((i + j) % 2 == 1);
                spawnedTile.TileColor(gridColourVary);

                tiles[new Vector2 (i, j)] = spawnedTile;
            }
        }
        camera.transform.position = new Vector3 (gridWidth/2f - 0.5f, gridHeight/2f - 0.5f, -10);
    }

    public Tile GetTile(Vector2 position)
    {
        if (tiles.TryGetValue(position, out var tile))
        {
            return tile;
        }
        else
        {
            return null;
        }
    }
}
