<?php
namespace ProVM\KI\Common\Controller\Web;

use Psr\Container\ContainerInterface as Container;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Views\Blade as View;

class Proyectos {
  public function destacados(Request $request, Response $response, Container $container, $page): Response {
    $filename = implode(DIRECTORY_SEPARATOR, [
      $container->get('folders.data'),
      'destacados.json'
    ]);
    $destacados = json_decode(trim(file_get_contents($filename)));
    $max = ceil(count($destacados) / 4);
    $output = [
      'information' => [
        'page' => $page
      ],
      'destacados' => []
    ];
    for ($i = ($page - 1) * 4; $i < $page * 4; $i ++) {
      $output['destacados'] []= $destacados[$i];
    }
    $response->getBody()->write(json_encode($output));
    return $response
      ->withHeader('Content-Type', 'application/json')
      ->withStatus(201);
  }
  public function ficha(Request $request, Response $response, Container $container, View $view, $proyecto): Response {
    $filename = implode(DIRECTORY_SEPARATOR, [
      $container->get('folders.data'),
      'proyectos.json'
    ]);
    $proyectos = json_decode(trim(file_get_contents($filename)));
    $filename = implode(DIRECTORY_SEPARATOR, [
      $container->get('folders.data'),
      'destacados.json'
    ]);
    $destacados = json_decode(trim(file_get_contents($filename)));
    $destacado = false;
    if (array_search($proyecto, $destacados) !== false) {
      $destacado = true;
    }
    $proyecto = $proyectos[$proyecto];
    $proyecto->destacado = $destacado;
    return $view->render($response, 'home.destacados.ficha', compact('proyecto'));
  }
}
