package de.sjanusch.message_handler;

import java.util.Collection;
import java.util.Collections;

import javax.inject.Inject;

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

import de.benjaminborbe.bot.agent.MessageHandler;
import de.benjaminborbe.bot.agent.Request;
import de.benjaminborbe.bot.agent.Response;

public class LunchMessageHandler implements MessageHandler {

  private static final Logger logger = LoggerFactory.getLogger(LunchMessageHandler.class);

  @Inject
  public LunchMessageHandler() {
  }

  @Override
  public Collection<Response> HandleMessage(final Request request) {
    logger.debug("handle message");
    if (request.getMessage() != null && request.getMessage().startsWith("/lunch")) {
      logger.debug("got hello => send response");
      final Response response = new Response();
      response.setMessage("hello from lunch");
      return Collections.singletonList(response);
    }
    return Collections.emptyList();
  }
}

@@ -89,16 +89,39 @@
 ections;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -337,16 +337,116 @@
 esponse;
+%0Aimport de.sjanusch.confluence.rest.SuperlunchRestClient;%0Aimport de.sjanusch.model.superlunch.Lunch;
 %0A%0Apublic
@@ -599,48 +599,258 @@
 %0A%0A  
-@Inject%0A  public LunchMessageHandler() %7B
+public static final String PREFIX = %22/essen%22;%0A%0A  private final SuperlunchRestClient superlunchRestClient;%0A%0A  @Inject%0A  public LunchMessageHandler(final SuperlunchRestClient superlunchRestClient) %7B%0A    this.superlunchRestClient = superlunchRestClient;
 %0A  %7D
@@ -1001,19 +1001,20 @@
 e() 
-!
+=
 = null 
-&& 
+%7C%7C !
 requ
@@ -1045,16 +1045,14 @@
 ith(
-%22/lunch%22
+PREFIX
 )) %7B
@@ -1076,40 +1076,85 @@
 ug(%22
-got hello =%3E send response%22
+message != %7B%7D =%3E skip%22, PREFIX);%0A      return Collections.emptyList(
 );%0A  
+  %7D%0A
     
@@ -1163,93 +1163,517 @@
 nal 
-Response response
+List%3CLunch%3E lunches = superlunchRestClient.superlunchRestApiGet();%0A    if (lunches == null) %7B%0A      logger.debug(%22lunches
  =
+=
  n
-ew Response();%0A      response.setMessage(%22hello from lunch%22
+ull%22);%0A      return getResponses(%22get lunches failed%22);%0A    %7D%0A    logger.debug(%22got %7B%7D lunches%22, lunches.size());%0A    StringBuffer sb = new StringBuffer();%0A    boolean first = true;%0A    for (Lunch lunch : lunches) %7B%0A      if (first) %7B%0A        first = false;%0A      %7D else %7B%0A        sb.append(%22%5Cn%22);%0A      %7D%0A      sb.append(lunch.getDate()).append(%22 %22).append(lunch.getTitle()
 );%0A  
+  %7D%0A
     
@@ -1683,50 +1683,189 @@
 urn 
-Collections.singletonList(response);%0A    %7D
+getResponses(sb.toString());%0A  %7D%0A%0A  private Collection%3CResponse%3E getResponses(final String message) %7B%0A    final Response response = new Response();%0A    response.setMessage(message);
 %0A   
@@ -1880,31 +1880,43 @@
 Collections.
-emptyList(
+singletonList(response
 );%0A  %7D%0A%7D%0A

return lunch list