package com.tiedup.remake.worldgen;

enum RoomLayout {
    SQUARE {
        @Override
        public boolean isInShape(int rx, int rz) {
            return rx >= 0 && rx <= 12 && rz >= 0 && rz <= 12;
        }
    },
    OCTAGONAL {
        @Override
        public boolean isInShape(int rx, int rz) {
            if (rx < 0 || rx > 12 || rz < 0 || rz > 12) return false;
            if (rx + rz <= 2) return false;
            if ((12 - rx) + rz <= 2) return false;
            if (rx + (12 - rz) <= 2) return false;
            if ((12 - rx) + (12 - rz) <= 2) return false;
            return true;
        }
    },
    CIRCULAR {
        @Override
        public boolean isInShape(int rx, int rz) {
            if (rx < 0 || rx > 12 || rz < 0 || rz > 12) return false;
            int dx = rx - 6;
            int dz = rz - 6;
            return dx * dx + dz * dz <= 36;
        }

        @Override
        public int[][] innerCorners() {
            return new int[][] { { 6, 1 }, { 6, 11 }, { 1, 6 }, { 11, 6 } };
        }
    },
    CROSS {
        @Override
        public boolean isInShape(int rx, int rz) {
            if (rx < 0 || rx > 12 || rz < 0 || rz > 12) return false;
            return (rx >= 3 && rx <= 9) || (rz >= 3 && rz <= 9);
        }

        @Override
        public int[][] innerCorners() {
            return new int[][] { { 3, 3 }, { 3, 9 }, { 9, 3 }, { 9, 9 } };
        }
    };

    public abstract boolean isInShape(int rx, int rz);

    public boolean isWall(int rx, int rz) {
        if (!isInShape(rx, rz)) return false;
        return (
            !isInShape(rx - 1, rz) ||
            !isInShape(rx + 1, rz) ||
            !isInShape(rx, rz - 1) ||
            !isInShape(rx, rz + 1)
        );
    }

    /** Is this interior position adjacent to a wall? */
    public boolean isWallAdjacent(int rx, int rz) {
        if (!isInShape(rx, rz) || isWall(rx, rz)) return false;
        return (
            isWall(rx - 1, rz) ||
            isWall(rx + 1, rz) ||
            isWall(rx, rz - 1) ||
            isWall(rx, rz + 1)
        );
    }

    /** Returns the 4 innermost corner positions for this layout. */
    public int[][] innerCorners() {
        return switch (this) {
            case SQUARE -> new int[][] {
                { 1, 1 },
                { 1, 11 },
                { 11, 1 },
                { 11, 11 },
            };
            case OCTAGONAL -> new int[][] {
                { 3, 1 },
                { 1, 9 },
                { 11, 3 },
                { 9, 11 },
            };
            default -> new int[][] {
                { 1, 1 },
                { 1, 11 },
                { 11, 1 },
                { 11, 11 },
            };
        };
    }
}
