package restAPI.controllers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpStatus;
import org.bots4j.msbotframework.beans.ChannelAccount;
import org.bots4j.msbotframework.beans.Message;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import utils.msbotframework.HelloSuribotConnectorClient;

@RestController
public class ReceivingMessagesController {
	
	// TODO To change every hour : make a daemon to update the token, and put it in the cache
	private static final String token = "";
	
	private static final String appid = System.getenv("APPID");
	private static final String appsecret = System.getenv("APPSECRET");
	
	@RequestMapping(value ="/")
	public int receivingMessage(HttpServletRequest request){
	    
		JSONObject json = null;
	    try {
			Reader body = request.getReader();
			BufferedReader reader = new BufferedReader(body);
			StringBuilder sb = new StringBuilder();
		    int cp;
		    while ((cp = reader.read()) != -1) {
		      sb.append((char) cp);
		    }
		    json = new JSONObject(sb.toString());
		    printUserMessage(json);
		    
		    System.out.println("\n====sendResponse=====\n");
		    HelloSuribotConnectorClient client = new HelloSuribotConnectorClient(appid, appsecret, token);
		    Message message = new Message()
		            .withFrom(from())
		            .withTo(to())
		            .withText("This is a test message from Hello Suribot")
		            .withLanguage("en");

		    printEntireJSONMessage(json);
		    Message reply = client.Messages.sendMessage(message);
		    System.out.println("Response : "+reply.getText());
		    
	    } catch (JSONException e){
	    	System.out.println("No user message but a request has been received.");
	    	printEntireJSONMessage(json);
	    } catch (RuntimeException | IOException e){
	    	e.printStackTrace();
	    	return HttpStatus.SC_UNAUTHORIZED;
	    }
			
	    return HttpStatus.SC_OK;
	}
	
	private void printEntireJSONMessage(JSONObject json){
		System.out.println("Entire JSON Body : \n"+ json.toString(3) );
	}
	
	private void printUserMessage(JSONObject json) throws JSONException {
		System.out.println("User message : "+json.getString("text"));
	}
	
	public static ChannelAccount to(){
        return new ChannelAccount()
                .withName("julien.margarido")
                .withId("U2LJMA17F")
//                .withAddress("julien.margarido")
//                .withChannelId("slack")
                .withIsBot(false);
    }

    public static ChannelAccount from(){
        return new ChannelAccount()
                .withName("lab_bot")
                .withId("B2S5SJQ3Y")
//                .withAddress("lab-bot")
//                .withChannelId("slack")
                .withIsBot(true);
    }
} 

@@ -201,113 +201,8 @@
 s;%0D%0A
-import org.bots4j.msbotframework.beans.ChannelAccount;%0D%0Aimport org.bots4j.msbotframework.beans.Message;%0D%0A
 impo
@@ -225,24 +225,24 @@
 Exception;%0D%0A
+
 import org.j
@@ -392,68 +392,8 @@
 %0D%0A%0D%0A
-import utils.msbotframework.HelloSuribotConnectorClient;%0D%0A%0D%0A
 @Res
@@ -456,280 +456,8 @@
 %0A%09%0D%0A
-%09// TODO To change every hour : make a daemon to update the token, and put it in the cache%0D%0A%09private static final String token = %22%22;%0D%0A%09%0D%0A%09private static final String appid = System.getenv(%22APPID%22);%0D%0A%09private static final String appsecret = System.getenv(%22APPSECRET%22);%0D%0A%09%0D%0A
 %09@Re
@@ -911,539 +911,8 @@
   %0D%0A
-%09%09    System.out.println(%22%5Cn====sendResponse=====%5Cn%22);%0D%0A%09%09    HelloSuribotConnectorClient client = new HelloSuribotConnectorClient(appid, appsecret, token);%0D%0A%09%09    Message message = new Message()%0D%0A%09%09            .withFrom(from())%0D%0A%09%09            .withTo(to())%0D%0A%09%09            .withText(%22This is a test message from Hello Suribot%22)%0D%0A%09%09            .withLanguage(%22en%22);%0D%0A%0D%0A%09%09    printEntireJSONMessage(json);%0D%0A%09%09    Message reply = client.Messages.sendMessage(message);%0D%0A%09%09    System.out.println(%22Response : %22+reply.getText());%0D%0A%09%09    %0D%0A
 %09   
@@ -1502,593 +1502,11 @@
 %09%7D%0D%0A
+
 %09%0D%0A
-%09public static ChannelAccount to()%7B%0D%0A        return new ChannelAccount()%0D%0A                .withName(%22julien.margarido%22)%0D%0A                .withId(%22U2LJMA17F%22)%0D%0A//                .withAddress(%22julien.margarido%22)%0D%0A//                .withChannelId(%22slack%22)%0D%0A                .withIsBot(false);%0D%0A    %7D%0D%0A%0D%0A    public static ChannelAccount from()%7B%0D%0A        return new ChannelAccount()%0D%0A                .withName(%22lab_bot%22)%0D%0A                .withId(%22B2S5SJQ3Y%22)%0D%0A//                .withAddress(%22lab-bot%22)%0D%0A//                .withChannelId(%22slack%22)%0D%0A                .withIsBot(true);%0D%0A    %7D%0D%0A
 %7D %0D%0A

delete no-working code (send message function) for pull request