package com.nincraft.ninbot.components.poll;

import com.nincraft.ninbot.components.common.MessageAction;
import lombok.val;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageReaction;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

class PollAnnounce extends TimerTask {

    private Poll poll;
    private Message pollMessage;

    PollAnnounce(Poll poll, Message pollMessage) {
        this.poll = poll;
        this.pollMessage = pollMessage;
    }

    @Override
    public void run() {
        announcePollResults().executeActions();
    }

    private MessageAction announcePollResults() {
        MessageAction messageAction = new MessageAction();
        val newMessage = pollMessage.getChannel().retrieveMessageById(pollMessage.getId()).complete();
        int highCount = 0;
        List<String> winningChoices = new ArrayList<>();
        List<MessageReaction> reactions = newMessage.getReactions();
        for (int i = 0; i < reactions.size() && i < poll.getChoices().size(); i++) {
            MessageReaction reaction = reactions.get(i);
            if (reaction.getCount() == highCount) {
                winningChoices.add(poll.getChoices().get(i));
            }
            if (reaction.getCount() > highCount) {
                highCount = reaction.getCount();
                winningChoices.clear();
                winningChoices.add(poll.getChoices().get(i));
            }
        }
        val resourceBundle = poll.getResourceBundle();

        String pollClosedMessage = resourceBundle.getString("poll.announce.closed");
        if (highCount <= 1) {
            poll.setResult(resourceBundle.getString("poll.announce.noonevoted") + pollClosedMessage);
        } else if (winningChoices.size() == 1) {
            poll.setResult(
                    "\"" + winningChoices.get(0) + "\"" + resourceBundle.getString("poll.announce.hadmostvotes") + (
                            highCount - 1) + resourceBundle.getString("poll.announce.votes")
                            + pollClosedMessage);
        } else if (winningChoices.size() > 1) {
            poll.setResult(
                    String.format(resourceBundle.getString("poll.announce.tie"), listWinners(winningChoices), (highCount
                            - 1))
                            + pollClosedMessage);
        }
        pollMessage.editMessage(poll.buildClosed()).queue();
        messageAction.addChannelAction(poll.getResult());
        pollMessage.unpin().queue();
        return messageAction;
    }

    private String listWinners(List<String> winningChoices) {
        StringBuilder winners = new StringBuilder();
        for (int i = 0; i < winningChoices.size(); i++) {
            if (i == winningChoices.size() - 1) {
                winners.append("and ");
            }
            winners.append("\"").append(winningChoices.get(i)).append("\"");
            if (i != winningChoices.size() - 1) {
                winners.append(", ");
            }
        }
        return winners.toString();
    }
}

@@ -43,68 +43,8 @@
 l;%0A%0A
-import com.nincraft.ninbot.components.common.MessageAction;%0A
 impo
@@ -504,16 +504,53 @@
         
+pollMessage.getChannel().sendMessage(
 announce
@@ -566,23 +566,15 @@
 ts()
-.executeActions
+).queue
 ();%0A
@@ -592,29 +592,22 @@
 private 
-MessageAction
+String
  announc
@@ -627,67 +627,8 @@
 ) %7B%0A
-        MessageAction messageAction = new MessageAction();%0A
     
@@ -2341,66 +2341,8 @@
 ();%0A
-        messageAction.addChannelAction(poll.getResult());%0A
     
@@ -2393,21 +2393,24 @@
 urn 
-messageAction
+poll.getResult()
 ;%0A  

Fix poll announcements