<?php

declare(strict_types=1);

namespace Drupal\acquia_dam\Entity;

use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Provides storage and field definitions for the DAM media source plugin.
 */
final class ManagedFileField {

  /**
   * The field name.
   */
  public const MANAGED_FILE_FIELD_NAME = 'acquia_dam_managed_file';

  /**
   * Get the field storage definition.
   *
   * @param string $entity_type_id
   *   The entity type ID.
   *
   * @return \Drupal\acquia_dam\Entity\BundleFieldDefinition
   *   The storage definition.
   */
  public static function getStorageDefinition(string $entity_type_id): BundleFieldDefinition {
    return self::bundleFieldDefinition()->setTargetEntityTypeId($entity_type_id);
  }

  /**
   * Get the field definition.
   *
   * @param string $entity_type_id
   *   The entity type ID.
   * @param string $bundle
   *   The entity bundle.
   * @param string $bundle_label
   *   The bundle label. Deprecated in Acquia Dam 1.1.
   * @param string $file_extensions
   *   Supported file extensions.
   *
   * @return \Drupal\acquia_dam\Entity\BundleFieldDefinition
   *   The field definition.
   */
  public static function getFieldDefinition(string $entity_type_id, string $bundle, string $bundle_label = '', string $file_extensions = ''): BundleFieldDefinition {
    if ($bundle_label !== '') {
      @trigger_error('Passing a bundle label to ManagedFileField::getFieldDefinition() is deprecated in acquia_dam:1.1.0 and is removed from acquia_dam:1.2.0. This parameter never set the label, there is no replacement.', E_USER_DEPRECATED);
    }

    // Get uri_scheme from media type source.
    $media_type = \Drupal::entityTypeManager()->getStorage('media_type')->load($bundle);
    $uri_scheme = $media_type->getSource()->getUriScheme();
    $file_extensions = $media_type->getSource()->getFileExtensions();

    $bundle_definitions = self::bundleFieldDefinition($uri_scheme, $file_extensions)
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayConfigurable('form', TRUE)
      ->setTargetEntityTypeId($entity_type_id)
      ->setTargetBundle($bundle);

    if ($file_extensions !== '') {
      $bundle_definitions->setSetting('file_extensions', $file_extensions);
    }

    return $bundle_definitions;
  }

  /**
   * Get the bundle field definition.
   *
   * @param string $uri_scheme
   *   The URI scheme for file storage (public or private).
   * @param string $file_extensions
   *   The allowed file extensions, separated by a comma or space.
   *
   * @return \Drupal\acquia_dam\Entity\BundleFieldDefinition
   *   The bundle field definition.
   */
  private static function bundleFieldDefinition(string $uri_scheme = '', string $file_extensions = ''): BundleFieldDefinition {
    $bundle_field_definition = BundleFieldDefinition::create('file')
      ->setSetting('target_type', 'file')
      ->setSetting('uri_scheme', $uri_scheme)
      ->setProvider('acquia_dam')
      ->setName(self::MANAGED_FILE_FIELD_NAME)
      ->setDescription(t('For keeping track of locally stored files of DAM assets.'))
      ->setLabel(new TranslatableMarkup('On-site stored asset file'))
      ->setReadOnly(FALSE);

    if (!empty($file_extensions)) {
      $bundle_field_definition->setSetting('file_extensions', $file_extensions);
    }

    return $bundle_field_definition;
  }

}
