<?php

/**
 * @file
 * Hooks for AI Automator.
 */

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldConfig;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function ai_automators_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state) {
  \Drupal::service('ai_automator.field_config')->alterForm($form, $form_state);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function ai_automators_form_base_field_override_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  \Drupal::service('ai_automator.field_config')->alterForm($form, $form_state);
}

/**
 * Implements hook_entity_presave().
 */
function ai_automators_entity_presave(EntityInterface $entity) {
  if (ai_automators_entity_can_save_toggle()) {
    \Drupal::service('ai_automator.entity_modifier')->saveEntity($entity);
  }
}

/**
 * Implements hook_entity_insert().
 */
function ai_automators_entity_insert(EntityInterface $entity) {
  // Never generate on import since that will be done via the importer.
  if ($entity instanceof FieldConfig && ai_automators_can_import_toggle()) {
    \Drupal::service('ai_automator.status_field')->modifyStatusField($entity->get('entity_type'), $entity->get('bundle'));
  }
  if (ai_automators_entity_can_save_toggle()) {
    \Drupal::service('ai_automator.entity_modifier')->saveEntity($entity, TRUE);
  }
}

/**
 * Implements hook_entity_update().
 */
function ai_automators_entity_update(EntityInterface $entity) {
  if ($entity instanceof FieldConfig && ai_automators_can_import_toggle()) {
    \Drupal::service('ai_automator.status_field')->modifyStatusField($entity->get('entity_type'), $entity->get('bundle'));
  }
  if (ai_automators_entity_can_save_toggle()) {
    \Drupal::service('ai_automator.entity_modifier')->saveEntity($entity, TRUE);
  }
}

/**
 * Implements hook_entity_delete().
 */
function ai_automators_entity_delete(EntityInterface $entity) {
  if ($entity instanceof FieldConfig && ai_automators_entity_can_save_toggle() && ai_automators_can_import_toggle()) {
    \Drupal::service('ai_automator.status_field')->modifyStatusField($entity->get('entity_type'), $entity->get('bundle'));
  }
}

/**
 * Implements hook_entity_insert().
 */
function ai_automators_config_import_steps_alter(&$context) {
  // We disable generation from entities while importing, so we don't have a
  // mis-match of configurations.
  ai_automators_can_import_toggle(FALSE);
}

/**
 * Global function to toggle or check if to save.
 *
 * @param bool $newToggle
 *   The new toggle if you want to change it.
 *
 * @return bool
 *   If it is possible to save or not.
 */
function ai_automators_entity_can_save_toggle($newToggle = NULL) {
  static $toggle;
  // Set default to TRUE.
  // @codingStandardsIgnoreLine
  $toggle = $toggle === NULL ? TRUE : $toggle;
  if (isset($newToggle)) {
    $toggle = $newToggle;
  }
  return $toggle;
}

/**
 * Global function to toggle if import is possible.
 *
 * @param bool $newToggle
 *   The new toggle if you want to change it.
 *
 * @return bool
 *   If it is possible to import or not.
 */
function ai_automators_can_import_toggle($newToggle = NULL) {
  static $toggle;
  // Set default to TRUE.
  // @codingStandardsIgnoreLine
  $toggle = $toggle === NULL ? TRUE : $toggle;
  if (isset($newToggle)) {
    $toggle = $newToggle;
  }
  return $toggle;
}
