/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2024  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 de.steamwar.messages.Chatter;
import de.steamwar.messages.Message;
import de.steamwar.sql.SteamwarUser;
import de.steamwar.velocitycore.discord.DiscordBot;
import de.steamwar.velocitycore.discord.listeners.ChannelListener;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;


@AllArgsConstructor
public class DiscordChannel extends Chatter.PlayerlessChatter {

    public static SteamwarUser userOrPublic(User dcUser) {
        SteamwarUser user = SteamwarUser.get(dcUser.getIdLong());
        return user != null ? user : SteamwarUser.get(0);
    }

    private final SteamwarUser user;
    @Getter
    private final MessageChannel channel;

    public DiscordChannel(User user) {
        this(userOrPublic(user), user.openPrivateChannel().complete());
    }

    public DiscordChannel(String channel) {
        this(DiscordBot.getGuild().getTextChannelById(channel));
    }

    public DiscordChannel(MessageChannel channel) {
        this(SteamwarUser.get(-1), channel);
        ChannelListener.getChannels().put(this.channel, this);
    }

    public void send(String message) {
        send(new MessageCreateBuilder()
                .setContent(message
                        .replace("&", "")
                        .replace("@everyone", "`@everyone`")
                        .replace("@here", "`@here`")
                        .replaceAll("<[@#]!?\\d+>", "`$0`")));
    }

    public void send(MessageCreateBuilder builder) {
        channel.sendMessage(builder.build()).queue();
    }

    public void received(MessageReceivedEvent event) {
        event.getMessage().delete().queue();
    }

    public void received(GenericComponentInteractionCreateEvent event) {
        //ignored
    }

    @Override
    public SteamwarUser user() {
        return user;
    }

    @Override
    public boolean chatShown() {
        return true;
    }

    @Override
    public Component parse(boolean prefixed, Message message) {
        return super.parse(false, message);
    }

    @Override
    public void sendMessage(Component msg) {
        send(PlainTextComponentSerializer.plainText().serialize(msg));
    }
}
