package fr.Skydust.JdrBot.menu;

import fr.Skydust.JdrBot.utils.UnicodeEmotes;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.api.utils.messages.MessageEditData;

import java.util.HashMap;

public class MenuSystem {
    private static final HashMap<Message, Menu> menuHash = new HashMap<>();

    /** Action Listening */
    public static void onMessageReactionAdd(MessageReactionAddEvent e) {
        if (e.getChannel() instanceof TextChannel) {
            if(e.getUser() != null && e.getUser().isBot())
                return;

            e.getChannel().retrieveMessageById(e.getMessageId()).queue(msg -> {

                // Check if the message has a menu associated
                Menu menu = menuHash.get(msg);
                if (menu != null) {

                    e.getReaction().removeReaction(e.getUser()).queue();

                    // Check if the emote is valid and get id
                    int emoteId = 0;
                    for(String emote : UnicodeEmotes.CharsUnicodeArray){
                        String cleanedUpEmote = UnicodeEmotes.cleanUpDiscordEmote(
                                e.getReaction().getEmoji().getAsReactionCode());
                        if(emote.equals(cleanedUpEmote)) {
                            break;
                        }
                        emoteId++;
                    }
                    // Do not proceed if not valid
                    if(emoteId > UnicodeEmotes.CharsUnicodeArray.length) {
                        return;
                    }

                    menu.onButtonClicked(e.getMember(), msg, emoteId);
                }
            });
        }
    }

    /** Reload the message with its menu */
    public static void reloadMenu(Message currentMessage) {
        Menu newMenu = menuHash.get(currentMessage);
        if(newMenu == null) {
            System.out.println("Error: No menu on this message");
            return;
        }
        currentMessage.editMessage(MessageEditData.fromCreateData(newMenu.makeMessage())).queue();
        updateReactions(currentMessage, newMenu);
    }

    public static void updateReactions(Message msg, Menu menu) {
        // Removes excess reactions
        for (int y = menu.items.length; y < msg.getReactions().size(); y++) {
            msg.getReactions().get(y).removeReaction().queue();
        }
        // Adds missing reactions
        for (int i = msg.getReactions().size(); i < menu.items.length; i++) {
            msg.addReaction(Emoji.fromUnicode(UnicodeEmotes.CharsUnicodeArray[i])).queue();
        }
    }

    /** Edit the message to be the new menu */
    public static void changeMenu(Message currentMessage, Menu newMenu) {
        // If null, unbind menu
        if(newMenu == null) {
            menuHash.put(currentMessage, null);
            return;
        }

        currentMessage.editMessage(MessageEditData.fromCreateData(newMenu.makeMessage())).queue();
        updateReactions(currentMessage, newMenu);
        menuHash.put(currentMessage, newMenu);
    }
}
