<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\ffmpeg_media\Attribute\FFmpegCodec;
use Drupal\ffmpeg_media\Plugin\FFmpegCodec\FFmpegCodecInterface;

/**
 * Plugin manager for FFmpeg codec plugins.
 *
 * @internal Runtime service, not part of the supported contrib extension API.
 */
class FFmpegCodecPluginManager extends DefaultPluginManager {

  /**
   * Constructs an FFmpegCodecPluginManager object.
   *
   * @param \Traversable $namespaces
   *   An object that implements \Traversable which contains the root paths
   *   keyed by the corresponding namespace.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Cache backend instance to use.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler to invoke alter hooks.
   */
  public function __construct(
    \Traversable $namespaces,
    CacheBackendInterface $cache_backend,
    ModuleHandlerInterface $module_handler,
  ) {
    parent::__construct(
      'Plugin/FFmpegCodec',
      $namespaces,
      $module_handler,
      FFmpegCodecInterface::class,
      FFmpegCodec::class,
    );

    $this->alterInfo('ffmpeg_codec_info');
    $this->setCacheBackend($cache_backend, 'ffmpeg_codec_plugins');
  }

  /**
   * Gets all available codec plugins.
   *
   * @return array<\Drupal\ffmpeg_media\Plugin\FFmpegCodec\FFmpegCodecInterface>
   *   Array of available codec instances.
   */
  #[\NoDiscard]
  public function getAvailableCodecs(): array {
    $codecs = [];
    foreach ($this->getDefinitions() as $id => $definition) {
      $codec = $this->createInstance($id);
      if ($codec instanceof FFmpegCodecInterface && $codec->isAvailable()) {
        $codecs[$id] = $codec;
      }
    }
    return $codecs;
  }

  /**
   * Gets codecs that support a specific container format.
   *
   * @param string $container
   *   Container format (e.g., 'mp4', 'webm').
   *
   * @return array<\Drupal\ffmpeg_media\Plugin\FFmpegCodec\FFmpegCodecInterface>
   *   Array of compatible codec instances.
   */
  #[\NoDiscard]
  public function getCodecsForContainer(string $container): array {
    $codecs = [];
    foreach ($this->getAvailableCodecs() as $id => $codec) {
      if (in_array($container, $codec->getSupportedContainers(), TRUE)) {
        $codecs[$id] = $codec;
      }
    }
    return $codecs;
  }

  /**
   * Gets codec options for form select.
   *
   * @param string|null $container
   *   Optional container to filter by.
   *
   * @return array<string, string>
   *   Array of codec labels keyed by ID.
   */
  #[\NoDiscard]
  public function getCodecOptions(?string $container = NULL): array {
    $codecs = $container !== NULL
      ? $this->getCodecsForContainer($container)
      : $this->getAvailableCodecs();

    $options = [];
    foreach ($codecs as $id => $codec) {
      $options[$id] = $codec->getLabel();
    }
    return $options;
  }

}
