<?php

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\NodeType;
use Drupal\taxonomy\Entity\Vocabulary;

/**
 * Implements hook_install().
 */
function kamihaya_cms_content_type_install() {
  module_set_weight('kamihaya_cms_content_type', 10);
}

/**
 * Update configs.
 */
function kamihaya_cms_content_type_update_10001() {
  _kamihaya_cms_common_change_configs('kamihaya_cms_content_type', [
    'taxonomy.vocabulary.news_category.yml',
    'language.content_settings.taxonomy_term.news_category.yml',
    'core.entity_form_display.taxonomy_term.news_category.default.yml',
  ], 'install');

  _kamihaya_cms_common_change_configs('kamihaya_cms_content_type', [
    'core.entity_view_mode.node.list.yml',
    'core.entity_view_mode.node.list_with_thumbnail.yml',
    'node.type.kamihaya_news.yml',
    'language.content_settings.node.kamihaya_news.yml',
    'field.storage.node.field_direct_link_file.yml',
    'field.storage.node.field_direct_link_url.yml',
    'field.storage.node.field_weight.yml',
    'field.field.node.kamihaya_news.field_body.yml',
    'field.field.node.kamihaya_news.field_category.yml',
    'field.field.node.kamihaya_news.field_description.yml',
    'field.field.node.kamihaya_news.field_direct_link_url.yml',
    'field.field.node.kamihaya_news.field_direct_link_file.yml',
    'field.field.node.kamihaya_news.field_publish_date.yml',
    'field.field.node.kamihaya_news.field_thumbnail_image.yml',
    'field.field.node.kamihaya_news.field_weight.yml',
    'core.entity_view_mode.node.pick_up.yml',
    'core.base_field_override.node.kamihaya_news.promote.yml',
    'core.entity_form_display.node.kamihaya_news.default.yml',
    'core.entity_view_display.node.kamihaya_news.default.yml',
    'core.entity_view_display.node.kamihaya_news.list.yml',
    'core.entity_view_display.node.kamihaya_news.list_with_thumbnail.yml',
    'core.entity_view_display.node.kamihaya_news.pick_up.yml',
    'core.entity_form_display.node.kamihaya_basic.default.yml',
    'core.entity_view_display.node.kamihaya_basic.default.yml',
    'core.entity_view_display.node.kamihaya_basic.list_with_thumbnail.yml',
    'core.entity_view_display.node.kamihaya_basic.pick_up.yml',
    'field.field.node.kamihaya_basic.field_body.yml',
    'pathauto.pattern.kamihaya_basic_page.yml',
    'pathauto.pattern.kamihaya_news_page.yml',
    'views.view.contents_list_page.yml',
    'views.view.news_list.yml',
  ], 'optional');

  _kamihaya_cms_common_save_translation_configs([
    'taxonomy.vocabulary.news_category.yml',
    'node.type.kamihaya_news.yml',
    'field.storage.node.field_direct_link_file.yml',
    'field.storage.node.field_direct_link_url.yml',
    'field.storage.node.field_weight.yml',
    'field.field.node.kamihaya_news.field_body.yml',
    'field.field.node.kamihaya_news.field_category.yml',
    'field.field.node.kamihaya_news.field_description.yml',
    'field.field.node.kamihaya_news.field_direct_link_url.yml',
    'field.field.node.kamihaya_news.field_direct_link_file.yml',
    'field.field.node.kamihaya_news.field_publish_date.yml',
    'field.field.node.kamihaya_news.field_thumbnail_image.yml',
    'field.field.node.kamihaya_news.field_weight.yml',
    'views.view.contents_list_page.yml',
    'views.view.news_list.yml',
  ], 'ja');

}

/**
 * Update configs.
 */
function kamihaya_cms_content_type_update_10002() {
  _kamihaya_cms_common_change_configs('kamihaya_cms_content_type', [
    'views.view.news_list.yml',
  ], 'optional');
}

/**
 * Update configs.
 */
