import java.util.*;

public class RoomManager {
    private static final int MAP_SIZE = 1000;
    private Room[][] map;

    public RoomManager() {
        map = new Room[MAP_SIZE][MAP_SIZE];
        initializeRooms();
        generateMapFeatures();
        linkRooms();
    }

    // Initialize rooms in a 1000x1000 grid
    private void initializeRooms() {
        for (int x = 0; x < MAP_SIZE; x++) {
            for (int y = 0; y < MAP_SIZE; y++) {
                map[x][y] = new Room(x, y, "A plain open area.");
            }
        }
    }

    // Generate features for rooms (cities, forests, rivers, etc.)
    private void generateMapFeatures() {
        Random rand = new Random();

        // Example: Create cities
        for (int i = 0; i < 10; i++) {
            int x = rand.nextInt(MAP_SIZE);
            int y = rand.nextInt(MAP_SIZE);
            map[x][y].setFeature("city");
            map[x][y].setDescription("A bustling city with walls and gates.");
        }

        // Example: Create forests
        for (int i = 0; i < 20; i++) {
            int x = rand.nextInt(MAP_SIZE);
            int y = rand.nextInt(MAP_SIZE);
            map[x][y].setFeature("forest");
            map[x][y].setDescription("A dense forest, dark and mysterious.");
        }

        // Example: Create rivers
        for (int i = 0; i < 5; i++) {
            int x = rand.nextInt(MAP_SIZE);
            int y = rand.nextInt(MAP_SIZE);
            map[x][y].setFeature("river");
            map[x][y].setDescription("A wide, fast-moving river.");
        }

        // Example: Create mountains
        for (int i = 0; i < 5; i++) {
            int x = rand.nextInt(MAP_SIZE);
            int y = rand.nextInt(MAP_SIZE);
            map[x][y].setFeature("mountain");
            map[x][y].setDescription("A towering mountain, almost impassable.");
        }
    }

    // Link rooms (basic cardinal directions: north, south, east, west)
    private void linkRooms() {
        for (int x = 0; x < MAP_SIZE; x++) {
            for (int y = 0; y < MAP_SIZE; y++) {
                Room currentRoom = map[x][y];
                if (x > 0) {
                    currentRoom.setExit("west", map[x - 1][y]);
                }
                if (x < MAP_SIZE - 1) {
                    currentRoom.setExit("east", map[x + 1][y]);
                }
                if (y > 0) {
                    currentRoom.setExit("north", map[x][y - 1]);
                }
                if (y < MAP_SIZE - 1) {
                    currentRoom.setExit("south", map[x][y + 1]);
                }
            }
        }
    }

    // Get the room at specific coordinates
    public Room getRoom(int x, int y) {
        if (x >= 0 && x < MAP_SIZE && y >= 0 && y < MAP_SIZE) {
            return map[x][y];
        }
        return null;  // Out of bounds
    }

    // Print a description of the map area (for testing)
    public void printMapArea(int xStart, int yStart, int width, int height) {
        for (int x = xStart; x < xStart + width; x++) {
            for (int y = yStart; y < yStart + height; y++) {
                Room room = getRoom(x, y);
                if (room != null) {
                    System.out.println("Room at (" + x + ", " + y + "): " + room.getDescription());
                }
            }
        }
    }
}
