<?php

use Slim\Http\Request;
use Slim\Http\Response;

// Routes

$app->get('/', function (Request $request, Response $response, array $args) {
    $this->logger->info("GDDWorkshop '/' route");
    return $this->renderer->render($response, 'index.html', $args);
});

$app->get('/registration', function (Request $request, Response $response, array $args) {
    $this->logger->info("GDDWorkshop '/registrtion' route");
    return $this->renderer->render($response, 'registration.html', $args);
});


$app->get('/participants', function (Request $request, Response $response, array $args) {
  $this->logger->info("GDDWorkshop '/participants' route");
 	$db = new DB($this->db);
 	$data = $db->getAll();
  return $this->renderer->render($response, 'participants.html', ['registros' => $data]);
});


$app->get('/committess', function (Request $request, Response $response, array $args) {
    $this->logger->info("GDDWorkshop '/committess' route");
    return $this->renderer->render($response, 'committess.html', $args);
});

$app->get('/practicalinfo', function (Request $request, Response $response, array $args) {
    $this->logger->info("GDDWorkshop '/practicalinfo' route");
    return $this->renderer->render($response, 'practicalinfo.html', $args);
});

$app->get('/invitedspeakers', function (Request $request, Response $response, array $args) {
    $this->logger->info("GDDWorkshop '/invitedspeakers' route");
    return $this->renderer->render($response, 'invitedspeakers.html', $args);
});

$app->get('/program', function (Request $request, Response $response, array $args) {
    $this->logger->info("GDDWorkshop '/program' route");
    return $this->renderer->render($response, 'program.html', $args);
});
/*
$mw = function ($request, $response, $next) {
    $response->getBody()->write('BEFORE');
    $response = $next($request, $response);
    $response->getBody()->write('AFTER');

    return $response;
};*/

// API ROUTES
$app->group('/api', function($app){

	$app->get("/test", function(Request $request, Response $response, array $args) {
		$this->logger->info("GDDWorkshop '/api/test' route");
		return var_dump($request);

	});

	$app->post('/register', function (Request $request, Response $response, array $args) {
	    $db = new DB($this->db);
	    $data = $request->getParsedBody();
      $messages = $this->flash;
      if(!isset($data['nombre'])||empty($data['nombre']))
        $messages->addMessageNow("submit-register-err", "First name could not be empty");
      if(!isset($data['apellido'])||empty($data['apellido']))
        $messages->addMessageNow("submit-register-err", "Last name could not be empty");
      if(!isset($data['email'])||empty($data['email']))
        $messages->addMessageNow("submit-register-err", "E-mail could not be empty");
      if(!isset($data['financiacion'])||!is_numeric($data['financiacion']))
        $messages->addMessageNow("submit-register-err", "Question about financial support must have a selected answer");
      if(!isset($data['invited'])||!is_numeric($data['invited']))
        $messages->addMessageNow("submit-register-err", "Question about 'invited to participate in this conference' must have a selected answer");
      if(!isset($data['letterinvited'])||!is_numeric($data['letterinvited']))
        $messages->addMessageNow("submit-register-err", "Question about needing a letter of invitation must have a selected answer");

      //ReCaptcha Validation
      if(!isset($data['g-recaptcha-response'])||empty($data['g-recaptcha-response'])){
        $messages->addMessageNow("submit-register-err", "Please validate captcha!");
      }
      else {
        $recaptcha = new ReCaptcha\ReCaptcha($this->settings['recaptcha']['secret-test']);
        //get remote ip from request header TODO
        $resp = $recaptcha->verify($data['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
        if(!$resp->isSuccess()){
          $messages->addMessageNow("submit-register-err", "ReCaptcha validation error " . implode($resp->getErrorCodes()));
        }
        else {
          unset($data['g-recaptcha-response']);
        }
      }

      try{
        if($db->findByMail($data['email']))
          $messages->addMessageNow("submit-register-err", $data['email']." is already registered");
        else
          $db->insert($data);
      }
      catch (Exception $e){
        $this->logger->debug("Submit register DB error: ".$e->getMessage());
        $messages->addMessageNow("submit-register-err", "DB error: ".$e->getMessage());
      }

      if($messages->hasMessage("submit-register-err")){
        $arrayresponse = array("success" => false,
        "msg"=>"You registration could not be completed:\n",
        "errors"=>$messages->getMessage("submit-register-err"), "data"=>$data);
      }
      else {
        $arrayresponse = array("success" => true,
        "msg"=>"You have been successfully registered to the GGDWorkshop",
        "data"=>$data);
      }
      $newres = $response->withJson($arrayresponse);
      return $newres;
	});

});
