<?php

namespace Drupal\edlp_studio\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\User\UserDataInterface;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\JsonResponse;

use Drupal\edlp_studio\Entity\Chutier;


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

  protected $user;
  protected $userdata;

  /**
   * Class constructor.
   */
  public function __construct(AccountInterface $account, UserDataInterface $userdata) {
    $this->user = $account;
    $this->userdata = $userdata;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    // Instantiates this form class.
    return new static(
      // Load the service required to construct this class.
      $container->get('current_user'),
      $container->get('user.data')
    );
  }

  /**
   * Studio-ui.
   *
   * @return array
   *   Return renderable array.
   */
  public function StudioUI() {
    return $this->buildStudioUI();
  }

  /**
   * Studio-ui.
   *
   * @return array
   *   Return renderable array.
   */
  public function StudioUIJson() {
    $renderable = $this->buildStudioUI();
    $rendered = render($renderable);

    // JSON
    $response = new JsonResponse();
    $response->setData([
      'rendered'=> $rendered,
    ]);
    return $response;

  }

  public function StudioChutierUIJson(){
    $renderable = $this->buildChutierUI();
    $rendered = render($renderable);

    // JSON
    $response = new JsonResponse();
    $response->setData([
      'rendered'=> $rendered,
    ]);
    return $response;
  }

  private function buildStudioUI(){
    return [
      '#theme' => 'edlp_studio_ui',
      '#chutier_ui' => $this->buildChutierUI(),
      '#composition_ui' => $this->buildCompostionUI(),
    ];
  }

  private function buildChutierUI(){
    // get his docs in chutier
    $documents_nids = Chutier::getUserChutiersContents($this->user->id());
    // dpm($documents_nids);

    $documents = entity_load_multiple('node', $documents_nids);

    // build content renderable array
    $chutier_ui = array(
      "#theme"=>'edlp_chutier_ui',
      '#title' => t('Chutier'),
      "#document_nodes" => $documents,
      '#uid' => $this->user->id(),
    );

    return $chutier_ui;
  }

  private function buildCompostionUI(){

    // build content renderable array
    $composition_ui = array(
      "#theme"=>'edlp_composition_ui',
      "#title" => t('Composition'),
      "#compositions" => $this->buildCompostionsList(),
      "#composer" => $this->buildComposer(),
    );

    return $composition_ui;
  }

  private function buildCompostionsList(){
    // get compositions
    $query = \Drupal::entityQuery('composition')
      ->condition('user_id', $this->user->id());

    $compos_nids = $query->execute();
    $compos = entity_load_multiple('composition', $compos_nids);

    $createurl = Url::fromRoute('edlp_studio.composition_controller_action_ajax', ['action' => 'create'], ['absolute' => TRUE]);

    return array(
      '#theme' => 'edlp_compositions_list',
      '#composition_entities' => $compos,
      '#new_composition_url' => $createurl,
    );
  }

  private function buildComposer(){
    return array('#markup'=>'TODO : composer UI');
  }
}
