package eu.mhsl.minenet.minigames.util;

import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.instance.generator.GenerationUnit;

import java.util.function.Consumer;


public class GeneratorUtils {
    public static void foreachXZ(GenerationUnit unit, Consumer<Point> bottomPoint) {
        double startX = unit.absoluteStart().x();
        double endX = unit.absoluteEnd().x() - 1;
        double startZ = unit.absoluteStart().z();
        double endZ = unit.absoluteEnd().z() - 1;

        for (double x = startX; x <= endX; x++) {
            for (double z = startZ; z <= endZ; z++) {
                Point currentPoint = new Pos(x, unit.absoluteStart().y(), z);
                bottomPoint.accept(currentPoint);
            }
        }
    }

    public static void foreachXZ(Point start, Point end, Consumer<Point> callback) {
        double startX = Math.min(start.x(), end.x());
        double endX = Math.max(start.x(), end.x());
        double startZ = Math.min(start.z(), end.z());
        double endZ = Math.max(start.z(), end.z());

        for (double x = startX; x <= endX; x++) {
            for (double z = startZ; z <= endZ; z++) {
                Point currentPoint = new Pos(x, start.y(), z);
                callback.accept(currentPoint);
            }
        }
    }

    public static void iterateArea(Point pos1, Point pos2, Consumer<Point> callback) {
        int minX = (int) Math.min(pos1.x(), pos2.x());
        int maxX = (int) Math.max(pos1.x(), pos2.x());
        int minY = (int) Math.min(pos1.y(), pos2.y());
        int maxY = (int) Math.max(pos1.y(), pos2.y());
        int minZ = (int) Math.min(pos1.z(), pos2.z());
        int maxZ = (int) Math.max(pos1.z(), pos2.z());

        for (int x = minX; x <= maxX; x++) {
            for (int y = minY; y <= maxY; y++) {
                for (int z = minZ; z <= maxZ; z++) {
                    Point point = new Pos(x, y, z);
                    callback.accept(point);
                }
            }
        }
    }

    public static void outline(Point pos1, Point pos2, Consumer<Point> outline) {
        outline(pos1, pos2, outline, point -> {});
    }

    public static void outline(Point pos1, Point pos2, Consumer<Point> outline, Consumer<Point> innerFill) {
        int minX = (int) Math.min(pos1.x(), pos2.x());
        int minY = (int) Math.min(pos1.y(), pos2.y());
        int minZ = (int) Math.min(pos1.z(), pos2.z());

        int maxX = (int) Math.max(pos1.x(), pos2.x());
        int maxY = (int) Math.max(pos1.y(), pos2.y());
        int maxZ = (int) Math.max(pos1.z(), pos2.z());

        for (int x = minX; x <= maxX; x++) {
            for (int y = minY; y <= maxY; y++) {
                for (int z = minZ; z <= maxZ; z++) {
                    Point blockPos = new Pos(x, y, z);
                    if (x == minX || x == maxX || y == minY || y == maxY || z == minZ || z == maxZ) {
                        outline.accept(blockPos);
                    }
                }
            }
        }
    }
}
