package de.yannicpunktdee.yoshibot.listeners;

import de.yannicpunktdee.yoshibot.audio.AudioPlayerListener;
import de.yannicpunktdee.yoshibot.command.YoshiCommandContext;
import de.yannicpunktdee.yoshibot.main.YoshiBot;
import de.yannicpunktdee.yoshibot.utils.Resources;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

/**
 * Lauscht auf eingehende Nachrichten und leitet diese an die YoshiBot.executeCommand-Methode weiter, falls es sich um
 * ein Kommando handelt.
 *
 * @author Yannic Link
 */
public class DiscordEventListener extends ListenerAdapter {
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onMessageReceived(MessageReceivedEvent event) {
		if (!event.isFromType(ChannelType.TEXT)) return;
		
		if (event.getAuthor().isBot()) return;
		
		boolean inErlaubtemKanal = Arrays.stream(Resources.getRestrict_commands_to_channel())
				.anyMatch(channel -> channel.equalsIgnoreCase(event.getTextChannel().getName()));
		
		if (Resources.getRestrict_commands_to_channel() != null && !inErlaubtemKanal)
			return;
		
		String raw = event.getMessage().getContentRaw().trim();
		
		if (!raw.startsWith(YoshiCommandContext.PREFIX)) return;
		
		YoshiCommandContext context = new YoshiCommandContext(raw, event);
		if (!context.getState().equals(YoshiCommandContext.State.NO_COMMAND)) YoshiBot.executeCommand(context);
	}
	
	@Override
	public void onGuildVoiceJoin(@NotNull GuildVoiceJoinEvent event) {
		super.onGuildVoiceJoin(event);
		if (event.getMember().getUser().isBot()) return;
		
		if (Resources.isGreetings_and_byebyes_on()) {
			String nameToPlay = event.getMember().getNickname();
			if (nameToPlay == null) nameToPlay = event.getMember().getUser().getName();
			
			YoshiBot.getInstance().sayTTS(Resources.getRandomGreeting(nameToPlay), event.getChannelJoined());
		}
		if (!AudioPlayerListener.isPlayingTrack()) YoshiBot.getInstance().joinVoiceChannelWithMostMembers();
		
	}
	
	@Override
	public void onGuildVoiceMove(@NotNull GuildVoiceMoveEvent event) {
		super.onGuildVoiceMove(event);
		if (event.getMember().getUser().isBot()) return;
		
		VoiceChannel afkChannel = YoshiBot.getInstance().getGuild().getAfkChannel();
		String nameToPlay = event.getMember().getNickname();
		nameToPlay = nameToPlay == null ? event.getMember().getUser().getName() : nameToPlay;
		if (event.getChannelJoined() == afkChannel) {
			YoshiBot.getInstance().sayTTS(Resources.getRandomAfk(nameToPlay, false), event.getChannelLeft());
		} else if (event.getChannelLeft() == afkChannel) {
			YoshiBot.getInstance().sayTTS(Resources.getRandomAfk(nameToPlay, true), event.getChannelJoined());
		}
		
		if (!AudioPlayerListener.isPlayingTrack()) YoshiBot.getInstance().joinVoiceChannelWithMostMembers();
	}
	
	@Override
	public void onGuildVoiceLeave(@NotNull GuildVoiceLeaveEvent event) {
		super.onGuildVoiceLeave(event);
		if (event.getMember().getUser().isBot() || event.getChannelLeft().getMembers().size() == 0) return;
		
		if (Resources.isGreetings_and_byebyes_on()) {
			
			String nameToPlay = event.getMember().getNickname();
			nameToPlay = nameToPlay == null ? event.getMember().getUser().getName() : nameToPlay;
			
			YoshiBot.getInstance().sayTTS(Resources.getRandomByebye(nameToPlay), event.getChannelLeft());
		}
		if (!AudioPlayerListener.isPlayingTrack()) YoshiBot.getInstance().joinVoiceChannelWithMostMembers();
	}
	
}
