// SPDX-FileCopyrightText: 2017 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0

package dan200.computercraft.shared.command.text;

import dan200.computercraft.shared.computer.core.ServerComputer;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.MutableComponent;
import org.jspecify.annotations.Nullable;

/**
 * Various helpers for building chat messages.
 */
public final class ChatHelpers {
    private static final ChatFormatting HEADER = ChatFormatting.LIGHT_PURPLE;

    private ChatHelpers() {
    }

    public static MutableComponent coloured(@Nullable String text, ChatFormatting colour) {
        return text(text).withStyle(colour);
    }

    public static MutableComponent text(@Nullable String text) {
        return Component.literal(text == null ? "" : text);
    }

    public static MutableComponent list(Component... children) {
        var component = Component.empty();
        for (var child : children) {
            component.append(child);
        }
        return component;
    }

    public static MutableComponent position(BlockPos pos) {
        return Component.literal(pos.toShortString());
    }

    public static MutableComponent bool(boolean value) {
        return value
            ? Component.translatable("commands.computercraft.generic.yes").withStyle(ChatFormatting.GREEN)
            : Component.translatable("commands.computercraft.generic.no").withStyle(ChatFormatting.RED);
    }

    public static Component link(MutableComponent component, String command, Component toolTip) {
        return link(component, new ClickEvent.RunCommand(command), toolTip);
    }

    public static Component link(Component component, ClickEvent click, Component toolTip) {
        var style = component.getStyle();

        if (style.getColor() == null) style = style.withColor(ChatFormatting.YELLOW);
        style = style.withClickEvent(click);
        style = style.withHoverEvent(new HoverEvent.ShowText(toolTip));

        return component.copy().withStyle(style);
    }

    public static MutableComponent header(String text) {
        return coloured(text, HEADER);
    }

    public static MutableComponent copy(String text) {
        return Component.literal(text).withStyle(s -> s
            .withClickEvent(new ClickEvent.CopyToClipboard(text))
            .withHoverEvent(new HoverEvent.ShowText(Component.translatable("gui.computercraft.tooltip.copy")))
        );
    }

    public static String makeComputerCommand(String command, ServerComputer computer) {
        return String.format("/computercraft %s @c[instance=%s]", command, computer.getInstanceUUID());
    }

    public static Component makeComputerDumpCommand(ServerComputer computer) {
        return link(
            text("#" + computer.getID()),
            makeComputerCommand("dump", computer),
            Component.translatable("commands.computercraft.dump.action")
        );
    }
}
