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 final boolean generated = false;
    private BlockPallet blockPallet = BlockPallet.GROUND;
    private int centerX = 0;
    private int centerZ = 0;

    public CircularPlateGenerator(int radius, int height) {
        this.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) {
        this.execution.singleRun(() -> unit.fork(setter -> {
            System.out.println(new Pos(-(this.radius + this.centerX), unit.absoluteStart().y(), -(this.radius + this.centerZ)));
            System.out.println(new Pos(this.radius + this.centerX, this.height, this.radius + this.centerZ));
            GeneratorUtils.foreachXZ(
                new Pos(-(this.radius + this.centerX), unit.absoluteStart().y(), -(this.radius + this.centerZ)),
                new Pos(this.radius + this.centerX, this.height, this.radius + this.centerZ),
                point -> {
                    double distance = point.distance(new Pos(this.centerX, point.y(), this.centerZ));

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

        super.generate(unit);
    }
}
