package com.hackathon.speakers.util;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;

public class SpeakUtil {
    public enum SpeakType {
        ST_FESTIVAL, ST_ESPEAK, ST_ESPEAK_FILE;
    }

    private static final Logger logger = LoggerFactory.getLogger(SpeakUtil.class);
    private static final String FESTIVAL = "echo \"%s\" | festival --tts";
    private static final String ESPEAK = "espeak -ven+f3 -k5 -s150 \"%s\"";
    private static final String ESPEAK_FILE = "espeak -ven+f3 -k5 -s150 -f %s";
    private static final int MAX_LENGTH = 150;

    public static void speak(String text, String userName) {
        speak(text, userName, SpeakType.ST_ESPEAK_FILE);
    }

    public static void speak(String text, String userName, SpeakType speakType) {
        switch (speakType) {
        case ST_FESTIVAL:
            festival(text, userName);
            break;
        case ST_ESPEAK:
            espeak(text, userName);
            break;
        case ST_ESPEAK_FILE:
            espeakFile(text, userName);
            break;
        }
    }

    private static void festival(String text, String userName) {
        try {
            String actualText = safeText(text);
            doSpeak(String.format(FESTIVAL, actualText), actualText, userName);
        } catch (Exception e) {
            logger.error("Failed to festival.", e);
        }
    }

    private static void espeak(String text, String userName) {
        try {
            String actualText = safeText(text);
            doSpeak(String.format(ESPEAK, actualText), actualText, userName);
        } catch (Exception e) {
            logger.error("Failed to espeak.", e);
        }
    }

    private static void espeakFile(String text, String userName) {
        try {
            String actualText = safeText(text);
            String filePath = FileUtil.writeToFile(actualText);

            if (filePath == null) {
                logger.warn("Unable to create temp file");
            }

            doSpeak(String.format(ESPEAK_FILE, filePath), actualText, userName);
        } catch (Exception e) {
            logger.error("Failed to espeak.", e);
        }
    }

    private static String safeText(String text) {
        if (Strings.isNullOrEmpty(text)) {
            return "";
        } else if (text.length() <= MAX_LENGTH) {
            return text;
        } else {
            return text.substring(0, MAX_LENGTH);
        }
    }

    private static void doSpeak(String command, String text, String userName) throws InterruptedException, IOException {
        logger.info("{} going to say {}.", userName, text);

        OSUtil.runCommand(command);
    }
}

@@ -1238,39 +1238,146 @@
 ctualText = 
-safeText(text);
+getTextToSay(text, userName);%0A%0A            if (Strings.isNullOrEmpty(actualText)) %7B%0A                return;%0A            %7D%0A
 %0A           
@@ -1651,39 +1651,146 @@
 ctualText = 
-safeText(text);
+getTextToSay(text, userName);%0A%0A            if (Strings.isNullOrEmpty(actualText)) %7B%0A                return;%0A            %7D%0A
 %0A           
@@ -2072,23 +2072,130 @@
 t = 
-safeText(text);
+getTextToSay(text, userName);%0A%0A            if (Strings.isNullOrEmpty(actualText)) %7B%0A                return;%0A            %7D%0A
 %0A   
@@ -2576,31 +2576,98 @@
 ing 
-safeText(String text) %7B
+getTextToSay(String text, String userName) %7B%0A        String safeTextStr = safeText(text);%0A
 %0A   
@@ -2697,20 +2697,27 @@
 OrEmpty(
-text
+safeTextStr
 )) %7B%0A   
@@ -2736,29 +2736,236 @@
 urn 
-%22%22;%0A        %7D else if
+null;%0A        %7D%0A%0A        String intro = userName.replaceAll(%22.%22, %22 %22) + %22 says %22;%0A        %0A        return intro + safeTextStr;%0A    %7D%0A%0A    private static String safeText(String text) %7B%0A        if ((Strings.isNullOrEmpty(text)) %7C%7C
  (te
@@ -2990,16 +2990,17 @@
 _LENGTH)
+)
  %7B%0A     

Add username to text