package com.zivilon.cinder_loe.command;

import com.zivilon.cinder_loe.carriage.CarriageDestination;
import com.zivilon.cinder_loe.carriage.CarriageDestinationsData;
import com.zivilon.cinder_loe.carriage.CarriageRoadRegistry;
import com.zivilon.cinder_loe.network.PacketOpenCarriageGui;
import lotr.common.network.LOTRPacketHandler;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.world.World;

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

public class CommandOpenCarriageMenu extends CommandBase {
    @Override
    public String getCommandName() {
        return "open_carriage_menu";
    }

    @Override
    public String getCommandUsage(ICommandSender sender) {
        return "/open_carriage_menu [roadId]";
    }

    @Override
    public void processCommand(ICommandSender sender, String[] args) throws CommandException {
        if (!(sender instanceof EntityPlayerMP)) {
            throw new CommandException("Must be run by a player.");
        }
        EntityPlayerMP player = (EntityPlayerMP) sender;
        World world = player.worldObj;

        CarriageDestinationsData data = CarriageDestinationsData.get(world);
        if (data == null) {
            throw new CommandException("Carriage data unavailable.");
        }

        // decide startRoad:
        String startRoad;
        if (args.length > 0) {
            // user explicitly specified a road
            startRoad = args[0];
            if (CarriageRoadRegistry.getRoad(startRoad) == null) {
                throw new CommandException("Unknown road ID: " + startRoad);
            }
        } else {
            // no road given, pick the very nearest destination and use its road
            List<CarriageDestination> rawNearby =
                    data.getNearestFour(player.posX, player.posZ, "", CarriageRoadRegistry.INSTANCE);
            if (rawNearby.isEmpty()) {
                throw new CommandException("No nearby carriage stops found.");
            }
            startRoad = rawNearby.get(0).road;
        }

        // now grab up to 4 stops on that road + its neighbors
        List<CarriageDestination> nearest =
                data.getNearestFour(player.posX, player.posZ, startRoad, CarriageRoadRegistry.INSTANCE);
        if (nearest.isEmpty()) {
            throw new CommandException(
                    "No stops found on road '" + startRoad + "' or its neighbors.");
        }

        // build the packet
        List<String> names  = new ArrayList<>();
        List<Integer> costs = new ArrayList<>();
        List<String> roads  = new ArrayList<>();
        for (CarriageDestination d : nearest) {
            double dx = d.x - player.posX;
            double dz = d.z - player.posZ;
            int cost = Math.max(1, (int)(Math.hypot(dx, dz) / 50));
            names .add(d.name);
            costs .add(cost);
            roads .add(d.road);
        }

        LOTRPacketHandler.networkWrapper
                .sendTo(new PacketOpenCarriageGui(names, costs, roads), player);
    }
}

