package eu.mhsl.minenet.minigames.util;

import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;

public class Position {
    public static final double PIXEL = 0.0625;

    public static Pos getPosFromCommaSeparated(String input) {
        String[] coordinates = input.split(",");
        if(coordinates.length < 3) throw new InputMismatchException("Coordinates have to contain at least x, y and z");
        float x = Float.parseFloat(coordinates[0]);
        float y = Float.parseFloat(coordinates[1]);
        float z = Float.parseFloat(coordinates[2]);

        if(coordinates.length > 4) {
            float yaw = Float.parseFloat(coordinates[3]);
            float pitch = Float.parseFloat(coordinates[4]);

            return new Pos(x, y, z, yaw, pitch);
        }

        return new Pos(x, y, z);
    }

    public static List<Block> blocksBelowPlayer(Instance instance, Player p) {
        Point playerPos = p.getPosition();
        List<Block> blocks = new ArrayList<>();
        GeneratorUtils.foreachXZ(playerPos.sub(0.5, 1, 0.5), playerPos.add(0.5, -1, 0.5), point -> {
            blocks.add(instance.getBlock(point));
        });
        return blocks.stream().distinct().toList();
    }
}
