<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media\Form\SettingsTab;

use Drupal\Core\Config\Config;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Builds the audio settings tab for the FFmpeg Media settings form.
 *
 * @internal
 */
final class AudioSettingsBuilder {

  use StringTranslationTrait;

  /**
   * Builds the audio 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['audio'] = [
      '#type' => 'details',
      '#title' => $this->t('Audio'),
      '#description' => $this->t('Audio normalization and format defaults. Sample rate and channels are global fallbacks for all video conversions. Codec and bitrate apply only to audio processors (Audio Normalization, Multi-Audio).'),
      '#group' => 'settings_tabs',
      '#open' => FALSE,
    ];

    $normalizationStates = [
      '#states' => [
        'visible' => [
          ':input[name="enabled_processors[audio_normalize]"]' => ['checked' => TRUE],
        ],
      ],
    ];

    $form['audio']['audio_normalize_mode'] = [
      '#type' => 'select',
      '#title' => $this->t('Normalization mode'),
      '#description' => $this->t('Configure audio normalization for the Audio Normalization processor.'),
      '#options' => [
        'loudnorm' => $this->t('<abbr title="European Broadcasting Union">EBU</abbr> R128 (loudnorm)'),
        'dynaudnorm' => $this->t('Dynamic (dynaudnorm)'),
        'ebur128' => $this->t('EBU R128 meter'),
      ],
      '#default_value' => $config->get('audio.normalize.mode') ?? 'loudnorm',
    ] + $normalizationStates;

    $form['audio']['audio_normalize_target_lufs'] = [
      '#type' => 'number',
      '#title' => $this->t('Target loudness (<abbr title="Loudness Units Full Scale">LUFS</abbr>)'),
      '#description' => $this->t('Target integrated loudness. Typical: -14 for streaming, -23 for broadcast.'),
      '#default_value' => $config->get('audio.normalize.target_lufs') ?? -14.0,
      '#step' => 0.1,
      '#min' => -70,
      '#max' => 0,
    ] + $normalizationStates;

    $form['audio']['audio_normalize_true_peak'] = [
      '#type' => 'number',
      '#title' => $this->t('True peak (<abbr title="Decibels True Peak">dBTP</abbr>)'),
      '#description' => $this->t('Maximum true peak level.'),
      '#default_value' => $config->get('audio.normalize.true_peak') ?? -1.0,
      '#step' => 0.1,
      '#min' => -10,
      '#max' => 0,
    ] + $normalizationStates;

    $form['audio']['audio_normalize_lra'] = [
      '#type' => 'number',
      '#title' => $this->t('Loudness range (<abbr title="Loudness Units">LU</abbr>)'),
      '#description' => $this->t('Target loudness range.'),
      '#default_value' => $config->get('audio.normalize.lra') ?? 11.0,
      '#step' => 0.1,
      '#min' => 1,
      '#max' => 20,
    ] + $normalizationStates;

    $form['audio']['audio_format'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Audio format'),
      '#description' => $this->t('Sample rate and channels serve as global fallback defaults for all video conversions when not overridden by a profile. Codec and bitrate are only used by audio-specific processors.'),
    ];

    $form['audio']['audio_format']['audio_sample_rate'] = [
      '#type' => 'select',
      '#title' => $this->t('Sample rate'),
      '#description' => $this->t('Fallback for all video conversions when not set by a profile.'),
      '#options' => [
        22050 => $this->t('22050 Hz'),
        44100 => $this->t('44100 Hz'),
        48000 => $this->t('48000 Hz'),
        96000 => $this->t('96000 Hz'),
      ],
      '#default_value' => $config->get('audio.format.sample_rate') ?? 48000,
    ];

    $form['audio']['audio_format']['audio_channels'] = [
      '#type' => 'select',
      '#title' => $this->t('Channels'),
      '#description' => $this->t('Fallback for all video conversions when not set by a profile.'),
      '#options' => [
        1 => $this->t('Mono'),
        2 => $this->t('Stereo'),
        6 => $this->t('5.1 Surround'),
      ],
      '#default_value' => $config->get('audio.format.channels') ?? 2,
    ];

    $form['audio']['audio_format']['audio_codec'] = [
      '#type' => 'select',
      '#title' => $this->t('Codec'),
      '#description' => $this->t('Used only by audio processors. Profile outputs define their own audio codec.'),
      '#options' => [
        'aac' => $this->t('<abbr title="Advanced Audio Coding">AAC</abbr>'),
        'opus' => $this->t('Opus'),
        'vorbis' => $this->t('Vorbis'),
        'mp3' => $this->t('MP3'),
        'flac' => $this->t('<abbr title="Free Lossless Audio Codec">FLAC</abbr>'),
      ],
      '#default_value' => $config->get('audio.format.codec') ?? 'libopus',
    ];

    $form['audio']['audio_format']['audio_bitrate'] = [
      '#type' => 'select',
      '#title' => $this->t('Bitrate'),
      '#description' => $this->t('Used only by audio processors. Also used as default for new profile outputs.'),
      '#options' => [
        '64k' => $this->t('64 kbps'),
        '96k' => $this->t('96 kbps'),
        '128k' => $this->t('128 kbps'),
        '192k' => $this->t('192 kbps'),
        '256k' => $this->t('256 kbps'),
        '320k' => $this->t('320 kbps'),
      ],
      '#default_value' => $config->get('audio.format.bitrate') ?? '128k',
    ];
  }

}
