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

public class RoomSpawner : MonoBehaviour
{
    private GameManager gameManager;
    public enum Direction {North, South, East, West};
    public Direction direction;
    //private List<GameObject> rooms = new List<GameObject>();
    System.Random random = new System.Random();

    // Start is called before the first frame update
    void Start()
    {
        gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
        gameManager.roomSpawnpoints.Add(gameObject);
        //Invoke("SpawnRoom", 0.02f);
    }

    public bool SpawnRoom()
    {
        //check if room can spawn
        GameObject room = Instantiate(gameManager.rooms[gameManager.Random(gameManager.rooms.Count)]);
        switch (direction)
        {
            case Direction.North:
                room.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y + 7, 0);
                break;
            case Direction.South:
                room.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y - 7, 0);
                break;
            case Direction.East:
                room.transform.position = new Vector3(gameObject.transform.position.x + 9, gameObject.transform.position.y, 0);
                break;
            default:
                room.transform.position = new Vector3(gameObject.transform.position.x - 9, gameObject.transform.position.y, 0);
                break;
        }
        foreach (GameObject i in gameManager.spawnedRooms)
        {
            if (room.transform.position == i.transform.position)
            {
                Destroy(room);
                return false;//Returns false if there isn't enough space to spawn a room
            }
        }
        GameObject.Find("Game Manager").GetComponent<GameManager>().spawnedRooms.Add(room);
        return true;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
