package eu.mhsl.minenet.minigames.world.generator.featureEnriched;

import eu.mhsl.minenet.minigames.util.GeneratorUtils;
import eu.mhsl.minenet.minigames.util.SingleExecution;
import eu.mhsl.minenet.minigames.world.BlockPallet;
import eu.mhsl.minenet.minigames.world.generator.terrain.HeightTerrainGenerator;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.generator.GenerationUnit;
import org.jetbrains.annotations.NotNull;

public class CircularPlateGenerator extends HeightTerrainGenerator {
    private final SingleExecution execution = SingleExecution.make();
    private final int radius;
    private final int height;
    private BlockPallet blockPallet = BlockPallet.GROUND;

    private int centerX = 0;
    private int centerZ = 0;

    private boolean generated = false;

    public CircularPlateGenerator(int radius, int height) {
        setCalculateHeight(point -> (int) ((point.distance(new Pos(0, point.y(), 0)) / 2) - radius));
        this.radius = radius;
        this.height = height;
    }

    public CircularPlateGenerator setBlockPallet(BlockPallet pallet) {
        this.blockPallet = pallet;
        return this;
    }

    public CircularPlateGenerator setCenter(int x, int z) {
        this.centerX = x;
        this.centerZ = z;
        return this;
    }

    @Override
    public void generate(@NotNull GenerationUnit unit) {
        execution.singleRun(() -> unit.fork(setter -> {
            System.out.println(new Pos(-(radius + centerX), unit.absoluteStart().y(), -(radius + centerZ)));
            System.out.println(new Pos(radius + centerX, height, radius + centerZ));
            GeneratorUtils.foreachXZ(
                    new Pos(-(radius + centerX), unit.absoluteStart().y(), -(radius + centerZ)),
                    new Pos(radius + centerX, height, radius + centerZ),
                    point -> {
                        double distance = point.distance(new Pos(centerX, point.y(), centerZ));

                        if(distance <= this.radius) {
                            setter.setBlock(point.add(1, height, 1), Block.GOLD_BLOCK);
                        }
                    }
            );
        }));

        super.generate(unit);
    }
}
