using UnityEngine;
using UnityEngine.Tilemaps;

public class Maze_Controller : MonoBehaviour
{
    //DEBUG
    public bool drawRoomWalls, drawRoomCenter, drawPath, drawRooms, debugControls;
    //public GameObject myPref;

    // Data for room grid
    public Vector3Int gridStartingTile = new Vector3Int(0,0,0);
    //public GameObject myGrid;
    public Tilemap myMap;

    // Grid
    private RoomGrid roomGrid;
    public Vector2Int gridSize = new Vector2Int(2, 2);
    public Vector2Int roomSize = new Vector2Int(3, 3);

    // Start is called before the first frame update
    void Start()
    {
        myMap = GetComponent<Tilemap>();

        roomGrid = new RoomGrid(gridStartingTile, gridSize, roomSize, myMap);

        roomGrid.generatePath();

        if (drawRoomWalls) roomGrid.drawRoomWalls();
        if (drawRoomCenter) roomGrid.drawRoomCenter("red");
        if (drawPath) roomGrid.drawPath();
        if (drawRooms) roomGrid.drawRooms();


        /*
         * // Attach prefab to a parent GameObject
        GameObject obj;
        obj = (GameObject)Instantiate(myPref, myGrid.transform.position, myGrid.transform.rotation);
        // child the object to MyShip:
        obj.transform.parent = myGrid.transform;
        */

        //MyUtils.drawTile(myMap, new Vector3Int(0, 0, 0), "Ground");

    }

    // Update is called once per frame
    void Update()
    {
        if (debugControls)
        {
            if (Input.GetKeyDown("space"))
            {
                myMap.ClearAllTiles();
                roomGrid.removeTileMaps();
                roomGrid.generatePath();
                if (drawRoomWalls) roomGrid.drawRoomWalls();
                if (drawRoomCenter) roomGrid.drawRoomCenter("red");
                if (drawPath) roomGrid.drawPath();
                if (drawRooms) roomGrid.drawRooms();
            }
            if (Input.GetKeyDown("d"))
            {
                myMap.ClearAllTiles();
                roomGrid.removeTileMaps();
            }
        }
    }

    public void redoMaze()
    {
        myMap.ClearAllTiles();
        roomGrid.removeTileMaps();

        myMap.ClearAllTiles();
        roomGrid.removeTileMaps();
        roomGrid.generatePath();
        if (drawRoomWalls) roomGrid.drawRoomWalls();
        if (drawRoomCenter) roomGrid.drawRoomCenter("red");
        if (drawPath) roomGrid.drawPath();
        if (drawRooms) roomGrid.drawRooms();
    }
}
