<?php

namespace Drupal\yufu_concept\Plugin\rest\resource;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\TypedData\Exception\MissingDataException;
use Drupal\node\NodeInterface;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Psr\Log\LogLevel;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
 * Creates post endpoint to create new concept.
 *
 * @RestResource(
 *   id = "add_concept_rest_resource",
 *   label = @Translation("Create or edit a concept"),
 *   uri_paths = {
 *     "canonical" = "/api/pojem/dodaj",
 *     "create" = "/api/pojem/dodaj",
 *   }
 * )
 */
class AddConcept extends ResourceBase {

  use StringTranslationTrait;

  /**
   * A current user instance.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * Entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a Drupal\rest\Plugin\ResourceBase object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param array $serializer_formats
   *   The available serialization formats.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   A current user instance.
   */
  public function __construct(
    array                  $configuration,
    string                 $plugin_id,
    array                  $plugin_definition,
    array                  $serializer_formats,
    LoggerInterface        $logger,
    AccountProxyInterface  $current_user,
    EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
    $this->currentUser = $current_user;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->getParameter('serializer.formats'),
      $container->get('logger.channel.yufu_concept'),
      $container->get('current_user'),
      $container->get('entity_type.manager')
    );
  }

  /**
   * Ustvari nov koncept.
   *
   * Logika:
   * 1. Zloadamo uporabnika preko emaila, ce ne obstaja, ga ustvarimo.
   * 2. Zloadamo koncept prek uuid, ce ne obstaja, da ustvarimo.
   *    - avtor je zloadan prek maila
   *
   * @param \Symfony\Component\HttpFoundation\Request;
   *   Post request.
   *
   * @return \Drupal\rest\ResourceResponse
   *  Returns rest resource.
   */
  public function post(Request $request) {
    $response_status['status'] = FALSE;
    $data = json_decode($request->getContent(), TRUE);
    // You must to implement the logic of your REST Resource here.
    // Use current user after pass authentication to validate access.
    if (!$this->currentUser->hasPermission('access content')) {
      throw new AccessDeniedHttpException();
    }
    // Fields: naslov, text, povezani pojmi, email
    // Optional fields: language, related concept.
    if (empty($data['title']) || empty($data['text']) || empty($data['uuid'])) {
      throw new MissingDataException('Title, uuid or text missing.');
    }
    $uid = $this->getUserIdByEmail($data['email'] ?? null);
    if ($concept = $this->getConceptFromUuid($data['uuid'])) {
      // Concept exists - create a new revision.
      $concept->setNewRevision(TRUE);
      $concept->setRevisionUserId($uid);
      $concept->set('title', $data['title']);
      $concept->set('body', $data['text'] ?? $concept->body->value);
      $concept->isDefaultRevision(FALSE);
      $concept->setRevisionLogMessage('New revision by concept endpoint.');
      $concept->moderation_state->target_id = 'draft';
      $concept->save();
      $this->logger->notice('New concept @title revision @revid created by uid @uid.', [
        '@title' => $concept->getTitle(),
        '@revid' => $concept->getRevisionId(),
        '@uid' => $uid,
      ]);
    }
    else {
      // Concept does not exist - create a new node.
      $concept = [
        'type' => 'concept',
        'title' => $data['title'],
        'body' => $data['text'],
        'uuid' => $data['uuid'],
        'uid' => $uid,
        'moderation_state' => 'draft',
      ];
      // @TODO Check if related concepts are set and add them to the concept.
      // @TODO Check language and add set it on concept if exists.
      /** @var \Drupal\node\Entity\NodeInterface $concept */
      $concept = $this->entityTypeManager->getStorage('node')->create($concept);
      $concept->save();
      $this->logger->log(LogLevel::NOTICE, $this->t('Creating concept: @title', [
        '@title' => $concept->getTitle(),
      ]));
    }
    $response_status['concept'] = [
      'title' => $concept->label(),
      'id' => $concept->id(),
      'uuid' => $concept->uuid(),
      'revision_id' => $concept->getRevisionId(),
      'revision_create_time' => $concept->getRevisionCreationTime(),
      'revision_uid' => $concept->getRevisionUserId(),
      'uid' => $concept->uid->target_id,
    ];
    $response = new ResourceResponse($response_status);
    return $response;
  }

  /**
   * Get concept from uuid.
   *
   * If concept is new, return null.
   *
   * @param string|null $uuid
   *   Uuid of the node.
   *
   * @return \Drupal\node\NodeInterface|null
   */
  protected function getConceptFromUuid(?string $uuid): ?NodeInterface {
    if (!$uuid) {
      return NULL;
    }
    $node_storage = $this->entityTypeManager->getStorage('node');
    if ($concept = end($node_storage->loadByProperties(['uuid' => $uuid]))) {
      return $concept;
    }
    return NULL;
  }

  /**
   * Loads user from email or creates a new blocked user.
   *
   * Side effect is to create a new user.
   *
   * @param string|null $email
   *   Email provided by the fe call to endpoint.
   *
   * @return int
   *   Id of the user.
   *
   */
  protected function getUserIdByEmail(?string $email): int {
    if (!$email) {
      return 0;
    }
    // Load user by email. If it doesn't exist, create one.
    $user_storage = $this->entityTypeManager->getStorage('user');
    if ($user = end($user_storage->loadByProperties(['mail' => $email]))) {
      return $user->id();
    }
    $user = [
      'mail' => $email,
      'name' => $email,
      'status' => 0,
    ];
    $new_user = $user_storage->create($user);
    $new_user->save();
    return $new_user->id();
  }

}
