<?php

namespace Drupal\simple_interactive_maps\Plugin\MapAction;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\simple_interactive_maps\Attribute\MapAction;
use Drupal\simple_interactive_maps\MapActionPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Display content in a modal popup.
 */
#[MapAction(
  id: 'modal_content',
  label: new TranslatableMarkup('Display Modal Content'),
  description: new TranslatableMarkup('Display content in a modal popup.'),
  is_system: FALSE,
)]
final class ModalContentAction extends MapActionPluginBase implements ContainerFactoryPluginInterface {

  /**
   * Constructs the plugin instance.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    protected RendererInterface $renderer,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
    return new self(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('renderer'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {

    $form['modal_content'] = [
      '#type' => 'text_format',
      '#format' => $this->configuration['modal_content']['format'],
      '#default_value' => $this->configuration['modal_content']['value'],
      '#rows' => 10,
      '#required' => TRUE,
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state): void {

  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
    $this->configuration['modal_content'] = $form_state->getValue('modal_content');
  }

  /**
   * {@inheritdoc}
   */
  public function getActionLibrary(): string {
    return 'simple_interactive_maps/modal_content';
  }

  /**
   * {@inheritdoc}
   */
  public function getActionConfiguration(): array {
    $config = $this->getConfiguration();

    $build = [
      '#type' => 'processed_text',
      '#text' => $config['modal_content']['value'],
      '#format' => $config['modal_content']['format'],
    ];

    return ['modal_content' => $this->renderer->renderInIsolation($build)];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration(): array {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration): void {
    $this->configuration = $configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration(): array {
    return [
      'modal_content' => [
        'value' => '',
        'format' => 'basic_html',
      ],
    ];
  }

}