function kamihaya_cms_content_type_update_10003() {
  $config_factory = \Drupal::configFactory();
  $configs = [];
  if (empty($config_factory->loadMultiple(['metatag.metatag_defaults.node__kamihaya_basic']))) {
    $configs[] = 'metatag.metatag_defaults.node__kamihaya_basic.yml';
  }
  if (empty($config_factory->loadMultiple(['metatag.metatag_defaults.node__kamihaya_news']))) {
    $configs[] = 'metatag.metatag_defaults.node__kamihaya_news.yml';
  }
  $field_config = FieldStorageConfig::loadByName('node', 'field_meta_tag');
  if (!$field_config) {
    $configs = array_merge($configs, [
      'field.storage.node.field_meta_tag.yml',
      'field.field.node.kamihaya_basic.field_meta_tag.yml',
      'field.field.node.kamihaya_news.field_meta_tag.yml',
    ]);
  }
  _kamihaya_cms_common_change_configs('kamihaya_cms_content_type', $configs, 'optional');

  if ($field_config) {
    return;
  }

  $bundles = ['kamihaya_basic', 'kamihaya_news'];
  $field_config = FieldStorageConfig::loadByName('node', 'field_meta_tag');

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');

  foreach($bundles as $bundle) {
    $field = FieldConfig::loadByName('node', $bundle, 'field_meta_tag');

    $display_repository->getFormDisplay('node', $bundle)
      ->setComponent('field_meta_tag', [
        'type' => 'metatag_firehose',
      ])
      ->save();

    $display_repository->getViewDisplay('node', $bundle)
      ->setComponent('field_meta_tag', [
        'label' => 'hidden',
        'type' => 'metatag_empty_formatter',
      ])
      ->save();
  }

  _kamihaya_cms_common_save_translation_configs([
    'field.field.node.kamihaya_basic.field_meta_tag.yml',
    'field.field.node.kamihaya_news.field_meta_tag.yml',
  ], 'ja');

}

/**
 * Update configs.
 */
function kamihaya_cms_content_type_update_10004() {
  if (!empty(NodeType::load('kamihaya_article'))) {
    return;
  }

  if (empty(Vocabulary::load('article_category'))) {
    _kamihaya_cms_common_change_configs('kamihaya_cms_content_type', [
      'taxonomy.vocabulary.article_category.yml',
      'language.content_settings.taxonomy_term.article_category.yml',
      'core.entity_form_display.taxonomy_term.article_category.default.yml',
    ], 'install');
  }
  _kamihaya_cms_common_change_configs('kamihaya_cms_content_type', [
    'node.type.kamihaya_article.yml',
    'language.content_settings.node.kamihaya_article.yml',
    'field.field.node.kamihaya_article.field_body.yml',
    'field.field.node.kamihaya_article.field_category.yml',
    'field.field.node.kamihaya_article.field_description.yml',
    'field.field.node.kamihaya_article.field_meta_tag.yml',
    'field.field.node.kamihaya_article.field_publish_date.yml',
    'field.field.node.kamihaya_article.field_thumbnail_image.yml',
    'core.base_field_override.node.kamihaya_article.promote.yml',
    'core.entity_form_display.node.kamihaya_article.default.yml',
    'core.entity_view_display.node.kamihaya_article.default.yml',
    'core.entity_view_display.node.kamihaya_article.list_with_thumbnail.yml',
    'core.entity_view_display.node.kamihaya_article.pick_up.yml',
    'pathauto.pattern.kamihaya_article_page.yml',
    'metatag.metatag_defaults.node__kamihaya_article.yml',
  ], 'optional');

  _kamihaya_cms_common_save_translation_configs([
    'taxonomy.vocabulary.news_category.yml',
    'node.type.kamihaya_article.yml',
    'field.field.node.kamihaya_article.field_body.yml',
    'field.field.node.kamihaya_article.field_category.yml',
    'field.field.node.kamihaya_article.field_description.yml',
    'field.field.node.kamihaya_article.field_meta_tag.yml',
    'field.field.node.kamihaya_article.field_publish_date.yml',
    'field.field.node.kamihaya_article.field_thumbnail_image.yml',
  ], 'ja');
}

/**
 * Update configs.
 */
function kamihaya_cms_content_type_update_10005() {
  if (!empty(NodeType::load('kamihaya_article'))) {
    return;
  }

  if (empty(Vocabulary::load('article_category'))) {
    _kamihaya_cms_common_change_configs('kamihaya_cms_content_type', [
      'taxonomy.vocabulary.article_category.yml',
      'language.content_settings.taxonomy_term.article_category.yml',
      'core.entity_form_display.taxonomy_term.article_category.default.yml',
    ], 'install');
  }
  _kamihaya_cms_common_change_configs('kamihaya_cms_content_type', [
    'node.type.kamihaya_article.yml',
    'language.content_settings.node.kamihaya_article.yml',
    'field.field.node.kamihaya_article.field_body.yml',
    'field.field.node.kamihaya_article.field_category.yml',
    'field.field.node.kamihaya_article.field_description.yml',
    'field.field.node.kamihaya_article.field_meta_tag.yml',
    'field.field.node.kamihaya_article.field_publish_date.yml',
    'field.field.node.kamihaya_article.field_thumbnail_image.yml',
    'core.base_field_override.node.kamihaya_article.promote.yml',
    'core.entity_form_display.node.kamihaya_article.default.yml',
    'core.entity_view_display.node.kamihaya_article.default.yml',
    'core.entity_view_display.node.kamihaya_article.list_with_thumbnail.yml',
    'core.entity_view_display.node.kamihaya_article.pick_up.yml',
    'pathauto.pattern.kamihaya_article_page.yml',
    'metatag.metatag_defaults.node__kamihaya_article.yml',
  ], 'optional');

  _kamihaya_cms_common_save_translation_configs([
    'taxonomy.vocabulary.news_category.yml',
    'node.type.kamihaya_article.yml',
    'field.field.node.kamihaya_article.field_body.yml',
    'field.field.node.kamihaya_article.field_category.yml',
    'field.field.node.kamihaya_article.field_description.yml',
    'field.field.node.kamihaya_article.field_meta_tag.yml',
    'field.field.node.kamihaya_article.field_publish_date.yml',
    'field.field.node.kamihaya_article.field_thumbnail_image.yml',
  ], 'ja');
}

