<?php

namespace Drupal\edlp_studio\Plugin\Block;

use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Url;

/**
 * Provides a 'StudioLinkBlock' block.
 *
 * @Block(
 *  id = "edlp_studio_link_block",
 *  admin_label = @Translation("Studio link block"),
 * )
 */
class StudioLinkBlock extends BlockBase  implements ContainerFactoryPluginInterface{

  protected $user;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    // Instantiates this form class.
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('current_user')
    );
  }

  /**
  * @param array $configuration
  * @param string $plugin_id
  * @param mixed $plugin_definition
  * @param \Drupal\Core\Session\AccountProxyInterface $account
  */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $account) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->user = $account;
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build = [];
    if($this->user->id()){
      $url = Url::fromRoute('edlp_studio.studio_ui', [], ['absolute' => TRUE]);
      $build['edlp_studio_link_block'] = array(
        '#title' => "Studio",
        '#type' => 'link',
        '#url' => $url,
        '#options'=>array(
          'attributes' => array(
            'data-drupal-link-system-path' => $url->getInternalPath(),
            'class' => array('ajax-link'),
          )
        )
      );

    }
    return $build;
  }

}
