<?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 Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Url;

use Drupal\edlp_studio\Entity\Compositon;

/**
 * Class CompositionController.
 */
class CompositionController 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')
    );
  }


  /**
   * AdddContent.
   *
   * @return json
   *   Return status.
   */
  public function CompositionActionJson($action, $cid, Request $request) {
    $this->error_message = null;
    $status = 'ok';
    $message = 'Hello';

    switch($action){
      case 'create':
        $name = $request->query->get('new_name');
        if($name){
          $this->createComposition($name);
        }else{
          $this->error_message = t("Composition creation needs a name as query paramater!");
        }
        break;
      // case 'delete':
      //
      //   break;
      // case 'rename':
      //
      //   break;
    }

    if($this->error_message){
      $status = 'error';
    }

    if($status == 'error'){
      $message = $this->error_message;
    }else{
      $url = Url::fromRoute('entity.composition.canonical', ['composition' => $this->compo->id()], ['absolute' => TRUE]);
      $title = $this->compo->getName();
      $new_link_build = array(
        '#title' => $title,
        '#type' => 'link',
        '#url' => $url,
        '#options'=>array(
          'attributes' => array(
            'data-drupal-link-system-path' => $url->getInternalPath(),
            'nid' => $this->compo->id(),
            'class' => ['composition-link'],
            'title'=>$title,
          ),
        ),
      );
      $new_link = render($new_link_build);
    }


    // JSON
    $response = new JsonResponse();
    $data = array(
      'action' => $action,
      'status' => $status,
      'message' => $this->error_message,
      'new_name' => $request->query->get('new_name'),
      'new_link' => $new_link,
    );
    $response->setData($data);
    return $response;

    // classic html
    // return array(
    //   '#markup'=>'Status : ' . $status . ' | Message : ' . $message,
    // );
  }

  private function createComposition($name){
    $compo = array(
      'id' => NULL,
      'uid' => $this->user->id(),
      'status' => TRUE,
      'name' => $name,
    );
    $this->compo = entity_create('composition', $compo);
    $this->compo->save();
  }

}
