<?php

/**
 * @file
 * Contains cookieconsent.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function cookieconsent_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the cookieconsent module.
    case 'help.page.cookieconsent':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('CookieConsent uses the Cookie Consent javascript library for alerting users about the use of cookies on your website') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_theme().
 */
function cookieconsent_theme($existing, $type, $theme, $path) {
  return [
    'cookieconsent' => [
      'variables' => [
        'dismiss' => '',
        'message' => '',
        'target' => '',
        'link' => '',
        'learn_more' => '',
      ],
    ],
  ];
}

/**
 * Implements hook_page_attachments().
 */
function cookieconsent_page_attachments(array &$attachments) {
  // Does the popup has to be shown for this user?
  $current_user = \Drupal::currentUser();
  $exclude_from_consent = $current_user->hasPermission('exclude from cookieconsent');

  // If not then proceed.
  if (!$exclude_from_consent) {
    $config = \Drupal::config('cookieconsent.settings');

    // Add the CookieConsent options.
    $attachments['#attached']['library'][] = 'cookieconsent/settings';

    if ($config->get('customise')) {
      // Set our custom texts.
      $attachments['#attached']['drupalSettings']['cookieconsent']['message'] = $config->get('headline_text');
      $attachments['#attached']['drupalSettings']['cookieconsent']['dismiss'] = $config->get('accept_button_text');
      $attachments['#attached']['drupalSettings']['cookieconsent']['learnMore'] = $config->get('read_more_button_text');
    }

    $alias = '';
    $node_id = $config->get('cookie_policy');
    if (is_numeric($node_id)) {
      $alias = Url::fromRoute('entity.node.canonical', ['node' => $node_id])->toString();
    }
    $attachments['#attached']['drupalSettings']['cookieconsent']['link'] = $alias;

    $attachments['#attached']['drupalSettings']['cookieconsent']['path'] = $config->get('path');
    $attachments['#attached']['drupalSettings']['cookieconsent']['expiry'] = (int) $config->get('expiry');
    $attachments['#attached']['drupalSettings']['cookieconsent']['target'] = $config->get('target');

    // Set the cookie domain.
    /** @var \Drupal\Core\Session\SessionConfiguration $session_confguration */
    $session_configuration = \Drupal::service('session_configuration');
    $session_configuration_options = $session_configuration->getOptions(\Drupal::request());

    $attachments['#attached']['drupalSettings']['cookieconsent']['domain'] = $session_configuration_options['cookie_domain'];

    // Render the template and pass it over to our javascript.
    /** @var Drupal\Core\Render\Renderer $renderer */
    $renderer = \Drupal::service('renderer');
    $element = [
      '#theme' => 'cookieconsent',
      '#dismiss' => $config->get('accept_button_text'),
      '#message' => $config->get('headline_text'),
      '#target' => $config->get('target'),
      '#link' => $alias,
      '#learn_more' => $config->get('read_more_button_text'),
    ];
    /** @var Drupal\Core\Render\Markup $markup */
    $markup = $renderer->renderRoot($element);

    // Cast to string (Uses the magic __toString method).
    $attachments['#attached']['drupalSettings']['cookieconsent']['markup'] = (string) $markup;

    // Determine if a container is used.
    $container = $config->get('container');
    if (!empty($container)) {
      $attachments['#attached']['drupalSettings']['cookieconsent']['container'] = $config->get('container');
    }
    else {
      $attachments['#attached']['drupalSettings']['cookieconsent']['container'] = NULL;
    }

    // Determine what theme to use.
    $theme = $config->get('theme');

    if ($theme === 'none') {
      $attachments['#attached']['drupalSettings']['cookieconsent']['theme'] = $config->get('theme_path');
    }
    else {
      // Add our CSS library that contains the CSS of the chosen theme
      // (see cookieconsent.libraries.yml).
      $attachments['#attached']['library'][] = 'cookieconsent/' . $theme;
      // Set it to FALSE, so no external theme is being loaded.
      $attachments['#attached']['drupalSettings']['cookieconsent']['theme'] = FALSE;
    }

    // Add the CookieConsent library itself.
    if ($config->get('minified')) {
      $attachments['#attached']['library'][] = 'cookieconsent/cookieconsent-min';
    }
    else {
      $attachments['#attached']['library'][] = 'cookieconsent/cookieconsent';
    }
  }
}
