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

[System.Serializable]
public class Room
{
    Vector3Int startingTile;
    // Size not including walls
    Vector2Int size;
    Tilemap tileMap;
    int roomType = 0;

    // Constructor
    public Room(Vector3Int startingTile, Vector2Int size, Tilemap tileMap)
    {
        this.startingTile = startingTile;
        this.size = size;
        this.tileMap = tileMap;
    }

    // Draws the starting tile using the Red tile
    public void drawStartingTile()
    {
        MyUtils.drawTile(tileMap, startingTile, "Red");
    }

    // Draws the walls of the Room using Ground tiles
    public void drawWalls()
    {
        int x = startingTile.x;
        int y = startingTile.y;

        // Top
        for (x = startingTile.x; x < (startingTile.x + size.x + 2); x++)
        {
            MyUtils.drawTile(tileMap, new Vector3Int(x, y, 0), "Ground");
        }
        x--;
        // Right
        for (y = startingTile.y - 1; y > (startingTile.y - size.y - 2); y--)
        {
            MyUtils.drawTile(tileMap, new Vector3Int(x, y, 0), "Ground");
        }
        x = startingTile.x;
        y = startingTile.y;
        // Left
        for (y = startingTile.y - 1; y > (startingTile.y - size.y - 2); y--)
        {
            MyUtils.drawTile(tileMap, new Vector3Int(x, y, 0), "Ground");
        }
        y++;
        // Bottom
        for (x = startingTile.x + 1; x < (startingTile.x + size.x + 2); x++)
        {
            MyUtils.drawTile(tileMap, new Vector3Int(x, y, 0), "Ground");
        }
    }

    // Room type access
    public void setRoomType(int type)
    {
        roomType = type;
    }
    public int getRoomType()
    {
        return roomType;
    }

    // Draws the center tiles with the given tile
    public void drawCenter(string tile)
    {
        Vector2 center = MyUtils.getRoomCenter(startingTile,size);
        //Debug.Log("Start: " + startingTile.x + " - " + startingTile.y);
        //Debug.Log("Center: " + center.x + " - " + center.y);

        MyUtils.drawTile(tileMap, new Vector3Int((int)center.x, (int)center.y, 0), tile);
        if (size.x % 2 == 0)
        {
            MyUtils.drawTile(tileMap, new Vector3Int((int)center.x - 1, (int)center.y, 0), tile);
            if (size.y % 2 == 0) MyUtils.drawTile(tileMap, new Vector3Int((int)center.x - 1, (int)center.y + 1, 0), tile);
        }
        if (size.y % 2 == 0) MyUtils.drawTile(tileMap, new Vector3Int((int)center.x, (int)center.y + 1, 0), tile);
    }

