<?php

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

/**
 * Implements hook_preprocess_HOOK() for html.html.twig.
 */
function erabletheme_preprocess_html(&$variables) {

}
/**
 * Implements hook_preprocess_HOOK() for page.html.twig.
 */
function erabletheme_preprocess_page(&$variables) {

}

/**
 * Implements hook_preprocess_HOOK() for node.html.twig.
 */
function erabletheme_preprocess_node(&$variables) {
  $node = $variables['node'];
  /** @var Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
  $date_formatter = \Drupal::service('date.formatter');
  $variables['date'] = $date_formatter->format($node->created->value, 'custom', 'j F Y', null, 'fr');
}
function erabletheme_preprocess_node__view__meetup(&$variables) {
  /** @var \Drupal\node\Entity $node */
  $node = $variables['node'];
  $user_id = $node->get('uid')->getValue()[0]['target_id'];
  /** @var Drupal\user\Entity  $user*/
  $user = \Drupal\user\Entity\User::load($user_id);
  /** @var Drupal\profile\Entity  $profile */
  $profile = $user->get('candidat' . '_profiles')->entity;
  if ($profile) {
    $variables['profile'] = [
      'prenom' => $profile->get('field_prenom')->getValue()[0]['value'],
      'nom' => $profile->get('field_nom')->getValue()[0]['value']
    ];
  } else {
    $variables['profile'] = [
      'prenom' => $user->getAccountName(),
      'nom' => "",
    ];
  }

}

/**
 * Implements hook_preprocess_HOOK() for node.html.twig.
 */
function erabletheme_preprocess_form(&$variables) {
  $t="t";
}

function erabletheme_preprocess_region(&$variables) {
  $t="t";
}


function erabletheme_preprocess_views_view(array &$variables) {
  if ($variables['view']->current_display === 'page_1') {
    $block_id = 'sitesmap_block';
    $block = \Drupal::service('plugin.manager.block')->createInstance($block_id, []);
    if ($block) {
      $block_render_array = $block->build();
      $variables['custom_sites_map'] = $block_render_array;
    }
  }
}

/* fonctionne mais meilleure méthode avec les suggestions page--xxx */

function erabletheme_theme_suggestions_views_view_alter(array &$suggestions, array $variables) {
  // il ne faut pas baser le template sur des contenus !
  // if ($variables['title'] !== "") {
  //   $view_title = $variables['title']['#markup'];
  //   if (isset($view_title) && $view_title === 'Actualités') {
  //     $suggestions[] = 'views_view__page_actus';
  //   } elseif (isset($view_title) && $view_title === 'Gouvernance') {
  //     $suggestions[] = 'views_view__content_gouvernance';
  //   } elseif (isset($view_title) && $view_title === 'Partenaires') {
  //     $suggestions[] = 'views_view__content_partenaires';
  //   }
    // elseif (isset($view) && $view === 'Meetup en ligne') {
    //   $suggestions[] = 'views_view__content_meetup';
    // }
  // }

  $view = $variables['view'];
  switch ($view->id()) {
    case 'actus':
      if ($view->getDisplay()->display['id'] !== 'block_1') {
        $suggestions[] = 'views_view__page_actus';
      }
      break;
    case 'gourvernance': // attention faute de frappe dans l'id dans drupal
      $suggestions[] = 'views_view__content_gouvernance';
      break;
    case 'partenaires':
      $suggestions[] = 'views_view__content_partenaires';
      break;
      case 'meetup':
        $suggestions[] = 'views_view__content_meetup';
        break;
      case 'projets':
        $suggestions[] = 'views_view__content_projets';
        break;
  }
}

function erabletheme_theme_suggestions_node_alter(array &$suggestions, array $variables) {
  $is_actu = isset($variables['elements']['field_type_d_actualite']);
  $is_projet = isset($variables['elements']['field_localisation_textuel']);
  $is_ressource = isset($variables['elements']['field_ressource_date']);

  if (!$is_projet && !$is_actu && !$is_ressource && isset($variables['elements']['#title'])) {
    $total_title = $variables['elements']['#title'];
    $start_pos = strpos($total_title, '<span>') + strlen('<span>');
    $end_pos = strpos($total_title, '</span>');
    $processed_title = substr($total_title, $start_pos, $end_pos - $start_pos);
    if ($processed_title !== 'Partenaires' || $processed_title !== 'Gouvernance') {
      $suggestions[] = 'node__content_global';
    }

  }
}

function erabletheme_theme_suggestions_region_alter(array &$suggestions, array $variables) {
  if (isset($variables['elements']['erabletheme_contenudelapageprincipale'])) {
    // il ne faut pas baser le template sur des contenus !
    // if ($variables['elements']['erabletheme_contenudelapageprincipale']['content']['#title'] == "Meet-up en ligne") {
    //   $suggestions[] = 'region__offre_meetup';
    // } else
    // if ($variables['elements']['erabletheme_contenudelapageprincipale']['content']['#title'] == "Foire aux Questions") {
    //   $suggestions[] = 'region__faq';
    // }

    // ça c'est mieux, se baser uniquement sur des infos structurelles (qui ne sont pas dynamiques)
    $routematch = \Drupal::routeMatch();
    $routename = $routematch->getRouteName();
    switch ($routename) {
      case 'faq.faq-page':
        $suggestions[] = 'region__faq';
        break;
      case 'entity.node.canonical':
        /** @var \Drupal\node\Entity\Node $node */
        $node = $routematch->getParameter('node');
        if ($node->getType() === 'offre') {
          $suggestions[] = 'region__offre_meetup';
        }
        break;
    }

  }
}