package com.alttd.webinterface.objects;

import com.alttd.webinterface.send_message.DiscordSender;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;

import java.awt.*;
import java.time.Instant;
import java.util.List;

public record MessageForEmbed(String title, String description, List<DiscordSender.EmbedField> fields, Integer colorRgb,
                              Instant timestamp, String footer) {

    public MessageEmbed toEmbed() {
        EmbedBuilder eb = new EmbedBuilder();
        if (title != null && !title.isBlank()) {
            eb.setTitle(title);
        }
        if (description != null && !description.isBlank()) {
            eb.setDescription(description);
        }
        if (colorRgb != null) {
            eb.setColor(new Color(colorRgb));
        } else {
            eb.setColor(new Color(0xFF8C00)); // default orange
        }
        eb.setTimestamp(timestamp != null ? timestamp : Instant.now());
        if (footer != null && !footer.isBlank()) {
            eb.setFooter(footer);
        }

        if (fields != null) {
            for (DiscordSender.EmbedField f : fields) {
                if (f == null) {
                    continue;
                }
                String name = f.getName() == null ? "" : f.getName();
                String value = f.getValue() == null ? "" : f.getValue();
                // JDA field value max is 1024; truncate to be safe
                if (value.length() > 1024) {
                    value = value.substring(0, 1021) + "...";
                }
                eb.addField(new MessageEmbed.Field(name, value, f.isInline()));
            }
        }

        return eb.build();
    }
}
