/*
 * The MIT License
 *
 * Copyright (c) 2016 Marcelo "Ataxexe" Guimarães <ataxexe@devnull.tools>
 *
 * Permission  is hereby granted, free of charge, to any person obtaining
 * a  copy  of  this  software  and  associated  documentation files (the
 * "Software"),  to  deal  in the Software without restriction, including
 * without  limitation  the  rights to use, copy, modify, merge, publish,
 * distribute,  sublicense,  and/or  sell  copies of the Software, and to
 * permit  persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * The  above  copyright  notice  and  this  permission  notice  shall be
 * included  in  all  copies  or  substantial  portions  of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY OF ANY KIND,
 * EXPRESS  OR  IMPLIED,  INCLUDING  BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN  NO  EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM,  DAMAGES  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT  OR  OTHERWISE,  ARISING  FROM,  OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE   OR   THE   USE   OR   OTHER   DEALINGS  IN  THE  SOFTWARE.
 */

package tools.devnull.boteco.plugins.request;

import tools.devnull.boteco.ServiceLocator;
import tools.devnull.boteco.message.IncomeMessage;
import tools.devnull.boteco.message.MessageProcessor;
import tools.devnull.boteco.request.Request;
import tools.devnull.boteco.request.RequestListener;

import static tools.devnull.boteco.Predicates.command;
import static tools.devnull.boteco.message.MessageChecker.check;

/**
 * A message processor that confirms requests.
 */
public class RequestMessageProcessor implements MessageProcessor {

  private final ServiceLocator serviceLocator;
  private final RequestRepository repository;

  public RequestMessageProcessor(ServiceLocator serviceLocator,
                                 RequestRepository repository) {
    this.serviceLocator = serviceLocator;
    this.repository = repository;
  }

  @Override
  public String id() {
    return "request";
  }

  @Override
  public boolean canProcess(IncomeMessage message) {
    return check(message).accept(command("confirm"));
  }

  @Override
  public void process(IncomeMessage message) {
    String token = message.command().as(String.class);
    Request request = this.repository.pull(token);
    RequestListener listener = serviceLocator.locate(RequestListener.class, "(request=%s)", request.type());
    if (listener != null) {
      listener.onConfirm(request);
      message.reply("Confirmation OK!");
    } else {
      message.reply("Couldn't handle your confirmation!");
    }
  }
}

@@ -2435,16 +2435,45 @@
 token);%0A
+    if (request != null) %7B%0A  
     Requ
@@ -2573,24 +2573,26 @@
 ype());%0A    
+  
 if (listener
@@ -2599,24 +2599,26 @@
  != null) %7B%0A
+  
       listen
@@ -2642,24 +2642,26 @@
 est);%0A      
+ 
+ 
 message.repl
@@ -2687,16 +2687,18 @@
 %22);%0A    
+  
 %7D else %7B
@@ -2698,16 +2698,18 @@
  else %7B%0A
+  
       me
@@ -2755,24 +2755,90 @@
 rmation!%22);%0A
+      %7D%0A    %7D else %7B%0A      message.reply(%22%5Be%5DInvalid token%5B/e%5D%22);%0A
     %7D%0A  %7D%0A%7D%0A

check if the token is valid