<?php

/**
 * @file
 * Install function hooks for the DropzoneJS entity browser widget module.
 */

use Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget\DropzoneJsEbWidget;
use Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget\MediaEntityDropzoneJsEbWidget;
use Drupal\entity_browser\Entity\EntityBrowser;

/**
 * Update hook dependencies.
 */
function dropzonejs_eb_widget_update_dependencies() {
  if (\Drupal::moduleHandler()->moduleExists('media_entity')) {
    $dependencies['dropzonejs_eb_widget'] = [
      8202 => ['media_entity' => 8201],
    ];
    return $dependencies;
  }
}

/**
 * Update configurations for configuration key renaming.
 */
function dropzonejs_eb_widget_update_8201() {

  $logger = \Drupal::logger('dropzonejs');

  /** @var \Drupal\entity_browser\Entity\EntityBrowser[] $entity_browsers */
  $entity_browsers = EntityBrowser::loadMultiple();

  foreach ($entity_browsers as $entity_browser) {
    /** @var \Drupal\entity_browser\WidgetInterface[] $widgets */
    $widgets = $entity_browser->getWidgets();

    $entity_browser_config = \Drupal::configFactory()->getEditable('entity_browser.browser.' . $entity_browser->id());

    $has_config_changes = FALSE;
    foreach ($widgets as $widget) {
      if ($widget instanceof MediaEntityDropzoneJsEbWidget) {
        $config = $widget->getConfiguration();

        if (!empty($config['settings']['media_entity_bundle']) && empty($config['settings']['media_type'])) {
          $has_config_changes = TRUE;

          $widget_id = $widget->getConfiguration()['uuid'];
          $value = $entity_browser_config->get('widgets.' . $widget_id . '.settings.media_entity_bundle');

          $entity_browser_config->set('widgets.' . $widget_id . '.settings.media_type', $value);
          $entity_browser_config->clear('widgets.' . $widget_id . '.settings.media_entity_bundle');

          $logger->info(sprintf('Configuration changed for widget (%s) of entity browser (%s)', $widget->label(), $entity_browser->label()));
        }
        else {
          $logger->warning(sprintf('Unable to change configuration for widget (%s) of entity browser (%s)', $widget->label(), $entity_browser->label()));
        }
      }
    }

    if ($has_config_changes) {
      $entity_browser_config->save();
    }
  }

}

/**
 * Update configurations for new keys.
 */
function dropzonejs_eb_widget_update_8202() {
  /** @var \Drupal\entity_browser\Entity\EntityBrowser[] $entity_browsers */
  $entity_browsers = EntityBrowser::loadMultiple();

  foreach ($entity_browsers as $entity_browser) {
    /** @var \Drupal\entity_browser\WidgetInterface[] $widgets */
    $widgets = $entity_browser->getWidgets();

    $has_config_changes = FALSE;
    foreach ($widgets as $widget) {
      if ($widget instanceof DropzoneJsEbWidget) {
        $config = $widget->getConfiguration();

        $config['settings']['clientside_resize'] = FALSE;
        $config['settings']['resize_height'] = 0;
        $config['settings']['resize_method'] = 'contain';
        $config['settings']['resize_quality'] = 1.0;
        $config['settings']['resize_width'] = 0;
        $config['settings']['thumbnail_method'] = 'contain';
        $widget->setConfiguration($config);
        $has_config_changes = TRUE;
      }
    }

    if ($has_config_changes) {
      $entity_browser->save();
    }
  }
}
