<?php

/**
 * @file
 * Allows webform emails to be scheduled.
 */

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionInterface;

/**
 * Implements hook_entity_update().
 */
function webform_scheduled_email_entity_update(EntityInterface $entity) {
  /** @var \Drupal\webform_scheduled_email\WebformScheduledEmailManagerInterface $webform_scheduled_email_manager */
  $webform_scheduled_email_manager = \Drupal::service('webform_scheduled_email.manager');
  $webform_scheduled_email_manager->reschedule($entity);
}

/**
 * Implements hook_entity_predelete().
 */
function webform_scheduled_email_entity_predelete(EntityInterface $entity) {
  /** @var \Drupal\webform_scheduled_email\WebformScheduledEmailManagerInterface $webform_scheduled_email_manager */
  $webform_scheduled_email_manager = \Drupal::service('webform_scheduled_email.manager');
  $webform_scheduled_email_manager->unschedule($entity);
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function webform_scheduled_email_webform_delete(WebformInterface $webform) {
  /** @var \Drupal\webform_scheduled_email\WebformScheduledEmailManagerInterface $webform_scheduled_email_manager */
  $webform_scheduled_email_manager = \Drupal::service('webform_scheduled_email.manager');
  $webform_scheduled_email_manager->delete($webform);
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function webform_scheduled_email_webform_submission_delete(WebformSubmissionInterface $webform_submission) {
  /** @var \Drupal\webform_scheduled_email\WebformScheduledEmailManagerInterface $webform_scheduled_email_manager */
  $webform_scheduled_email_manager = \Drupal::service('webform_scheduled_email.manager');
  $webform_scheduled_email_manager->delete($webform_submission);
}

/**
 * Implements hook_cron().
 */
function webform_scheduled_email_cron() {
  /** @var \Drupal\webform_scheduled_email\WebformScheduledEmailManagerInterface $webform_scheduled_email_manager */
  $webform_scheduled_email_manager = \Drupal::service('webform_scheduled_email.manager');
  $webform_scheduled_email_manager->cron();
}

/**
 * Implements hook_theme().
 */
function webform_scheduled_email_theme() {
  return [
    'webform_handler_scheduled_email_summary' => [
      'variables' => ['settings' => NULL, 'handler' => [], 'status' => NULL],
    ],
  ];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function webform_scheduled_email_form_webform_admin_config_handlers_form_alter(&$form, FormStateInterface $form_state) {
  $form['webform_scheduled_email'] = [
    '#type' => 'details',
    '#title' => t('Scheduled email settings'),
    '#open' => TRUE,
    '#tree' => TRUE,
  ];
  $form['webform_scheduled_email']['schedule_type'] = [
    '#type' => 'select',
    '#title' => t('Date type'),
    '#description' => t('Scheduled emails are queued and sent via hourly <a href="@href">cron tasks</a>. To schedule an email for a specific time, requires site administrators to increase the cron task execution frequency.', ['@href' => 'https://www.drupal.org/docs/8/cron-automated-tasks/cron-automated-tasks-overview']),
    '#options' => [
      'date' => t('Date (@format)', ['@format' => 'YYYY-MM-DD']),
      'datetime' => t('Date/time (@format)', ['@format' => 'YYYY-MM-DD HH:MM:SS']),
    ],
    '#required' => TRUE,
    '#default_value' => \Drupal::config('webform_scheduled_email.settings')->get('schedule_type'),
  ];

  $form['#submit'][] = '_webform_scheduled_email_form_webform_admin_config_handlers_form_submit';
}

/**
 * Submit callback for Scheduled email settings.
 */
function _webform_scheduled_email_form_webform_admin_config_handlers_form_submit(&$form, FormStateInterface $form_state) {
  $values = $form_state->getValue('webform_scheduled_email');
  \Drupal::configFactory()
    ->getEditable('webform_scheduled_email.settings')
    ->set('schedule_type', $values['schedule_type'])
    ->save();
}
