<?php

/**
 * @file
 * Provides installations method.
 */

use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\link\LinkItemInterface;

/**
 * Implements hook_install().
 */
function nodehive_core_install() {
  // Add some default permissions.
  $roles = [
    "nodehive_content_editor",
    "nodehive_content_admin"
  ];

  foreach ($roles as $rid) {
    user_role_grant_permissions(
      $rid,
      [
        "administer nodehive space",
        "administer nodehive users"
      ]);
  }

  // Auto-enable the submodules.
  $installer = \Drupal::service('module_installer');
  $installer->install([
    'nodehive_core_space_dashboard',
    'nodehive_visualeditor'
  ]);
}

/**
 * Implements hook_uninstall().
 */
function nodehive_core_uninstall() {
  // Remove some default permissions.
  $roles = [
    "nodehive_content_editor",
    "nodehive_content_admin"
  ];

  foreach ($roles as $rid) {
    user_role_revoke_permissions($rid, ["administer nodehive space"]);
  }
}

/**
 * Implements hook_update_n().
 *
 * Enable translation modules.
 */
function nodehive_core_update_10000() {
  \Drupal::service("module_installer")->install([
    "config_translation",
    "content_translation",
    "locale",
    "language",
  ]);
}

/**
 * Implements hook_update_n().
 *
 * Add space type and space tag fields.
 */
function nodehive_core_update_10001() {
  $space_type_definition = BaseFieldDefinition::create('list_string')
    ->setLabel(t('Space Type'))
    ->setDescription(t('Type of space.'))
    ->setSetting('allowed_values_function', 'nodehive_core_space_type_allowed_values')
    ->setRequired(TRUE)
    ->setCardinality(1)
    ->setDisplayOptions('form', [
      'type' => 'select',
      'weight' => 0,
    ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', FALSE);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition(
      'type',
      'nodehive_space',
      'nodehive_space',
      $space_type_definition
  );

  // Create new field.
  $tags_definition = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Tags'))
    ->setDescription(t('Space tags.'))
    ->setRevisionable(TRUE)
    ->setSetting('target_type', 'taxonomy_term')
    ->setSetting('handler', 'default')
    ->setSetting('handler_settings', [
      'target_bundles' => [
        'nodehive_space_tags' => 'nodehive_space_tags',
      ],
      'auto_create' => TRUE,
      'auto_create_bundle' => 'nodehive_space_tags',
    ])
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setTranslatable(TRUE)
    ->setDisplayOptions('form', [
      'type' => 'entity_reference_autocomplete',
      'weight' => 0,
      'settings' => [
        'match_operator' => 'CONTAINS',
        'size' => '60',
        'placeholder' => '',
      ],
    ]);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition(
      'tags',
      'nodehive_space',
      'nodehive_space',
      $tags_definition
    );

  // Create new taxonomy vocabulary.
  $tags_vocabulary = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_vocabulary')
    ->create([
      'vid' => 'nodehive_space_tags',
      'description' => '',
      'name' => 'NodeHive Space Tags',
    ]);
  $tags_vocabulary->save();
}

/**
 * Implements hook_update_n().
 *
 * Migrate to a new space url field.
 */
function nodehive_core_update_10002() {
  // Create new field.
  $space_url_definition = BaseFieldDefinition::create('link')
    ->setLabel(t('Url'))
    ->setDescription(t('The space urls.'))
    ->setRequired(TRUE)
    ->setSettings([
      'link_type' => LinkItemInterface::LINK_EXTERNAL,
      'title' => DRUPAL_DISABLED,
    ])
    ->setDisplayOptions('form', [
      'type' => 'link_default',
      'weight' => 0,
    ])
    ->setCardinality(1)
    ->setDisplayConfigurable('form', TRUE);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition(
      'space_url',
      'nodehive_space',
      'nodehive_space',
      $space_url_definition
    );

  $database = \Drupal::database();

  // Retrieve existing field data.
  $urls = $database->select('nodehive_space__url', 'sp')
    ->fields('sp', ['entity_id', 'url_uri', 'url_options'])
    ->execute()
    ->fetchAllKeyed();

  $url_per_entity = [];
  foreach ($urls as $entity_id => $url) {
    if (!isset($url_per_entity[$entity_id])) {
      $url_per_entity[$entity_id] = $url;
    }
  }

  foreach ($url_per_entity as $entity_id => $url) {
    $entity = \Drupal::entityTypeManager()
      ->getStorage('nodehive_space')
      ->load($entity_id);

    $entity->set('space_url', $url);
    $entity->save();
  }
}

/**
 * Implements hook_update_n().
 *
 * Create a new 'space_type' field that replaces the 'type' field.
 */
function nodehive_core_update_10003() {
  // Create a new space_type field.
  $space_type_definition = BaseFieldDefinition::create('list_string')
    ->setLabel(t('Space Type'))
    ->setDescription(t('Type of space.'))
    ->setSetting('allowed_values_function', 'nodehive_core_space_type_allowed_values')
    ->setRequired(TRUE)
    ->setCardinality(1)
    ->setDisplayOptions('form', [
      'type' => 'select',
      'weight' => 0,
    ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', FALSE);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition(
      'space_type',
      'nodehive_space',
      'nodehive_space',
      $space_type_definition
  );
}

/**
 * Implements hook_update_n().
 *
 * Create a new 'frontpage_node' and 'forntpage_login_url ' fields.
 */
function nodehive_core_update_10004() {
  $frontpage_node = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Frontpage Node'))
    ->setDescription(t('Node used for the frontpage.'))
    ->setSetting('target_type', 'node')
    ->setRequired(FALSE)
    ->setSetting('handler', 'default')
    ->setCardinality(1)
    ->setDisplayOptions('form', [
      'type' => 'entity_reference_autocomplete',
      'weight' => 0,
      'settings' => [
        'match_operator' => 'CONTAINS',
        'size' => '60',
        'placeholder' => '',
      ],
    ]);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition(
      'frontpage_node',
      'nodehive_space',
      'nodehive_space',
      $frontpage_node
    );

  $frontpage_login_url = BaseFieldDefinition::create('link')
    ->setLabel(t('Frontpage Login Url'))
    ->setDescription(t('The login url for the frontpage.'))
    ->setRequired(FALSE)
    ->setSettings([
      'link_type' => LinkItemInterface::LINK_EXTERNAL,
      'title' => DRUPAL_DISABLED,
    ])
    ->setDisplayOptions('form', [
      'type' => 'link_default',
      'weight' => 0,
    ])
    ->setCardinality(1)
    ->setDisplayConfigurable('form', TRUE);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition(
      'frontpage_login_url',
      'nodehive_space',
      'nodehive_space',
      $frontpage_login_url
  );
}
