<?php

/**
 * @file
 * Contains wxt_ext_breadcrumb.module.
 */

use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;

/**
 * Implements hook_help().
 */
function wxt_ext_breadcrumb_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    case 'help.page.wxt_ext_breadcrumb':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Setting breadcrumbs for all of WxT.') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_system_breadcrumb_alter().
 */
function wxt_ext_breadcrumb_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInterface $route_match, array $context) {

  if (\Drupal::service('router.admin_context')->isAdminRoute()) {
    // Use core breadcrumb for admin pages.
    return;
  }

  // Get current language.
  $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  $config = \Drupal::config('wxt_ext_breadcrumb.settings');

  $enable_leading = $config->get('enable_wxt_breadcrumbs');
  $label_home = $config->get('enable_app_breadcrumb');
  $leading_links = [];
  $app_link = [];

  // Only alter leading breadcrumbs if enabled.
  if ($enable_leading) {
    // Set configured header breadcrumbs.
    if ($breadcrumb_settings = $config->get($language)) {

      for ($i = 0; $i < count($breadcrumb_settings); $i++) {
        $title = $language . '_crumb';
        $url = $language . '_url';
        if (!empty($breadcrumb_settings[$i][$title]) && !empty($breadcrumb_settings[$i][$url])) {
          if (stripos($breadcrumb_settings[$i][$url], 'http') >= 0) {
            $leading_links[] = Link::fromTextAndUrl($breadcrumb_settings[$i][$title], Url::fromUri($breadcrumb_settings[$i][$url]));
          }
        }
      }
    }
  }

  if ($label_home) {
    $breadcrumb_links = $breadcrumb->getLinks();
    $sitename = \Drupal::config('system.site')->get('name');

    $idx = 0;
    foreach ($breadcrumb_links as $key) {
      if (!is_null($breadcrumb_links[$idx])) {
        if ($breadcrumb_links[$idx]->getText() == 'Home' || $breadcrumb_links[$idx]->getText() == 'Accueil') {
          $breadcrumb_links = $breadcrumb->getLinks();
          unset($breadcrumb_links[$idx]);
          $breadcrumb = new Breadcrumb();
          $breadcrumb->setLinks($breadcrumb_links);
          $breadcrumb_links = $breadcrumb->getLinks();
          // Only expect one 'Home'.
          break;
        }
      }
      $idx++;
    }

    $is_front = \Drupal::service('path.matcher')->isFrontPage();

    if (!$is_front) {
      $app_link[] = Link::fromTextAndUrl($sitename, Url::fromRoute('<front>'));
    }
  }

  $breadcrumb_links = $breadcrumb->getLinks();
  $links = array_merge($leading_links, $app_link, $breadcrumb_links);
  $breadcrumb = new Breadcrumb();
  $breadcrumb->setLinks($links);

  // Add back the cacheable dependency at the end of these modifications.
  if ($route_match && $node = $route_match->getParameter('node')) {
    if ($node instanceof NodeInterface) {
      if ($node->bundle() == 'blog_post') {
        $links = $breadcrumb->getLinks();

        foreach ($links as $key => $link) {
          $text = $link->getText();

          if ($text instanceof TranslatableMarkup) {
            $arguments = $text->getArguments();

            if (!empty($arguments['@username'])) {
              // Remove user blog path.
              unset($links[$key]);
              $breadcrumb = new Breadcrumb();
              $breadcrumb->setLinks($links);
              break;
            }
          }
        }
      }
    }

    $breadcrumb->addCacheableDependency($node);
  }
  if ($route_match && $term = $route_match->getParameter('taxonomy_term')) {
    $breadcrumb->addCacheableDependency($term);
  }
  if ($route_match && $mlink = $route_match->getParameter('menu_link_content')) {
    $breadcrumb->addCacheableDependency($mlink);
  }

  $breadcrumb->addCacheContexts(['route', 'url.path', 'languages']);
}
