<?php

/**
 * @file
 * Functions to support theming in the Seven theme.
 */

use Drupal\breakpoint\Breakpoint;
use Drupal\Core\Form\FormStateInterface;
use \Drupal\Core\Url;
use Drupal\Core\Render\Markup;

use function Psy\debug;

/**
 * Implements hook_preprocess_HOOK() for HTML document templates.
 */
// function ouatminimal_preprocess_html(&$variables) {
//   // If on a node add or edit page, add a node-layout class.
//   $path_args = explode('/', \Drupal::request()->getPathInfo());
//   if ($suggestions = theme_get_suggestions($path_args, 'page', '-')) {
//     foreach ($suggestions as $suggestion) {
//       // dsm($suggestion);
//       preg_match('/taxonomy-manage-[^-]+-add$/', $suggestion, $matches);
//       // ksm($matches);
//       if ($suggestion === 'page--taxonomy-term-edit' || isset($matches)) {
//         $variables['attributes']['class'][] = 'node-form-layout';
//       }
//     }
//   }
// }

// function ouatminimal_form_alter(&$form, FormStateInterface $form_state, $form_id){
//   // dsm($form_id);
//   // create a colomuned term form (not working yet)
//   if(in_array($form_id, ['taxonomy_term_company_form', 'taxonomy_term_showroom_form'])){
//     // ksm($form);
//     $form['#theme'] = ['term_edit_form'];
//     $form['#attached']['library'][] = 'seven/node-form';
// 
//     $form['advanced']['#type'] = 'container';
//     $form['meta']['#type'] = 'container';
//     $form['meta']['#access'] = TRUE;
//     $form['meta']['changed']['#wrapper_attributes']['class'][] = 'container-inline';
//     $form['meta']['author']['#wrapper_attributes']['class'][] = 'container-inline';
// 
//     $form['revision_information']['#type'] = 'container';
//     $form['revision_information']['#group'] = 'meta';
//   }
// }

/**
 * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
 *
 * Changes vertical tabs to container.
 */
function ouatminimal_form_node_form_alter(&$form, FormStateInterface $form_state) {
  $form['#theme'] = ['node_edit_form'];
  $form['#attached']['library'][] = 'seven/node-form';

  // unset($form['advanced']);
  $form['advanced']['#type'] = 'container';
  $form['advanced']['#access'] = FALSE;
  $form['meta']['#type'] = 'container';
  $form['meta']['#access'] = TRUE;
  $form['meta']['changed']['#wrapper_attributes']['class'][] = 'container-inline';
  $form['meta']['author']['#wrapper_attributes']['class'][] = 'container-inline';

  $form['revision_information']['#type'] = 'container';
  $form['revision_information']['#group'] = 'meta';

  // $form['author']['#group']['group_admin'];

}

// https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21theme.api.php/function/hook_theme_suggestions_HOOK_alter/10
function ouatminimal_theme_suggestions_node_edit_form_alter(array &$suggestions, array $variables) {
  if ($node = \Drupal::routeMatch()->getParameter('node')){
    $nid = $node->id();
    $type = $node->getType();
    $suggestions[] = 'node_edit_form__node_' . $type;

  }
}

// https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21theme.api.php/function/hook_theme_suggestions_HOOK_alter/10
function ouatminimal_theme_suggestions_field_multiple_value_form_alter(array &$suggestions, array $variables) {
  if ($node = \Drupal::routeMatch()->getParameter('node')){
    $nid = $node->id();
    $type = $node->getType();
    $suggestions[] = 'field_multiple_value_form__' . $variables['element']['#field_name'];
    $suggestions[] = 'field_multiple_value_form__node_' . $type;
    $suggestions[] = 'field_multiple_value_form__' . $variables['element']['#field_name'] . '__node_' . $type;

  }
}
// field-multiple-value-form--field-entite--node-concernement
function ouatminimal_preprocess_field_multiple_value_form__field_entite__node_concernement(&$variables) {
  // $element = $variables['element'];
  $attributes = $variables['attributes'];
  $variables['#attached']['library'][] = 'ouatminimal/boussole';
}
// template_preprocess_field_multiple_value_form
// function ouatminimal_preprocess_field_multiple_value_form(&$variables) {
//   $element = $variables['element'];
//   $attributes = $variables['attributes'];
//   if($attributes['data-drupal-selector'] === 'edit-field-entite' ){
//     $t="t";
//   }
// }