    public void drawRoom()
    {
        GameObject room;
        GameObject roomGO;

        switch (roomType)
        {
            case 1:
                // Loads the room data
                room = Resources.Load<GameObject>("Rooms/10x7/Start/R1_1");
                // Creates a GameObject to give the room
                roomGO = GameObject.Instantiate(room, tileMap.transform.position, tileMap.transform.rotation) as GameObject;
                // Makes the room a child of the tileMap
                roomGO.transform.parent = tileMap.transform;
                // Moves the tiles where the room is
                roomGO.transform.position = new Vector3(startingTile.x, startingTile.y, 0);
                // Open walls
                actionWall(0, false);
                actionWall(1, false);
                actionWall(3, false);
                break;
            case 2:
                // Loads the room data
                switch (Random.Range(1, 8))
                {
                    case 1:
                        room = Resources.Load<GameObject>("Rooms/10x7/Normal/R2_1");
                        break;
                    case 2:
                        room = Resources.Load<GameObject>("Rooms/10x7/Normal/R2_2");
                        break;
                    case 3:
                        room = Resources.Load<GameObject>("Rooms/10x7/Normal/R2_3");
                        break;
                    case 4:
                        room = Resources.Load<GameObject>("Rooms/10x7/Normal/R2_4");
                        break;
                    case 5:
                        room = Resources.Load<GameObject>("Rooms/10x7/Normal/R2_5");
                        break;
                    case 6:
                        room = Resources.Load<GameObject>("Rooms/10x7/Normal/R2_6");
                        break;
                    case 7:
                        room = Resources.Load<GameObject>("Rooms/10x7/Normal/R2_7");
                        break;
                    default:
                        room = Resources.Load<GameObject>("Rooms/10x7/Normal/R2_1");
                        break;

                }
                // Creates a GameObject to give the room
                roomGO = GameObject.Instantiate(room, tileMap.transform.position, tileMap.transform.rotation) as GameObject;
                // Makes the room a child of the tileMap
                roomGO.transform.parent = tileMap.transform;
                // Moves the tiles where the room is
                roomGO.transform.position = new Vector3(startingTile.x, startingTile.y, 0);
                // Open walls
                actionWall(1, false);
                actionWall(3, false);
                break;
            case 3:
                // Loads the room data
                room = Resources.Load<GameObject>("Rooms/10x7/Down/R3_1");
                // Creates a GameObject to give the room
                roomGO = GameObject.Instantiate(room, tileMap.transform.position, tileMap.transform.rotation) as GameObject;
                // Makes the room a child of the tileMap
                roomGO.transform.parent = tileMap.transform;
                // Moves the tiles where the room is
                roomGO.transform.position = new Vector3(startingTile.x, startingTile.y, 0);
                // Open walls
                actionWall(1, false);
                actionWall(2, false);
                actionWall(3, false);
                break;
            case 4:
                // Loads the room data
                room = Resources.Load<GameObject>("Rooms/10x7/Up/R4_1");
                // Creates a GameObject to give the room
                roomGO = GameObject.Instantiate(room, tileMap.transform.position, tileMap.transform.rotation) as GameObject;
                // Makes the room a child of the tileMap
                roomGO.transform.parent = tileMap.transform;
                // Moves the tiles where the room is
                roomGO.transform.position = new Vector3(startingTile.x, startingTile.y, 0);
                // Open walls
                actionWall(0, false);
                actionWall(1, false);
                actionWall(3, false);
                break;
            case 5:
                // Loads the room data
                room = Resources.Load<GameObject>("Rooms/10x7/Finish/R5_1");
                // Creates a GameObject to give the room
                roomGO = GameObject.Instantiate(room, tileMap.transform.position, tileMap.transform.rotation) as GameObject;
                // Makes the room a child of the tileMap
                roomGO.transform.parent = tileMap.transform;
                // Moves the tiles where the room is
                roomGO.transform.position = new Vector3(startingTile.x, startingTile.y, 0);
                // Open walls
                actionWall(1, false);
                actionWall(2, false);
                actionWall(3, false);
                break;
            case 0:
                // Loads the room data
                room = Resources.Load<GameObject>("Rooms/10x7/Fill");
                // Creates a GameObject to give the room
                roomGO = GameObject.Instantiate(room, tileMap.transform.position, tileMap.transform.rotation) as GameObject;
                // Makes the room a child of the tileMap
                roomGO.transform.parent = tileMap.transform;
                // Moves the tiles where the room is
                roomGO.transform.position = new Vector3(startingTile.x, startingTile.y, 0);
                // Open walls
                actionWall(1, false);
                actionWall(3, false);
                break;
            default:
                break;
        }
    }

    // Actions a wall of the room (0 to 3, clockwise, 0 is top)
    // If close, wall is closed, if !close, wall is empty
    public void actionWall(int side, bool close)
    {
        Vector3Int pos = new Vector3Int (0,0,0);
        switch (side)
        {
            case 0:
                pos.x = MyUtils.getRoomCenter(startingTile, size).x;
                pos.y = startingTile.y;
                //MyUtils.drawTile(tileMap, pos, "Highlight");
                if (close) MyUtils.drawTile(tileMap, pos, "Ground");
                else { tileMap.SetTile(pos, null); }
                if (size.x%2 == 0){
                    pos.x--;
                    //MyUtils.drawTile(tileMap, pos, "Highlight");
                    if (close) MyUtils.drawTile(tileMap, pos, "Ground");
                    else { tileMap.SetTile(pos, null); }
                }
                break;
            case 1:
                pos.x = startingTile.x + size.x + 1;
                pos.y = startingTile.y - size.y + 1;
                //MyUtils.drawTile(tileMap, pos, "Highlight");
                if (close) MyUtils.drawTile(tileMap, pos, "Ground");
                else { tileMap.SetTile(pos, null); }
                pos.y++;
                //MyUtils.drawTile(tileMap, pos, "Highlight");
                if (close) MyUtils.drawTile(tileMap, pos, "Ground");
                else { tileMap.SetTile(pos, null); }
                break;
            case 2:
                pos.x = MyUtils.getRoomCenter(startingTile, size).x;
                pos.y = startingTile.y - size.y - 1;
                //MyUtils.drawTile(tileMap, pos, "Highlight");
                if (close) MyUtils.drawTile(tileMap, pos, "Ground");
                else { tileMap.SetTile(pos, null); }
                if (size.x % 2 == 0)
                {
                    pos.x--;
                    //MyUtils.drawTile(tileMap, pos, "Highlight");
                    if (close) MyUtils.drawTile(tileMap, pos, "Ground");
                    else { tileMap.SetTile(pos, null); }
                }
                break;
            case 3:
                pos.x = startingTile.x;
                pos.y = startingTile.y - size.y + 1;
                //MyUtils.drawTile(tileMap, pos, "Highlight");
                if (close) MyUtils.drawTile(tileMap, pos, "Ground");
                else { tileMap.SetTile(pos, null); }
                pos.y++;
                //MyUtils.drawTile(tileMap, pos, "Highlight");
                if (close) MyUtils.drawTile(tileMap, pos, "Ground");
                else { tileMap.SetTile(pos, null); }
                break;
            default:
                break;
        }
    }
}
