package com.nincraft.ninbot.components.poll;

import lombok.Data;
import lombok.val;
import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.MessageBuilder;
import net.dv8tion.jda.core.entities.Message;
import org.apache.commons.lang3.StringUtils;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

@Data
class Poll {
    private String title;
    private List<String> choices;
    private String result;
    private long timeLength;

    Poll(Message message) {
        val pollMessage = message.getContentStripped().substring("@Ninbot poll ".length());
        choices = new ArrayList<>();
        if (pollMessage.contains("\"")) {
            this.title = pollMessage.substring(0, pollMessage.indexOf("\""));
            val pollOptions = pollMessage.substring(
                    pollMessage.indexOf("\"") + 1, pollMessage.lastIndexOf("\"")).replace("\"", "");
            this.choices = Arrays.asList(pollOptions.split(", "));
            val timeString = pollMessage.substring(pollMessage.lastIndexOf("\"") + 1).trim();
            if (StringUtils.isNotBlank(timeString)) {
                this.timeLength = Long.valueOf(timeString);
            } else {
                this.timeLength = 5L;
            }
        }
    }

    Message build() {
        return buildPollMessage("Poll will close in " + timeLength + " minutes at "
                + Instant.now().plus(timeLength, ChronoUnit.MINUTES));
    }

    Message buildClosed() {
        return buildPollMessage(result);
    }

    private Message buildPollMessage(String footer) {
        MessageBuilder messageBuilder = new MessageBuilder();
        EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.setTitle(title);
        embedBuilder.addField("Choices", buildPollChoices(), false);
        embedBuilder.setFooter(footer, null);
        messageBuilder.setEmbed(embedBuilder.build());
        return messageBuilder.build();
    }

    private String buildPollChoices() {
        StringBuilder stringBuilder = new StringBuilder();
        IntStream.range(0, choices.size()).forEachOrdered(i -> {
            String choice = choices.get(i);
            stringBuilder.append(i + 1).append(": ").append(choice).append("\n");
        });
        return stringBuilder.toString();
    }
}

@@ -399,43 +399,8 @@
 ist;
-%0Aimport java.util.stream.IntStream;
 %0A%0A@D
@@ -2126,64 +2126,40 @@
     
-IntStream.range(0, choices.size()).forEachOrdered(i -%3E %7B
+char digitalOneEmoji = '%5Cu0031';
 %0A   
@@ -2159,28 +2159,29 @@
 1';%0A        
-    
+for (
 String choic
@@ -2186,17 +2186,17 @@
 ice 
-=
+:
  choices
 .get
@@ -2195,16 +2195,11 @@
 ices
-.get(i);
+) %7B
 %0A   
@@ -2232,24 +2232,70 @@
 end(
-i + 1).append(%22:
+Character.toString(digitalOneEmoji)).append(%22%5Cu20E3%22).append(%22
  %22).
@@ -2335,11 +2335,40 @@
     
-%7D);
+    digitalOneEmoji++;%0A        %7D
 %0A   

Update poll choices to show the emojis