<?php

use Drupal\Core\Serialization\Yaml;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\NodeType;

/**
 * Update configs.
 */
function kamihaya_cms_paragraph_type_update_10001() {
  \Drupal::service('module_installer')->install(['entity_browser']);
  _kamihaya_cms_common_change_configs('kamihaya_cms_paragraph_type', [
    'paragraphs.paragraphs_type.headings.yml',
    'field.storage.paragraph.field_color.yml',
    'field.storage.paragraph.field_style.yml',
    'field.storage.paragraph.field_title_tag.yml',
    'field.storage.paragraph.field_title_text.yml',
    'field.field.paragraph.headings.field_color.yml',
    'field.field.paragraph.headings.field_style.yml',
    'field.field.paragraph.headings.field_title_tag.yml',
    'field.field.paragraph.headings.field_title_text.yml',
    'core.entity_form_display.paragraph.headings.default.yml',
    'core.entity_view_display.paragraph.headings.default.yml',
    'paragraphs.paragraphs_type.slider_item.yml',
    'paragraphs.paragraphs_type.slider.yml',
    'field.storage.paragraph.field_link.yml',
    'field.storage.paragraph.field_text.yml',
    'field.storage.paragraph.field_title.yml',
    'field.storage.paragraph.field_slider_image.yml',
    'field.storage.paragraph.field_slider_items.yml',
    'field.field.paragraph.slider_item.field_link.yml',
    'field.field.paragraph.slider_item.field_slider_image.yml',
    'field.field.paragraph.slider_item.field_text.yml',
    'field.field.paragraph.slider_item.field_title.yml',
    'field.field.paragraph.slider.field_slider_items.yml',
    'core.entity_form_display.paragraph.slider_item.default.yml',
    'core.entity_view_display.paragraph.slider_item.default.yml',
    'core.entity_form_display.paragraph.slider.default.yml',
    'core.entity_view_display.paragraph.slider.default.yml',
    'paragraphs.paragraphs_type.embed_tag.yml',
    'field.storage.paragraph.field_embed_tag.yml',
    'field.field.paragraph.embed_tag.field_embed_tag.yml',
    'core.entity_form_display.paragraph.embed_tag.default.yml',
    'core.entity_view_display.paragraph.embed_tag.default.yml',
    'paragraphs.paragraphs_type.file_download.yml',
    'field.storage.paragraph.field_file.yml',
    'field.field.paragraph.file_download.field_file.yml',
    'core.entity_form_display.paragraph.file_download.default.yml',
    'core.entity_view_display.paragraph.file_download.default.yml',
    'paragraphs.paragraphs_type.video.yml',
    'field.storage.paragraph.field_video_media.yml',
    'field.storage.paragraph.field_video_tag.yml',
    'field.field.paragraph.video.field_video_media.yml',
    'field.field.paragraph.video.field_video_tag.yml',
    'core.entity_form_display.paragraph.video.default.yml',
    'core.entity_view_display.paragraph.video.default.yml',
    'core.entity_form_display.paragraph.divider.default.yml',
    'core.entity_form_display.paragraph.link_button.default.yml',
  ], 'install');

  _kamihaya_cms_common_change_configs('kamihaya_cms_paragraph_type', [
    'views.view.contents_list.yml',
    'entity_browser.browser.contents_list.yml',
    'paragraphs.paragraphs_type.featured_content.yml',
    'field.storage.paragraph.field_content.yml',
    'field.field.paragraph.featured_content.field_content.yml',
    'core.entity_form_display.paragraph.featured_content.default.yml',
    'core.entity_view_display.paragraph.featured_content.default.yml',
    'paragraphs.paragraphs_type.two_col_table_row.yml',
    'field.storage.paragraph.field_row_data.yml',
    'field.storage.paragraph.field_row_label.yml',
    'field.field.paragraph.two_col_table_row.field_row_data.yml',
    'field.field.paragraph.two_col_table_row.field_row_label.yml',
    'core.entity_form_display.paragraph.two_col_table_row.default.yml',
    'core.entity_view_display.paragraph.two_col_table_row.default.yml',
    'paragraphs.paragraphs_type.two_col_table.yml',
    'field.storage.paragraph.field_table_rows.yml',
    'field.field.paragraph.two_col_table.field_table_rows.yml',
    'core.entity_form_display.paragraph.two_col_table.default.yml',
    'core.entity_view_display.paragraph.two_col_table.default.yml',
  ], 'optional');

  _kamihaya_cms_common_save_translation_configs([
    'paragraphs.paragraphs_type.headings.yml',
    'field.field.paragraph.headings.field_color.yml',
    'field.field.paragraph.headings.field_style.yml',
    'field.field.paragraph.headings.field_title_tag.yml',
    'field.field.paragraph.headings.field_title_text.yml',
    'paragraphs.paragraphs_type.slider_item.yml',
    'paragraphs.paragraphs_type.slider.yml',
    'field.field.paragraph.slider_item.field_link.yml',
    'field.field.paragraph.slider_item.field_slider_image.yml',
    'field.field.paragraph.slider_item.field_text.yml',
    'field.field.paragraph.slider_item.field_title.yml',
    'field.field.paragraph.slider.field_slider_items.yml',
    'paragraphs.paragraphs_type.embed_tag.yml',
    'field.field.paragraph.headings.feild_embed_tag.yml',
    'views.view.contents_list.yml',
    'entity_browser.browser.contents_list.yml',
    'paragraphs.paragraphs_type.featured_content.yml',
    'field.field.paragraph.featured_content.field_content.yml',
    'paragraphs.paragraphs_type.file_download.yml',
    'field.field.paragraph.file_download.field_file.yml',
    'paragraphs.paragraphs_type.two_col_table_row.yml',
    'field.field.paragraph.two_col_table_row.field_row_data.yml',
    'field.field.paragraph.two_col_table_row.field_row_label.yml',
    'paragraphs.paragraphs_type.two_col_table.yml',
    'field.field.paragraph.two_col_table.field_table_rows.yml',
    'paragraphs.paragraphs_type.video.yml',
    'field.field.paragraph.video.feild_video_media.yml',
    'field.field.paragraph.video.feild_video_tag.yml',
  ], 'ja');

}

