<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media\Form\SettingsTab;

use Drupal\Core\Config\Config;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\ffmpeg_media\Enum\HardwareType;
use Drupal\ffmpeg_media\Service\HardwareDetectorInterface;

/**
 * Builds the hardware acceleration tab for the FFmpeg Media settings form.
 *
 * @internal
 */
final class HardwareSettingsBuilder {

  use StringTranslationTrait;

  /**
   * Constructs a HardwareSettingsBuilder.
   *
   * @param \Drupal\ffmpeg_media\Service\HardwareDetectorInterface $hardwareDetector
   *   Hardware detector service.
   */
  public function __construct(
    protected readonly HardwareDetectorInterface $hardwareDetector,
  ) {
  }

  /**
   * Builds the hardware acceleration section render array.
   *
   * @param array<string, mixed> $form
   *   Form structure, passed by reference.
   * @param \Drupal\Core\Config\Config $config
   *   Module configuration.
   */
  public function build(array &$form, Config $config): void {
    $form['hardware'] = [
      '#type' => 'details',
      '#title' => $this->t('Hardware Acceleration'),
      '#description' => $this->t('Use GPU-based encoding for faster video conversions when compatible hardware is available.'),
      '#group' => 'settings_tabs',
      '#open' => FALSE,
    ];

    $hwOptions = [
      'auto' => $this->t('Auto detect'),
      'disabled' => $this->t('Disabled (software only)'),
      HardwareType::Nvenc->value => $this->t('NVIDIA <abbr title="NVIDIA Video Encoder">NVENC</abbr>'),
      HardwareType::Vaapi->value => $this->t('<abbr title="Video Acceleration API">VA-API</abbr> (Intel/AMD)'),
      HardwareType::Qsv->value => $this->t('Intel <abbr title="Quick Sync Video">QSV</abbr>'),
      HardwareType::VideoToolbox->value => $this->t('VideoToolbox (macOS)'),
      HardwareType::Amf->value => $this->t('AMD <abbr title="Advanced Media Framework">AMF</abbr>'),
      HardwareType::Vulkan->value => $this->t('Vulkan'),
    ];

    $form['hardware']['hardware_acceleration'] = [
      '#type' => 'select',
      '#title' => $this->t('Hardware acceleration'),
      '#description' => $this->t('Select which GPU encoder to use. "Auto detect" probes the server for available hardware. "Disabled" forces software encoding (slower but universally compatible).'),
      '#options' => $hwOptions,
      '#default_value' => $config->get('hardware_acceleration'),
    ];

    $form['hardware']['hardware_fallback_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable software fallback'),
      '#description' => $this->t('Automatically retry with software encoding when GPU encoding fails (e.g., driver issues or unsupported format). Recommended to keep enabled.'),
      '#default_value' => $config->get('hardware_fallback_enabled') ?? TRUE,
    ];

    $detectedHw = $this->hardwareDetector->detectAvailableHardware();
    if (!empty($detectedHw)) {
      $hwMarkup = $detectedHw
        |> (fn(array $hw): array => array_map(strtoupper(...), $hw))
        |> (fn(array $hw): string => implode(', ', $hw));
      $form['hardware']['detected_hardware'] = [
        '#type' => 'item',
        '#title' => $this->t('Detected hardware'),
        '#markup' => $hwMarkup,
      ];
    }
  }

}
