<?php

/**
 * @file
 * Install, update and uninstall functions for the social_like module.
 */

use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use Symfony\Component\Yaml\Yaml;

/**
 * Implements hook_install().
 *
 * Perform actions related to the installation of social_like.
 */
function social_like_install() {
  // Grant the default permissions for this feature.
  user_role_grant_permissions(
    RoleInterface::ANONYMOUS_ID,
    [
      'view like widget',
    ]
  );
  user_role_grant_permissions(
    RoleInterface::AUTHENTICATED_ID,
    [
      'view like widget',
    ]
  );
  user_role_grant_permissions(
    'verified',
    [
      'add or remove like votes on post of post',
      'add or remove like votes on photo of post',
      'add or remove like votes on comment of comment',
      'add or remove like votes on post_comment of comment',
      'add or remove like votes on topic of node',
      'add or remove like votes on event of node',
    ]
  );
  user_role_grant_permissions(
    'contentmanager',
    [
      'add or remove like votes on post of post',
      'add or remove like votes on photo of post',
      'add or remove like votes on comment of comment',
      'add or remove like votes on post_comment of comment',
      'add or remove like votes on topic of node',
      'add or remove like votes on event of node',
    ]
  );
  user_role_grant_permissions(
    'sitemanager',
    [
      'add or remove like votes on post of post',
      'add or remove like votes on photo of post',
      'add or remove like votes on comment of comment',
      'add or remove like votes on post_comment of comment',
      'add or remove like votes on topic of node',
      'add or remove like votes on event of node',
    ]
  );
  _social_like_set_defaults();
}

/**
 * Re-set permissions for viewing the like widget.
 */
function social_like_update_8001() {
  // Set some default permissions.
  _social_like_set_permissions();
}

/**
 * Re-set permissions for viewing the like widget.
 */
function social_like_update_8002() {
  // Revoke permission for AN like widget.
  user_role_revoke_permissions('anonymous', ['view like widget']);
}

/**
 * Disable bootstrap modal.
 */
function social_like_update_8003() {
  // Bootstrap modal breaks jquery.dialog, so turn it off.
  $settings = \Drupal::configFactory()->getEditable('socialbase.settings');
  $settings->set('modal_enabled', 0)->save();
}

/**
 * Install views_infinite_scroll module.
 */
function social_like_update_8004() {
  $modules = ['views_infinite_scroll'];
  \Drupal::service('module_installer')->install($modules);
}

/**
 * Enable deleting likes from a database when a user is deleted.
 */
function social_like_update_8005() {
  \Drupal::configFactory()->getEditable('votingapi.settings')
    ->set('delete_everywhere', TRUE)
    ->save();
}

/**
 * Function to set permissions.
 */
function _social_like_set_permissions() {
  $roles = Role::loadMultiple();

  /** @var \Drupal\user\Entity\Role $role */
  foreach ($roles as $role) {
    if ($role->id() === 'administrator') {
      continue;
    }

    $permissions = _social_like_get_permissions($role->id());
    user_role_grant_permissions($role->id(), $permissions);
  }
}

/**
 * Set default config.
 */
function _social_like_set_defaults() {
  // Add like and dislike settings.
  /** @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig $config */
  $container = \Drupal::getContainer()->get('config.factory');
  $config = $container->getEditable('like_and_dislike.settings');
  $config->set('hide_vote_widget', FALSE)->save();
  $config->set(
    'enabled_types',
    [
      'comment' => [
        'comment',
        'post_comment',
      ],
      'node' => [
        'topic',
        'event',
      ],
      'post' => [
        'post',
      ],
    ]
  )->save();
  // Add voting api settings.
  $config = $container->getEditable('like_and_dislike.settings');
  $config->set('delete_everywhere', FALSE)->save();
}

/**
 * Build the permissions.
 */
function _social_like_get_permissions($role) {
  // Anonymous.
  $permissions[RoleInterface::ANONYMOUS_ID] = ['view like widget'];

  // Authenticated.
  $permissions[RoleInterface::AUTHENTICATED_ID] = array_merge($permissions[RoleInterface::ANONYMOUS_ID], []);

  // Verified.
  $permissions['verified'] = array_merge($permissions[RoleInterface::AUTHENTICATED_ID], [
    'add or remove like votes on post of post',
    'add or remove like votes on photo of post',
    'add or remove like votes on comment of comment',
    'add or remove like votes on post_comment of comment',
    'add or remove like votes on topic of node',
    'add or remove like votes on event of node',
  ]);

  // Content manager.
  $permissions['contentmanager'] = array_merge($permissions['verified'], []);

  // Site manager.
  $permissions['sitemanager'] = array_merge($permissions['contentmanager'], []);

  if (isset($permissions[$role])) {
    return $permissions[$role];
  }

  return [];
}

/**
 * Disable the setting "Hide vote widget instead of disabling it".
 */
function social_like_update_10301() {
  \Drupal::configFactory()->getEditable('like_and_dislike.settings')
    ->set('hide_vote_widget', FALSE)
    ->save();
}

/**
 * Update message template "message.template.create_like_node_or_post".
 *
 * Added new 'preview' and 'cta_button' tokens.
 */
function social_like_update_10302() {
  $config_file = \Drupal::service('extension.list.module')->getPath('social_like') . '/config/static/message.template.create_like_node_or_post_10302.yml';
  if (is_file($config_file)) {
    $settings = Yaml::parse(file_get_contents($config_file));
    if (is_array($settings)) {
      $config = \Drupal::configFactory()
        ->getEditable('message.template.create_like_node_or_post');
      $config->setData($settings)->save(TRUE);
    }
  }
}

/**
 * Update exposed filter button name.
 */
function social_like_update_11401(): string {
  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper->executeUpdate('social_like', __FUNCTION__);

  // Output logged messages to related channel of update execution.
  return $updateHelper->logger()->output();
}

/**
 * Re-set permissions for viewing the like widget.
 */
function social_like_update_11402(): void {
  // Set some default permissions.
  _social_like_set_permissions();
}
