package de.blackcraze.grb.listener;

import java.util.stream.Collectors;

import de.blackcraze.grb.core.BotConfig;
import de.blackcraze.grb.core.Speaker;
import de.blackcraze.grb.i18n.Resource;
import de.blackcraze.grb.util.CommandUtils;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDA.Status;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.events.ReadyEvent;
import net.dv8tion.jda.core.events.ReconnectedEvent;
import net.dv8tion.jda.core.events.ResumedEvent;
import net.dv8tion.jda.core.events.StatusChangeEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;

public class ReadyListener extends ListenerAdapter {

	public ReadyListener() {
	}

	@Override
	public void onReady(ReadyEvent event) {
		System.out.println(event.getJDA().getGuilds().stream().map(Guild::getName)
				.collect(Collectors.joining(", ", "Listening on: ", "")));

		initialiseServers(event.getJDA());
	}

	@Override
	public void onResume(ResumedEvent event) {
		initialiseServers(event.getJDA());
	}

	@Override
	public void onReconnect(ReconnectedEvent event) {
		initialiseServers(event.getJDA());
	}

	private void initialiseServers(JDA jda) {
		for (Guild guild : jda.getGuilds()) {
			String listenChannel = BotConfig.getConfig(guild).CHANNEL;
			for (TextChannel channel : guild.getTextChannelsByName(listenChannel, true)) {
				initialise(channel);
			}
		}
	}

	private void initialise(TextChannel channel) {
		Speaker.say(channel, Resource.getString("INIT", CommandUtils.getResponseLocale(channel))
				+ "`" + BotConfig.getConfig(channel.getGuild()).PREFIX + "`");
	}

	/* Send a goodbye message when the shutting down event is triggered. */
	
	@Override
	public void onStatusChange(StatusChangeEvent event) {
		if (event.getStatus() == Status.SHUTTING_DOWN) {
			goodbyeServers(event.getJDA());	
		}
	}	
	
	private void goodbyeServers(JDA jda) {
		for (Guild guild : jda.getGuilds()) {
			String listenChannel = BotConfig.getConfig(guild).CHANNEL;
			for (TextChannel channel : guild.getTextChannelsByName(listenChannel, true)) {
				goodbyeMessage(channel);
			}
		}
	}
	
	private void goodbyeMessage(TextChannel channel) {
		Speaker.say(channel, Resource.getString("BYE_MSG", CommandUtils.getResponseLocale(channel))
				+ "`" + BotConfig.getConfig(channel.getGuild()).PREFIX + "`");
	}	
}

@@ -268,48 +268,8 @@
 DA;%0A
-import net.dv8tion.jda.core.JDA.Status;%0A
 impo
@@ -511,62 +511,8 @@
 nt;%0A
-import net.dv8tion.jda.core.events.StatusChangeEvent;%0A
 impo
@@ -1555,728 +1555,6 @@
 %09%7D%0A%0A
-%09/* Send a goodbye message when the shutting down event is triggered. */%0A%09%0A%09@Override%0A%09public void onStatusChange(StatusChangeEvent event) %7B%0A%09%09if (event.getStatus() == Status.SHUTTING_DOWN) %7B%0A%09%09%09goodbyeServers(event.getJDA());%09%0A%09%09%7D%0A%09%7D%09%0A%09%0A%09private void goodbyeServers(JDA jda) %7B%0A%09%09for (Guild guild : jda.getGuilds()) %7B%0A%09%09%09String listenChannel = BotConfig.getConfig(guild).CHANNEL;%0A%09%09%09for (TextChannel channel : guild.getTextChannelsByName(listenChannel, true)) %7B%0A%09%09%09%09goodbyeMessage(channel);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09%0A%09private void goodbyeMessage(TextChannel channel) %7B%0A%09%09Speaker.say(channel, Resource.getString(%22BYE_MSG%22, CommandUtils.getResponseLocale(channel))%0A%09%09%09%09+ %22%60%22 + BotConfig.getConfig(channel.getGuild()).PREFIX + %22%60%22);%0A%09%7D%09%0A
 %7D%0A

Revert "Goodbye-Messsage"