/**
 * Update configs.
 */
function kamihaya_cms_paragraph_type_update_10002() {
  \Drupal::service('module_installer')->install(['entity_browser']);
  _kamihaya_cms_common_change_configs('kamihaya_cms_paragraph_type', [
    'field.storage.paragraph.field_color.yml',
    'field.storage.paragraph.field_mobile_image.yml',
    'field.field.paragraph.headings.field_color.yml',
    'field.field.paragraph.slider_item.field_mobile_image.yml',
    'field.field.paragraph.slider_item.field_slider_image.yml',
    'core.entity_form_display.paragraph.headings.default.yml',
    'core.entity_view_display.paragraph.headings.default.yml',
    'core.entity_form_display.paragraph.divider.default.yml',
    'core.entity_form_display.paragraph.image_link.default.yml',
    'core.entity_form_display.paragraph.link_button.default.yml',
    'field.field.paragraph.slider_item.field_image_link.yml',
    'core.entity_view_display.paragraph.slider_item.default.yml',
    'core.entity_form_display.paragraph.slider_item.default.yml',
  ], 'install');

  _kamihaya_cms_common_save_translation_configs([
    'field.field.paragraph.slider_item.field_image_link.yml',
    'field.field.paragraph.slider_item.field_mobile_image.yml',
  ], 'ja');

}

/**
 * Update configs.
 */
