<?php

namespace Drupal\materio_commerce\Plugin\Commerce\CheckoutPane;

use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface;
use Drupal\node\Entity\Node;
use Drupal\Core\Url;
use Drupal\Core\Link;

/**
 * Provides the completion message pane.
 *
 * @CommerceCheckoutPane(
 *   id = "materio_commerce_agree_nominative",
 *   label = @Translation("Agree to Nominative"),
 *   default_step = "review",
 * )
 */
class MaterioCommerceAgreeNominative extends CheckoutPaneBase implements CheckoutPaneInterface {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
        'path' => NULL,
        'text' => Null,
        'link_text' => NULL,
        'invalid_text' => 'You must agree with the nominative terms before continuing'
      ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationSummary() {
    $text = $this->configuration['text'];
    $invalid_text = $this->configuration['invalid_text'];
    $link_text = $this->configuration['link_text'];
    $path = $this->configuration['path'];
    $summary = '';
    if (!empty($text)) {
      $summary .= $this->t('Text: @text', ['@text' => $text]) . '<br/>';
    }
    if (!empty($link_text)) {
      $summary .= $this->t('Link text: @text', ['@text' => $link_text]) . '<br/>';
    }
    if (!empty($invalid_text)) {
      $summary .= $this->t('Error text: @text', ['@text' => $invalid_text]) . '<br/>';
    }
    if (!empty($path)) {
      $summary .= $this->t('Multi page: @path', ['@path' => $path]);
    }

    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['nom_text'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Text'),
      '#default_value' => $this->configuration['text'],
    ];
    $form['nom_invalid_text'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Invalid text'),
      '#default_value' => $this->configuration['invalid_text'],
    ];
    $form['nom_link_text'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Link text'),
      '#default_value' => $this->configuration['link_text'],
      '#required' => TRUE,
    ];

    if ($this->configuration['path']) {
      $path = $this->configuration['path'];
    }
    else {
      $path = NULL;
    }
    $form['nom_path'] = [
      '#title' => 'path',
      '#type' => 'textfield',
      '#default_value' => $path,
      '#required' => TRUE
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    // Todo  validate existing path
    // Get route name and parameters from path.
    // $url_object = \Drupal::service('path.validator')->getUrlIfValid($path);
    // $route_name = $url_object->getRouteName();
    // $route_parameters = $url_object->getrouteParameters();
    
    if (!$form_state->getErrors()) {
      $values = $form_state->getValue($form['#parents']);
      $this->configuration['text'] = $values['nom_text'];
      $this->configuration['invalid_text'] = $values['nom_invalid_text'];
      $this->configuration['link_text'] = $values['nom_link_text'];
      $this->configuration['path'] = $values['nom_path'];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
    $text = $this->configuration['text'];
    $link_text = $this->configuration['link_text'];
    $path = $this->configuration['path'];
    // $attributes = [];
    // if ($this->configuration['new_window']) {
    //   $attributes = ['attributes' => ['target' => '_blank']];
    // }
    // $link = Link::createFromRoute(
    //   $this->t($link_text),
    //   $path['route_name'],
    //   $path['route_parameters'],
    //   $attributes
    // )->toString();

    $url = Url::fromUserInput($path);
    $link = Link::fromTextAndUrl($link_text, $url)->toString();
    // $link = '<a href="'.$path.'">'.$this->t($link_text).'</a>';

    $pane_form['nominative_terms'] = [
      '#type' => 'checkbox',
      '#default_value' => FALSE,
      '#title' => $this->t($text, ['%multi' => $link]),
      '#required' => TRUE,
      '#weight' => $this->getWeight(),
    ];

    return $pane_form;
  }

  /**
   * {@inheritdoc}
   */
  public function validatePaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
    $values = $form_state->getValue($pane_form['#parents']);
    if (!$values['nominative_terms']) {
      $form_state->setError($pane_form, $this->configuration['invalid_text']);
    }
  }

}
