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

import de.steamwar.command.SWCommand;
import de.steamwar.sql.UserPerm;
import de.steamwar.velocitycore.VelocityCore;
import de.steamwar.velocitycore.discord.DiscordBot;
import de.steamwar.velocitycore.discord.channels.DiscordChannel;
import de.steamwar.velocitycore.discord.channels.InteractionReply;
import lombok.Getter;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.InteractionType;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

public class ChannelListener extends ListenerAdapter {
    @Getter
    private static final Map<MessageChannel, DiscordChannel> channels = new HashMap<>();

    @Override
    public void onMessageReceived(@NotNull MessageReceivedEvent event) {
        if(event.getAuthor().isBot())
            return;

        DiscordChannel channel = channels.get(event.getChannel());
        if(channel != null)
            channel.received(event);
    }

    @Override
    public void onGenericComponentInteractionCreate(@NotNull GenericComponentInteractionCreateEvent event) {
        if(event.getType() != InteractionType.COMPONENT)
            return;

        DiscordChannel channel = channels.get(event.getChannel());
        if(channel != null) {
            channel.received(event);
            return;
        }

        if(event.getChannelType() == ChannelType.PRIVATE && event.getComponentId().equals("tada"))
            event.reply(":tada:").queue();
    }

    @Override
    public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
        if (event.getGuild().getIdLong() == 1241489896909180998L) return;
        InteractionReply.reply(event, sender -> {
            if(sender.user().getDiscordId() == null)
                return;

            OptionMapping option = event.getOption(DiscordBot.ARGUMENT_NAME);
            String args = "";
            if(option != null)
                args = option.getAsString();

            VelocityCore.getLogger().log(Level.INFO, "%s -> executed Discord command /%s %s".formatted(sender.user().getUserName(), event.getName(), args));
            SWCommand command = DiscordBot.getCommands().get(event.getName());
            UserPerm permission = command.getPermission();
            if(permission != null && !sender.user().perms().contains(permission))
                return;

            command.execute(sender, args.isEmpty() ? new String[0] : args.split(" "));
        });
    }
}