function kamihaya_cms_paragraph_type_update_10003() {
  $fields = [
    'field.field.paragraph.divider.field_divider_image',
    'field.field.paragraph.image_link.field_image',
    'field.field.paragraph.image.field_image',
    'field.field.paragraph.left_image_text.field_image',
    'field.field.paragraph.right_image_text.field_image',
    'field.field.paragraph.slider_item.field_mobile_image',
    'field.field.paragraph.slider_item.field_slider_image',
  ];

  foreach($fields as $field) {
    if ($field_config = \Drupal::configFactory()->getEditable($field)) {
      $value = explode(' ', $field_config->get('settings.file_extensions'));
      if (!in_array('svg', $value)) {
        $value[] = 'svg';
      }
      if (!in_array('webp', $value)) {
        $value[] = 'webp';
      }
      $field_config->set('settings.file_extensions', implode(' ', $value));
      $field_config->save(TRUE);
    }
  }
}

/**
 * Add fields to Image link Paragraph.
 */
function kamihaya_cms_paragraph_type_update_10004() {
  // Add fields to Image link Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'image_link', [
    'field_image_description',
    'field_image_desc_text_size',
  ]);
}

/**
 * Add fields to Image and Image Link paragraphs.
 */
function kamihaya_cms_paragraph_type_update_10005() {
  // Add fields to Image Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'image', [
    'field_width',
    'field_width_unit',
    'field_height',
    'field_height_unit',
  ]);

    // Add fields to Image link Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'image_link', [
    'field_width',
    'field_width_unit',
    'field_mobile_height',
  ]);
}

/**
 * Update configs.
 */
function kamihaya_cms_paragraph_type_update_10006() {
  if (empty(NodeType::load('kamihaya_article'))) {
    return;
  }
  if ($field_config = \Drupal::configFactory()->getEditable('field.field.paragraph.featured_content.field_content')) {
    $value = $field_config->get('settings.handler_settings.target_bundles');
    if (in_array('kamihaya_article', $value)) {
      return;
    }
    $value['kamihaya_article'] = 'kamihaya_article';
    $field_config->set('settings.handler_settings.target_bundles', $value);
    $field_config->save(TRUE);
  }
}

/**
 * Create Block and Views Paragraph.
 */
function kamihaya_cms_paragraph_type_update_10007() {
  // Create Block and Views Paragraph.
  _kamihaya_cms_common_create_paragraph('block', [
    'field_block',
  ], [
    'accordion_item' => 'field_accordion_body',
    'drawer' => 'field_drawer_body',
    'modal_dialog' => 'field_dialog_body',
    'two_col_table_row' => 'field_row_data',
  ]);

  // Create Views Paragraph.
  _kamihaya_cms_common_create_paragraph('views', [
    'field_views',
  ], [
    'accordion_item' => 'field_accordion_body',
    'drawer' => 'field_drawer_body',
    'modal_dialog' => 'field_dialog_body',
    'two_col_table_row' => 'field_row_data',
  ]);
}

/**
 * Create border box item and border box paragraphs.
 */
function kamihaya_cms_paragraph_type_update_10008() {
    // Create Boeder box item Paragraph.
  _kamihaya_cms_common_create_paragraph('border_box_item', [
    'field_border_box_body',
    'field_bg_color',
    'field_border_top_color',
    'field_border_bottom_color',
    'field_border_left_color',
    'field_border_right_color',
    'field_border_top_width',
    'field_border_bottom_width',
    'field_border_left_width',
    'field_border_right_width',
    'field_border_style',
    'field_radius_type',
    'field_radius_size',
    'field_width',
    'field_width_unit',
    'field_padding',
    'field_align',
  ], [], 'optional');

  // Create Boeder box Paragraph.
  _kamihaya_cms_common_create_paragraph('border_box', [
    'field_border_box_item',
    'field_bg_color',
    'field_border_top_color',
    'field_border_bottom_color',
    'field_border_left_color',
    'field_border_right_color',
    'field_border_top_width',
    'field_border_bottom_width',
    'field_border_left_width',
    'field_border_right_width',
    'field_border_style',
    'field_radius_type',
    'field_radius_size',
    'field_width',
    'field_width_unit',
    'field_padding',
    'field_align',
  ],
  [
    'accordion_item' => 'field_accordion_body',
    'border_box_item' => 'field_border_box_body',
    'drawer' => 'field_drawer_body',
    'modal_dialog' => 'field_dialog_body',
    'two_col_table_row' => 'field_row_data',
  ], 'optional');
}

