<?php

/**
 * @file
 * Allows sending emails with Symfony Mailer.
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;

/**
 * Implements hook_theme().
 */
function symfony_mailer_theme($existing, $type, $theme, $path) {
  return [
    'email' => [
      'variables' => [
        'email' => NULL,
      ],
      'mail theme' => TRUE,
    ],
    'email_wrap' => [
      'variables' => [
        'email' => NULL,
        'body' => '',
        'is_html' => TRUE,
      ],
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_HOOK() for email.
 */
function symfony_mailer_theme_suggestions_email(array $variables) {
  return array_slice($variables['email']->getSuggestions('email', '__'), 1);
}

/**
 * Implements hook_theme_suggestions_HOOK() for email_wrap.
 */
function symfony_mailer_theme_suggestions_email_wrap(array $variables) {
  return array_slice($variables['email']->getSuggestions('email_wrap', '__'), 1);
}

/**
 * Implements hook_module_implements_alter().
 */
function symfony_mailer_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'form_alter') {
    // Move our implementation to the end of the list.
    $value = $implementations['symfony_mailer'];
    unset($implementations['symfony_mailer']);
    $implementations['symfony_mailer'] = $value;
  }
}

/**
 * Implements hook_form_alter().
 */
function symfony_mailer_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  Drupal::service('symfony_mailer.helper')->formAlter($form, $form_state, $form_id);
}

/**
 * Implements hook_preprocess_HOOK() for commerce_order.
 */
function symfony_mailer_preprocess_commerce_order(&$variables) {
  if ($variables['elements']['#view_mode'] == 'email') {
    // Workaround for https://www.drupal.org/project/commerce/issues/2949726.
    unset($variables['order']['billing_information']);
  }
}

/**
 * Prepares variables for email template.
 *
 * Default template: email.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - email: Email object.
 */
function template_preprocess_email(array &$variables) {
  $email = $variables['email'];
  $variables['body'] = $email->getBody();
  $variables['subject'] = $email->getSubject();
  $variables = array_merge($variables, $email->getVariables());
}

/**
 * Prepares variables for email wrap template.
 *
 * Default template: email-wrap.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - email: Email object.
 *   - body: Body string.
 *   - is_html: True if generating HTML output, false for plain text.
 */
function template_preprocess_email_wrap(array &$variables) {
  $email = $variables['email'];
  $variables['subject'] = $email->getSubject();
  $variables['type'] = $email->getType();
  $variables['sub_type'] = $email->getSubType();
  $variables['attributes'] = new Attribute();
}

/**
 * Implements hook_help().
 */
function symfony_mailer_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'entity.mailer_policy.collection':
      return '<p>' . t('Configure Mailer Policy to customise outgoing emails in many different ways. There are many possible policies to apply including: subject; body; addresses (from, to, ...); theme; transport; convert to plain text. Each policy can be set globally or for emails of a specific type.') . '</p>';

    case 'symfony_mailer.override.status':
      $params = [':mailer_policy' => Url::fromRoute('entity.mailer_policy.collection')->toString()];
      $output = '<p>' . t('You can override email formatting from other modules with enhanced versions using all the features of the Mailer and <a href=":mailer_policy">Mailer Policy</a>. Each type of email override can be enabled or disabled independently.', $params);
      $output .= ' ' . t("When enabling an override, it is recommended also to import, which creates Policy equivalent to the other module's configuration.") . '</p>';
      return $output;

    case 'symfony_mailer.test':
      return '<p>' . t('Verify your Mailer configuration by sending a test email.') . '</p>';
  }
}
