package com.zivilon.cinder_loe.util;

import net.minecraft.block.Block;
import net.minecraft.block.BlockBush;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import java.util.ArrayList;
import java.util.Random;

public class BlockPos {
    public int x, y, z;

    public BlockPos(int x, int y, int z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

	public BlockPos down() {
		return new BlockPos(x, y - 1, z);
	}

	public static BlockPos findSafeSpawnLocation(World world, EntityPlayer entityPlayer) {
	    Random rand = new Random();
	    ArrayList<BlockPos> validPositions = new ArrayList<BlockPos>();
	    int radius = 5; // 5-block radius

	    int originX = MathHelper.floor_double(entityPlayer.posX);
	    int originY = MathHelper.floor_double(entityPlayer.posY);
	    int originZ = MathHelper.floor_double(entityPlayer.posZ);

	    for (int x = originX - radius; x <= originX + radius; x++) {
	        for (int z = originZ - radius; z <= originZ + radius; z++) {
	            for (int y = originY - radius; y <= originY + radius; y++) {
	                Block block = world.getBlock(x, y, z);
	                Block blockAbove = world.getBlock(x, y + 1, z);

	                // Check if the block is solid and there's air above it
	                if (!block.isAir(world, x, y, z) && !(block instanceof BlockBush) && (blockAbove.isAir(world, x, y + 1, z) || (blockAbove instanceof BlockBush))) {
	                    validPositions.add(new BlockPos(x, y + 1, z)); // Add the position above the solid block
	                }
	            }
	        }
	    }
	    if (validPositions.isEmpty()) {
	        return null; // No valid position found
	    }

	    // Select a random valid position
	    return validPositions.get(rand.nextInt(validPositions.size()));
	}

}
