/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.velocitycore.discord.util;

import de.steamwar.messages.Chatter;
import de.steamwar.messages.Message;
import de.steamwar.velocitycore.discord.DiscordBot;
import de.steamwar.velocitycore.discord.channels.DiscordChannel;
import lombok.experimental.UtilityClass;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;

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

@UtilityClass
public class DiscordAlert {

    public static void send(Chatter player, Color color, Message title, Message description, boolean success) {
        DiscordBot.withBot(bot -> {
            Long discordId = player.user().getDiscordId();
            if (discordId == null) return;

            User user = DiscordBot.getInstance().getJda().retrieveUserById(discordId).complete();
            if (user == null) return;

            MessageCreateBuilder builder = new MessageCreateBuilder()
                    .setEmbeds(new EmbedBuilder()
                            .setAuthor("SteamWar", "https://steamwar.de", "https://cdn.discordapp.com/app-icons/869606970099904562/60c884000407c02671d91d8e7182b8a1.png")
                            .setColor(color)
                            .setTitle(player.parseToPlain(title))
                            .setDescription(player.parseToPlain(description))
                            .setTimestamp(Instant.now())
                            .build());
            if (success) {
                builder.setComponents(ActionRow.of(Button.success("tada", Emoji.fromUnicode("U+1F389"))));
            }

            new DiscordChannel(user).send(builder);
        });
    }
}
