package kr.co.swh.lecture.opensource.project.discode;

import javax.security.auth.login.LoginException;

import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.javacord.api.entity.emoji.CustomEmojiBuilder;
import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.message.MessageBuilder;
import org.javacord.api.entity.message.component.ActionRow;
import org.javacord.api.entity.message.component.Button;
import org.javacord.api.entity.message.component.ButtonStyle;
import org.javacord.api.interaction.MessageComponentInteraction;


public class JavaCordBot {
	
	public static void main(String[] args) throws LoginException, InterruptedException {
		// TODO Auto-generated method stub
		String token = "NjYzMjgzODYxMTUyNTk1OTkz.XhGROw.Ps3JJIKlEXoE1eah_5OKHwViThY";
        DiscordApi api = new DiscordApiBuilder().setToken(token).login().join();

        // Add a listener which answers with "Pong!" if someone writes "!ping"
        api.addMessageCreateListener(event -> {
            if (event.getMessageContent().equalsIgnoreCase("!ping")) {
                new MessageBuilder()
                .setContent("Click on one of these Buttons!")
                .addComponents(
                    ActionRow.of(Button.success("success", "Send a message"),
                        Button.danger("danger", "Delete this message"),
                        Button.link("http://naver.com", "", "1️⃣"),
                        Button.secondary("secondary", "Remind me after 5 minutes")))
                .send(event.getChannel());
            }
        });
        
        api.addMessageComponentCreateListener(event -> {
            MessageComponentInteraction messageComponentInteraction = event.getMessageComponentInteraction();
            String customId = messageComponentInteraction.getCustomId();

            switch (customId) {
                case "success":
                    messageComponentInteraction.createImmediateResponder()
                            .setContent("You clicked a button!")
                            .respond();
                    break;
                case "danger":
                    messageComponentInteraction.getMessage().ifPresent(Message::delete);
                    break;
                case "secondary":
                    messageComponentInteraction.respondLater().thenAccept(interactionOriginalResponseUpdater -> {
                        //Code to respond after 5 minutes
                    });
                    break;
            }
        });

        // Print the invite url of your bot
        System.out.println("You can invite the bot by using the following url: " + api.createBotInvite());
	}
}
