package de.sjanusch;

import javax.inject.Inject;

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

import de.benjaminborbe.bot.agent.Address;
import de.benjaminborbe.bot.agent.Runner;

public class Webhook implements Runnable {

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

  private final Runner instance;

  private final String botname = "lunch";

  @Inject
  public Webhook(final Runner instance) {
    this.instance = instance;
  }

  @Override
  public void run() {
    try {
      logger.debug("starting webhook");
      final Address nsqdAddress = Address.fromEnv("NSQD_ADDRESS");
      final Address nsqLookupdAddress = Address.fromEnv("NSQ_LOOKUPD_ADDRESS");
      instance.run(nsqdAddress, nsqLookupdAddress, botname);
      logger.debug("webhook started");
    } catch (final Exception e) {
      logger.warn("start webhook failed", e);
    }
  }
}

@@ -232,18 +232,17 @@
 able %7B%0A%0A
-  
+%09
 private 
@@ -311,26 +311,25 @@
 lass);%0A%0A
-  
+%09
 private 
 final Ru
@@ -324,42 +324,122 @@
 ate 
-final Runner instance;%0A%0A  
+static final String BOT_NAME = %22BOT_NAME%22;%0A%0A%09private static final String NSQD_ADDRESS = %22NSQD_ADDRESS%22;%0A%0A%09
 private 
 fina
@@ -434,16 +434,23 @@
 private 
+static 
 final St
@@ -458,38 +458,96 @@
 ing 
-botname = %22lunch%22;%0A%0A  
+NSQ_LOOKUPD_ADDRESS = %22NSQ_LOOKUPD_ADDRESS%22;%0A%0A%09private final Runner instance;%0A%0A%09
 @Inject%0A
   pu
@@ -542,18 +542,17 @@
 @Inject%0A
-  
+%09
 public W
@@ -583,20 +583,18 @@
 ance) %7B%0A
-    
+%09%09
 this.ins
@@ -615,15 +615,13 @@
 ce;%0A
-  %7D%0A%0A  
+%09%7D%0A%0A%09
 @Ove
@@ -626,18 +626,17 @@
 verride%0A
-  
+%09
 public v
@@ -651,24 +651,19 @@
 ) %7B%0A
-    
+%09%09
 try %7B%0A
-      
+%09%09%09
 logg
@@ -688,30 +688,27 @@
  webhook%22);%0A
-      
+%09%09%09
 final Addres
@@ -739,17 +739,16 @@
 fromEnv(
-%22
 NSQD_ADD
@@ -751,26 +751,22 @@
 _ADDRESS
-%22
 );%0A
-      
+%09%09%09
 final Ad
@@ -807,17 +807,16 @@
 fromEnv(
-%22
 NSQ_LOOK
@@ -830,18 +830,220 @@
 RESS
-%22
 );%0A
-      
+%09%09%09final String bot_name = System.getenv(BOT_NAME);%0A%09%09%09if (bot_name == null %7C%7C bot_name.isEmpty()) %7B%0A%09%09%09%09logger.warn(%22env %7B%7D missing%22, BOT_NAME);%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%09logger.debug(%22bot_name: %7B%7D%22, bot_name);%0A%09%09%09
 inst
@@ -1090,21 +1090,19 @@
  bot
+_
 name);%0A
-      
+%09%09%09
 logg
@@ -1130,20 +1130,18 @@
 rted%22);%0A
-    
+%09%09
 %7D catch 
@@ -1166,14 +1166,11 @@
 ) %7B%0A
-      
+%09%09%09
 logg
@@ -1209,16 +1209,13 @@
 e);%0A
-    %7D%0A  
+%09%09%7D%0A%09
 %7D%0A%7D%0A

add bot_name param