/*
 *   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.channels;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
import net.dv8tion.jda.api.utils.messages.MessageEditData;

import java.util.function.Consumer;
import java.util.function.Supplier;

public class StaticMessageChannel extends DiscordChannel {

    public static MessageCreateBuilder toMessageBuilder(EmbedBuilder embedBuilder) {
        MessageCreateBuilder messageBuilder = new MessageCreateBuilder();
        messageBuilder.setEmbeds(embedBuilder.build());
        return messageBuilder;
    }

    private Message message;
    private final Supplier<MessageCreateBuilder> supplier;
    private final Consumer<GenericComponentInteractionCreateEvent> interaction;

    public StaticMessageChannel(String channel, Supplier<MessageCreateBuilder> supplier) {
        this(channel, supplier, event -> {});
    }

    public StaticMessageChannel(String channel, Supplier<MessageCreateBuilder> supplier, Consumer<GenericComponentInteractionCreateEvent> interaction) {
        super(channel, 0);
        this.supplier = supplier;
        this.interaction = interaction;
        init();
    }

    public StaticMessageChannel(MessageChannel channel, Supplier<MessageCreateBuilder> supplier, Consumer<GenericComponentInteractionCreateEvent> interaction) {
        super(channel, 0);
        this.supplier = supplier;
        this.interaction = interaction;
        init();
    }

    private void init() {
        if(getChannel().getLatestMessageIdLong() != 0)
            message = getChannel().getIterableHistory().complete().stream().filter(m -> m.getAuthor().isBot()).findFirst().orElse(null);

        update();
    }

    public void update() {
        if (message == null) {
            getChannel().sendMessage(supplier.get().build()).queue(m -> message = m);
        } else {
            message.editMessage(MessageEditData.fromCreateData(supplier.get().build())).queue();
        }
    }

    @Override
    public void received(GenericComponentInteractionCreateEvent event) {
        interaction.accept(event);
    }
}