/**
 * Update configs.
 */
function kamihaya_cms_content_type_update_10006() {
  $content_types = [];
  if (!empty(NodeType::load('kamihaya_article'))) {
    $content_types[] = 'kamihaya_article';
  }
  if (!empty(NodeType::load('kamihaya_basic'))) {
    $content_types[] = 'kamihaya_basic';
  }
  if (!empty(NodeType::load('kamihaya_news'))) {
    $content_types[] = 'kamihaya_news';
  }
  if (empty($content_types)) {
    return;
  }

  foreach($content_types as $content_type) {
    _kamihaya_cms_common_add_paragraph('node', $content_type, 'block', 'field_body');
    _kamihaya_cms_common_add_paragraph('node', $content_type, 'views', 'field_body');
  }
}

/**
 * Update configs.
 */
function kamihaya_cms_content_type_update_10007() {
  $content_types = [];
  if (!empty(NodeType::load('kamihaya_article'))) {
    $content_types[] = 'kamihaya_article';
  }
  if (!empty(NodeType::load('kamihaya_basic'))) {
    $content_types[] = 'kamihaya_basic';
  }
  if (!empty(NodeType::load('kamihaya_news'))) {
    $content_types[] = 'kamihaya_news';
  }
  if (empty($content_types)) {
    return;
  }
  foreach($content_types as $content_type) {
    _kamihaya_cms_common_add_paragraph('node', $content_type, 'border_box', 'field_body');
  }
}

/**
 * Update configs.
 */
function kamihaya_cms_content_type_update_10008() {
  $content_types = [];
  if (!empty(NodeType::load('kamihaya_article'))) {
    $content_types[] = 'kamihaya_article';
  }
  if (!empty(NodeType::load('kamihaya_basic'))) {
    $content_types[] = 'kamihaya_basic';
  }
  if (!empty(NodeType::load('kamihaya_news'))) {
    $content_types[] = 'kamihaya_news';
  }
  if (empty($content_types)) {
    return;
  }
  foreach($content_types as $content_type) {
    _kamihaya_cms_common_add_paragraph('node', $content_type, 'field_not_display_title', 'field_body');
  }
}

/**
 * Update configs.
 */
function kamihaya_cms_content_type_update_10009() {
  $content_types = [];
  if (!empty(NodeType::load('kamihaya_article'))) {
    $content_types[] = 'kamihaya_article';
  }
  if (!empty(NodeType::load('kamihaya_basic'))) {
    $content_types[] = 'kamihaya_basic';
  }
  if (!empty(NodeType::load('kamihaya_news'))) {
    $content_types[] = 'kamihaya_news';
  }
  if (empty($content_types)) {
    return;
  }
  foreach($content_types as $content_type) {
    _kamihaya_cms_common_add_paragraph('node', $content_type, 'anchor_link', 'field_body');
  }
}

/**
 * Update configs.
 */
function kamihaya_cms_content_type_update_10010() {
  $content_types = [];
  if (!empty(NodeType::load('kamihaya_article'))) {
    $content_types[] = 'kamihaya_article';
  }
  if (!empty(NodeType::load('kamihaya_basic'))) {
    $content_types[] = 'kamihaya_basic';
  }
  if (!empty(NodeType::load('kamihaya_news'))) {
    $content_types[] = 'kamihaya_news';
  }
  if (empty($content_types)) {
    return;
  }
  foreach($content_types as $content_type) {
    _kamihaya_cms_common_add_paragraph('node', $content_type, 'background_image', 'field_body');
  }
}

/**
 * Update the module weight.
 */
function kamihaya_cms_content_type_update_10011() {
  module_set_weight('kamihaya_cms_content_type', 10);
}

/**
 * Implements hook_update_dependencies().
 */
function kamihaya_cms_content_type_update_dependencies() {
  $dependencies['kamihaya_cms_content_type'] = [
    10006 => [
      'kamihaya_cms_paragraph_type' => 10007,
    ],
    10007 => [
      'kamihaya_cms_paragraph_type' => 10008,
    ],
    10009 => [
      'kamihaya_cms_paragraph_type' => 10012,
    ],
    10010 => [
      'kamihaya_cms_paragraph_type' => 10013,
    ],
  ];
  return $dependencies;
}