/**
 * Add align field to some paragraphs.
 */
function kamihaya_cms_paragraph_type_update_10009() {
  // Add fields to Divider Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'divider', [
    'field_align',
  ]);

  // Add fields to Image Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'image', [
    'field_align',
  ]);

  // Add fields to Image link Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'image_link', [
    'field_align',
  ]);

  // Add fields to Link button Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'link_button', [
    'field_align',
  ]);

  // Add fields to File download Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'file_download', [
    'field_align',
  ]);

  // Add fields to Two col table Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'two_col_table', [
    'field_align',
  ]);

  // Add fields to Video Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'video', [
    'field_align',
  ]);
}

/**
 * Add fields to Image link Paragraph.
 */
function kamihaya_cms_paragraph_type_update_10010() {
    // Add fields to Image link Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'image_link', [
    'field_link_text',
    'field_link_text_halign',
    'field_link_text_valign',
    'field_image_desc_text_align',
  ]);
}

/**
 * Change field settings of Link button Paragraph and add fields to some paragraphs.
 */
function kamihaya_cms_paragraph_type_update_10011() {
  $field_storage = FieldStorageConfig::loadByName('paragraph', 'field_button_link');
  if ($field_storage) {
    $field_storage->set('cardinality', -1);
    $field_storage->save();
  }
  $field_config = FieldConfig::loadByName('paragraph', 'link_button', 'field_button_link');
  if ($field_config) {
    $settings = $field_config->get('settings');
    $settings['title'] = 1;
    $field_config->set('settings', $settings);
    $field_config->save();
  }
  $field_config = FieldConfig::loadByName('paragraph', 'link_button', 'field_button_text');
  if ($field_config) {
    $field_config->set('required', false);
    $field_config->save();
  }

  // Add fields to Modal dialog Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'modal_dialog', [
    'field_cookie_name',
    'field_default_open',
    'field_one_time',
    'field_modal_width',
    'field_width_unit',
  ], 'optional');

  // Add fields to Headings Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'headings', [
    'field_align',
  ]);

  // Add fields to Video Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'video', [
    'field_width',
    'field_width_unit',
  ]);
}

/**
 * Create Anchor link paragraph and add fields to some paragraphs.
 */
function kamihaya_cms_paragraph_type_update_10012() {

  // Create Anchor link Paragraph.
  _kamihaya_cms_common_create_paragraph('anchor_link', [
    'field_link_item',
  ]);

    // Add fields to Headings Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'headings', [
    'field_anchor_link_id',
  ]);

  // Add fields to H2 title Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'h2_title', [
    'field_anchor_link_id',
  ]);

  // Add fields to H3 title Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'h3_title', [
    'field_anchor_link_id',
  ]);

  // Add fields to H4 title Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'h4_title', [
    'field_anchor_link_id',
  ]);
}

/**
 * Create background image paragraph and add fields to some paragraphs.
 */
