package com.alttd.webinterface.appeals;

import com.alttd.webinterface.objects.MessageForEmbed;
import com.alttd.webinterface.send_message.DiscordSender;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.components.actionrow.ActionRow;
import net.dv8tion.jda.api.components.buttons.Button;
import net.dv8tion.jda.api.entities.Message;

import java.util.List;
import java.util.Optional;

@Slf4j
public class AppealSender {

    private static final AppealSender INSTANCE = new AppealSender();

    public static AppealSender getInstance() {
        return INSTANCE;
    }

    public void sendAppeal(List<Long> channelIds, MessageForEmbed messageForEmbed, long assignedTo) {
        DiscordSender.getInstance()
                .sendEmbedToChannels(channelIds, messageForEmbed)
                .whenCompleteAsync((result, error) -> {
                    if (error != null) {
                        log.error("Failed sending embed to channels", error);
                        return;
                    }
                    List<Message> list = result.stream()
                            .filter(Optional::isPresent)
                            .map(Optional::get)
                            .toList();


                    list.forEach(message -> {
                        message.createThreadChannel("Appeal")
                                .queue(channel -> {
                                    if (assignedTo == 0L) {
                                        return;
                                    }
                                    String assignedUserMessage = "<@" + assignedTo + "> you have a new appeal!";
                                    channel.sendMessage(assignedUserMessage).queue();
                                });
                    });

                    addButtons(list);
                });
    }

    public void addButtons(List<Message> messages) {
        Button reminderAccepted = Button.primary("reminder_accepted", "Accepted");
        Button reminderInProgress = Button.secondary("reminder_in_progress", "In Progress");
        Button reminderDenied = Button.danger("reminder_denied", "Denied");
        messages.forEach(message -> {
            message.editMessageComponents(ActionRow.of(reminderAccepted, reminderInProgress, reminderDenied)).queue();
        });
    }

}
