package com.livingworld.commands;

import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import net.minecraft.world.phys.Vec3;

import com.livingworld.regions.Region;
import com.livingworld.regions.RegionManager;

/**
 * Prints the Living World region state at the command source position.
 */
public final class RegionInfoCommand {

    private RegionInfoCommand() {
    }

    public static int execute(
            CommandSourceStack source,
            RegionManager regionManager) {
        if (source == null) {
            throw new IllegalArgumentException("source must not be null");
        }
        if (regionManager == null) {
            throw new IllegalArgumentException("regionManager must not be null");
        }

        Vec3 position = source.getPosition();
        String dimensionId = source.getLevel().dimension().location().toString();
        Region region = regionManager.getOrCreateRegionAtBlock(
                dimensionId,
                floorToBlock(position.x),
                floorToBlock(position.z));

        for (String line : RegionInfoFormatter.format(region)) {
            source.sendSuccess(() -> Component.literal(line), false);
        }
        return 1;
    }

    private static int floorToBlock(double coordinate) {
        return (int) Math.floor(coordinate);
    }
}