function ouatminimal_preprocess_toolbar(&$variables) {
  unset($variables['tabs']['home']['link']['#options']['attributes']['data-toolbar-escape-admin']);
  unset($variables['tabs']['home']['link']['#attributes']['data-toolbar-escape-admin']);
  unset($variables['tabs']['home']['link']['#markup']);
  unset($variables['tabs']['home']['link']['#children']);
  $uri = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'];
  $variables['tabs']['home']['link']['#url'] = Url::fromUri($uri);
}

function ouatminimal_preprocess_form_element(&$variables) {
  $element = $variables['element'];
  
  if(isset($variables['description_display'])){
    $variables['description_display']="before";   
  }

}

/**
 * Prepares variables for text format wrapper templates.
 *
 * Default template: text-format-wrapper.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - attributes: An associative array containing properties of the element.
 */
// function ouatminimal_preprocess_text_format_wrapper(&$variables) {
//   $t="t";
//   // $children = $variables['children'];
// }

function ouatminimal_preprocess_textarea(&$variables) {
  $element = $variables['element'];
  unset($variables['element']['#cols']);
  $variables['attributes']->removeAttribute('cols');
  // $variables['attributes']->setAttribute('cols', 40);
}

function ouatminimal_preprocess_node_edit_form__node_concernement(&$variables) {
  // $variables['form']['advanced']['#group'] = "group_admin";
  // $ph = $variables['form']['title']['widget'][0]['value']['#placeholder'];
  // $variables['form']['title']['widget']['#description'] = $ph;
  // $variables['form']['title']['widget'][0]['#description'] = $ph;
  // $variables['form']['title']['widget'][0]['value']['#description'] = $ph;
  
  
  // $variables['form']['title']['widget']['#title'] = $ph;
  // $variables['form']['title']['widget'][0]['#title'] = $ph;
  // $variables['form']['title']['widget'][0]['value']['#title'] = $ph;
  // $variables['form']['title']['widget'][0]['value']['#placeholder'] = "";
}

function ouatminimal_preprocess_input(&$variables) {
  $element = &$variables['element'];
  if ($element['#type'] === "submit") {
    // edit-field-entite-0-subform-field-entite-actions-ief-add
    $name = $element['#name'];
    
    // rename entité reference in paragraphe "add node" to "editer entité"
    if (preg_match('/^ief-field_entite-\d+-subform-field_entite-form-add$/', $name)) {
      $element['#value'] = $element['#attributes']['value'] = $variables['attributes']['value'] = "Editer l'entité";
    } 

    // ief-add-submit-field_entite-15-subform-field_entite-form
    if (preg_match('/^ief-add-submit-field_entite-\d+-subform-field_entite-form$/', $name)) {
      $element['#value'] = $element['#attributes']['value'] = $variables['attributes']['value'] = "Créer l'entité";
    } 

    // ief-edit-submit-field_entite-14-subform-field_entite-form-0
    if (preg_match('/^ief-edit-submit-field_entite-\d+-subform-field_entite-form-\d+$/', $name)) {
      $element['#value'] = $element['#attributes']['value'] = $variables['attributes']['value'] = "Enregistrer l'entité";
    } 

    // rename remove (paragraphe) to "supprimer" 
    if (preg_match('/field_entite_\d+_remove/', $name)) {
      $element['#value'] = $element['#attributes']['value'] = $variables['attributes']['value'] = "Supprimer";
    }
    
    // hide remove node ref submit
    if (preg_match('/^ief-field_entite-\d+-subform-field_entite-form-entity-remove-\d+$/', $name)) {
      $element['#value'] = $element['#attributes']['value'] = $variables['attributes']['value'] = "test";
      $variables['attributes']['class'][] = 'ief-submit-remove';
    }

    // puissance d'agir
    // rename ajouter besoin
    // edit-field-besoin-actions-ief-add--auhryC0X7yE
    if (preg_match('/^ief-field_besoin-form-add$/', $name)) {
      $element['#value'] = $element['#attributes']['value'] = $variables['attributes']['value'] = "Ajouter un Besoin";
    } 
    // ief-edit-submit-field_besoin-form-0
    if (preg_match('/^ief-edit-submit-field_besoin-form-\d$/', $name)) {
      $element['#value'] = $element['#attributes']['value'] = $variables['attributes']['value'] = "Mise à jour du besoin";
    } 
    // ief-add-submit-field_besoin-form
    if (preg_match('/^ief-add-submit-field_besoin-form$/', $name)) {
      $element['#value'] = $element['#attributes']['value'] = $variables['attributes']['value'] = "Créer le besoin";
    } 
    
    
    
  }
  if ($element['#type'] === "text" || $element['#type'] === "url") {
    unset($variables['element']['#size']);
    // $variables['attributes']->removeAttribute('size');
    // $variables['attributes']->setAttribute('cols', 40);
  }
}