<?php

/**
 * @file
 * The social profile manager notes module file.
 */

use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\profile\Entity\Profile;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;

/**
 * Implements hook_form_alter().
 */
function social_profile_manager_notes_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $profile_forms = [
    'comment_comment_profile_form',
  ];

  if (in_array($form_id, $profile_forms)) {
    if (!\Drupal::currentUser()->hasPermission('edit field manager notes')) {
      $form['field_comment_body']['#access'] = FALSE;
      unset($form['actions']);
    }
    else {
      unset($form['actions']['preview']);
      unset($form['field_comment_body']['widget'][0]['#title']);
      $form['actions']['submit']['#value'] = new TranslatableMarkup('Leave remark');
      $form['#attached']['library'][] = 'social_profile_manager_notes/manager_notes';
      $form['actions']['submit']['#submit'][] = '_social_profile_manager_notes_form_submit';
    }
  }
}

/**
 * Form submit for profile_profile_edit_form and profile_profile_add_form.
 *
 * @param array $form
 *   The profile edit form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The form state interface.
 */
function _social_profile_manager_notes_form_submit(array $form, FormStateInterface $form_state) {
  /** @var \Drupal\profile\Entity\Profile $profile */
  $profile = _social_profile_get_profile_from_route();
  // Let's be on the safe side.
  if ($profile instanceof Profile) {
    // Get the uid of the profile.
    $uid = $profile->getOwnerId();
    // Invalidate cache tag.
    Cache::invalidateTags(['user:breadcrumb:' . $uid]);
    // Set redirect to the profile page when a SM submits a note.
    $form_state->setRedirect(
      'view.user_information.user_information', [
        'user' => $uid,
      ]
    );
  }
}

/**
 * Implements hook_ENTITY_TYPE_view_alter().
 */
function social_profile_manager_notes_profile_view_alter(array &$build, EntityInterface $entity, EntityViewDisplay $display) {
  if (!\Drupal::currentUser()->hasPermission('view field manager notes')) {
    unset($build['field_manager_notes']);
  }
}

/**
 * Implements hook_theme().
 */
function social_profile_manager_notes_theme($existing, $type, $theme, $path) {
  $items = [
    'field__profile__type__comment' => [
      'template' => 'field--profile--type--comment',
      'path' => $path . '/templates',
      'render element' => 'elements',
      'base hook' => 'comment',
    ],
  ];
  return $items;
}

/**
 * Implements hook_entity_view_display_alter().
 */
function social_profile_manager_notes_entity_view_display_alter(EntityViewDisplayInterface $display, array $context): void {
  if ($context['entity_type'] === 'profile'
    && $context['view_mode'] === EntityDisplayRepositoryInterface::DEFAULT_DISPLAY_MODE) {
    $display->setComponent('field_manager_notes', [
      'weight' => 6,
      'settings' => [],
      'third_party_settings' => [],
      'type' => 'comment_default',
      'region' => 'content',
      'label' => 'visually_hidden',
    ]);
  }
}

/**
 * Implements hook_entity_form_display_alter().
 */
function social_profile_manager_notes_entity_form_display_alter(EntityFormDisplayInterface $form_display, array $context): void {
  if ($context['entity_type'] === 'profile') {
    if (in_array($context['form_mode'], [
      EntityDisplayRepositoryInterface::DEFAULT_DISPLAY_MODE,
      'edit',
    ])) {
      $form_display->removeComponent('field_manager_notes');
    }
  }
}
