package com.nincraft.ninbot.components.reaction;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@Component
public class ReactionsReader {

    private static List<String> badCharacters = Arrays.asList(" ", "!", ":");

    @Bean
    List<ReactionResponse> reactionResponseList() {
        try {
            String jsonString = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader()
                    .getResource("responses.json")
                    .toURI())));
            ObjectMapper objectMapper = new ObjectMapper();
            List<ReactionResponse> reactionResponseList = objectMapper.readValue(objectMapper.readTree(jsonString)
                    .get("responses")
                    .toString(), objectMapper.getTypeFactory()
                    .constructCollectionType(List.class, ReactionResponse.class));
            return reactionResponseList.stream()
                    .map(this::generateResponse)
                    .sorted(Comparator.comparing(ReactionResponse::getType).reversed())
                    .collect(Collectors.toList());
        } catch (IOException | URISyntaxException e) {
            log.error("Failed to load reaction responses JSON", e);
        }
        return new ArrayList<>();
    }

    private ReactionResponse generateResponse(ReactionResponse response) {
        if (isCanEmoji(response.getResponse())) {
            return new EmojiReactionResponse(response);
        } else {
            return new StringReactionResponse(response);
        }
    }

    private boolean isCanEmoji(String response) {
        if (containsBadCharacters(response)) {
            return false;
        }
        for (char c : response.toCharArray()) {
            String check = StringUtils.replaceOnce(response, Character.toString(c), "");
            if (check.contains(Character.toString(c))) {
                return false;
            }
        }
        return true;
    }

    private boolean containsBadCharacters(String response) {
        return badCharacters.stream().anyMatch(response::contains);
    }
}

@@ -292,27 +292,30 @@
 java.io.
-IOException
+BufferedReader
 ;%0Aimport
@@ -324,21 +324,13 @@
 ava.
-net.URISyntax
+io.IO
 Exce
@@ -352,22 +352,22 @@
 ava.
-n
 io.
-file.Files
+InputStream
 ;%0Aim
@@ -380,22 +380,28 @@
 ava.
-n
 io.
-file.Paths
+InputStreamReader
 ;%0Aim
@@ -746,16 +746,17 @@
   try %7B%0A
+%0A
         
@@ -783,47 +783,27 @@
 g = 
-new String(Files.readAllBytes(Paths.get
+readFromInputStream
 (get
@@ -859,16 +859,24 @@
 Resource
+AsStream
 (%22respon
@@ -889,39 +889,8 @@
 on%22)
-%0A                    .toURI()))
 );%0A 
@@ -1517,29 +1517,8 @@
 ion 
-%7C URISyntaxException 
 e) %7B
@@ -1630,23 +1630,484 @@
 st%3C%3E();%0A
-
     %7D%0A%0A
+    private String readFromInputStream(InputStream inputStream) throws IOException %7B%0A        StringBuilder resultStringBuilder = new StringBuilder();%0A        try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) %7B%0A            String line;%0A            while ((line = br.readLine()) != null) %7B%0A                resultStringBuilder.append(line).append(%22%5Cn%22);%0A            %7D%0A        %7D%0A        return resultStringBuilder.toString();%0A    %7D%0A%0A%0A
     priv

Fix file reading