function kamihaya_cms_paragraph_type_update_10013() {
  // Create Background Image Paragraph.
  _kamihaya_cms_common_create_paragraph('background_image', [
    'field_body',
    'field_background_image',
    'field_background_mobile_image',
    'field_background_position',
    'field_background_repeat',
    'field_background_size',
    'field_component_id',
    'field_css_class',
    'field_vertical_margin',
    'field_width',
    'field_width_unit',
    'field_align',
  ],
  [
    'accordion_item' => 'field_accordion_body',
    'border_box_item' => 'field_border_box_body',
    'drawer' => 'field_drawer_body',
    'modal_dialog' => 'field_dialog_body',
    'two_col_table_row' => 'field_row_data',
  ], 'optional');

  // Add fields to Border box Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'border_box', [
    'field_component_id',
    'field_css_class',
    'field_vertical_margin',
  ], 'optional');

  // Add fields to Image Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'image', ['field_mobile_image']);

  // Add fields to Image link Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'image_link', [
    'field_font_bold',
    'field_image_desc_color',
    'field_image_desc_position',
    'field_link_text_sp_halign',
    'field_mobile_image',
    'field_padding',
    'field_text_bg_color',
    'field_text_bg_padding_x',
    'field_text_bg_padding_y',
    'field_text_bg_width',
    'field_text_bg_width_unit',
  ]);

  // Add fields to Headings Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'headings', ['field_text_size']);
  // Change field display of Headings Paragraph.
  _kamihaya_cms_common_set_field_display('kamihaya_cms_paragraph_type', 'paragraph', 'headings', 'field_color');
  // Change field display of Border box item Paragraph.
  _kamihaya_cms_common_set_field_display('kamihaya_cms_paragraph_type', 'paragraph', 'border_box_item', 'field_border_box_body', 'optional', FALSE);

  // Change the allowed values of Image text vertical align field.
  $field_storage = FieldStorageConfig::loadByName('paragraph', 'field_image_text_valign');
  if (!$field_storage) {
    return;
  }
  $module_path = \Drupal::service('extension.path.resolver')->getPath('module', 'kamihaya_cms_paragraph_type');
  $filename = "$module_path/config/install/field.storage.paragraph.field_image_text_valign.yml";
  $file = file_get_contents($filename);
  $storage_config = !empty($file) ? Yaml::decode($file) : [];
  $allowed_values = [];
  foreach($storage_config['settings']['allowed_values'] as $value) {
    $allowed_values[$value['value']] = $value['label'];
  }
  $field_storage->setSetting('allowed_values', $allowed_values);
  $field_storage->save();
}

/**
 * Add fields to Border box Paragraph.
 */
function kamihaya_cms_paragraph_type_update_10014() {
  // Add fields to Border box Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'border_box', [
    'field_bg_color_gd_end',
    'field_gradation_direction',
  ], 'optional');

}

/**
 * Add fields to two col table paragraph and its row paragraph.
 */
function kamihaya_cms_paragraph_type_update_10015() {
  // Add fields to Two col table Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'two_col_table', [
    'field_table_class',
  ], 'optional');

  // Add fields to Two col table row Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'two_col_table_row', [
    'field_row_class',
    'field_row_label_class',
    'field_row_data_class',
  ], 'optional');
}

/**
 * Change the image style of image related paragraphs.
 */
function kamihaya_cms_paragraph_type_update_10016() {
  $image_style = \Drupal::entityTypeManager()->getStorage('image_style')->load('convert_to_webp');
  if (!$image_style) {
    return;
  }

  $paragraph_types = [
    'image' => [
      'field_image',
      'field_mobile_image',
    ],
    'image_link' => [
      'field_image',
      'field_mobile_image',
    ],
    'left_image_text' => [
      'field_image',
    ],
    'right_image_text' => [
      'field_image',
    ],
    'background_image' => [
      'field_background_image',
      'field_background_mobile_image',
    ],
  ];

  foreach ($paragraph_types as $paragraph => $fields) {
    foreach ($fields as $field_name) {
      _kamihaya_cms_common_set_field_display(
        'kamihaya_cms_paragraph_type',
        'paragraph',
        $paragraph,
        $field_name,
        $paragraph === 'background_image' ? 'optional' : 'install',
        FALSE,
      );
    }
  }
}

/**
 * Add YouTube fields to Video Paragraph.
 */
function kamihaya_cms_paragraph_type_update_10017() {
  // Add YouTube fields to Video Paragraph.
  _kamihaya_cms_common_add_fields('kamihaya_cms_paragraph_type', 'paragraph', 'video', [
    'field_youtube',
  ]);

}

/**
 * Implements hook_update_dependencies().
 */
function kamihaya_cms_paragraph_type_update_dependencies() {
  $dependencies['kamihaya_cms_paragraph_type'] = [
    10016 => [
      'kamihaya_cms_media_type' => 10005,
    ],
  ];
  return $dependencies;
}
