<?php

namespace Drupal\materio_home\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Cache\CacheableJsonResponse;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Url;
use Drupal\core\render\RenderContext;

/**
 * Class AjaxHomeController.
 */
class AjaxHomeController extends ControllerBase {

  /**
   * Drupal\Core\Entity\EntityManagerInterface definition.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;
  /**
   * Drupal\language\ConfigurableLanguageManagerInterface definition.
   *
   * @var \Drupal\language\ConfigurableLanguageManagerInterface
   */
  protected $languageManager;
  /**
   * Drupal\Core\Render\RendererInterface definition.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;
  /**
   * Drupal\Core\Render\RendererInterface definition.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new AjaxHomeController object.
   */
  public function __construct(EntityManagerInterface $entity_manager, ConfigurableLanguageManagerInterface $language_manager, RendererInterface $renderer, EntityTypeManagerInterface $entity_type_manager) {
    $this->entityManager = $entity_manager;
    $this->languageManager = $language_manager;
    $this->renderer = $renderer;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.manager'),
      $container->get('language_manager'),
      $container->get('renderer'),
      $container->get('entity_type.manager')
    );
  }

  /**
   * Hello.
   *
   * @return string
   *   Return Hello string.
   */
  public function getHome() {

    $path = \Drupal::config('system.site')->get('page.front');

    $params = Url::fromUri("internal:" . $path)->getRouteParameters();
    $entity_type = key($params);
    $entity = $this->entityTypeManager()->getStorage($entity_type)->load($params[$entity_type]);

    $view_builder = $this->entityTypeManager()->getViewBuilder('node');
    $renderable = $view_builder->view($entity, 'default');
    $rendered = $this->renderer->executeInRenderContext(new RenderContext(), function () use ($renderable) {
      return render($renderable);
    });

    $data['rendered'] = $rendered;

    $data['#cache'] = [
      'max-age' => \Drupal\Core\Cache\Cache::PERMANENT,
      'tags' => ['materio-home-cache'],
      'contexts' => [
        'languages:language_content'
      ]
    ];
    $response = new JsonResponse();
    $response->setData($data);
    // $response = new CacheableJsonResponse($data);
    // $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
    // $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($renderable));

    return $response;
  }

}
