using UnityEngine;

public class RoomV2
{
    public string roomFolder = "Rooms/";
    GameObject room;
    Vector3 startingLocation; // Bottom left corner
    Vector2Int size;
    EnumRoomType type;
    Transform parent;

    // Constructors
    public RoomV2(Transform parent, Vector3 startingLocation, Vector2Int size)
    {
        this.startingLocation = startingLocation;
        this.size = size;
        this.parent = parent;
    }
    public RoomV2(Transform parent, Vector3 startingLocation, Vector2Int size, EnumRoomType type)
    {
        this.startingLocation = startingLocation;
        this.size = size;
        this.type = type;
        this.parent = parent;
    }

    /*
    // Sets the data and creates the room, called from MazeController
    public void setData (Vector3 startingLocation, Vector2Int size, EnumRoomType type)
    {
        this.startingLocation = startingLocation;
        this.size = size;
        this.type = type;

        gameObject.transform.position = startingLocation;

        createRoom();
    }
    */

    // Getters & Setters
    public Vector2Int getStartingLocation()
    {
        return new Vector2Int((int) startingLocation.x, (int) startingLocation.y);
    }
    public EnumRoomType getType()
    {
        return type;
    }
    public void setType(EnumRoomType type)
    {
        this.type = type;
    }
    

    // Creates the room gameObject depending on the room type
    public void createRoom()
    {
        switch (type)
        {
            case EnumRoomType.START:
                instantiateRoom("/Start/R1_1_V2");
                break;
            case EnumRoomType.NORMAL:
                switch (Random.Range(1, 8))
                {
                    case 1:
                        instantiateRoom("/Normal/R2_1_V2");
                        break;
                    case 2:
                        instantiateRoom("/Normal/R2_2_V2");
                        break;
                    case 3:
                        instantiateRoom("/Normal/R2_3_V2");
                        break;
                    case 4:
                        instantiateRoom("/Normal/R2_4_V2");
                        break;
                    case 5:
                        instantiateRoom("/Normal/R2_5_V2");
                        break;
                    case 6:
                        instantiateRoom("/Normal/R2_6_V2");
                        break;
                    case 7:
                        instantiateRoom("/Normal/R2_7_V2");
                        break;
                    default:
                        instantiateRoom("/Normal/R2_1_V2");
                        break;
                }
                break;
            case EnumRoomType.DOWN:
                instantiateRoom("/Down/R3_1_V2");
                break;
            case EnumRoomType.UP:
                instantiateRoom("/Up/R4_1_V2");
                break;
            case EnumRoomType.FINISH:
                instantiateRoom("/Finish/R5_1_V2");
                break;
            case EnumRoomType.FILL:
                instantiateRoom("/Fill_V2");
                break;
            default:
                instantiateRoom("/Fill_V2");
                break;
        }
    }
    public void instantiateRoom(string roomName)
    {
        room = GameObject.Instantiate(Resources.Load<GameObject>(roomFolder + "" + size.x + "x" + size.y + roomName), parent);
        room.transform.position = startingLocation;
    }

    // Rerolls the room
    public void rerollRoom()
    {
        destroyGenerated();
        createRoom();
    }

    // Destroys the generated room/gameObjects
    public void destroyGenerated()
    {
            GameObject.Destroy(room.gameObject);
    }

}
