<?php

/**
 * @file
 * Theme settings form for disign_theme.
 */

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_system_theme_settings_alter().
 */
function disign_theme_form_system_theme_settings_alter(array &$form, FormStateInterface $form_state): void {
  /** @var \Drupal\Core\Extension\ThemeSettingsProvider $theme_settings */
  $theme_settings = \Drupal::service(\Drupal\Core\Extension\ThemeSettingsProvider::class);

  $form['#validate'][] = 'disign_theme_theme_settings_validate';
  $form['#submit'][] = 'disign_theme_theme_settings_submit';

  $form['logo']['show_on_slides'] = [
    '#type' => 'checkbox',
    '#title' => t('Show logo on all slides'),
    '#default_value' => $theme_settings->getSetting('logo.show_on_slides') ?? FALSE,
    '#parents' => ['show_on_slides'],
    '#weight' => 10,
  ];
}

/**
 * Validation handler for disign_theme theme settings.
 */
function disign_theme_theme_settings_validate(array &$form, FormStateInterface $form_state): void {
  $values = $form_state->getValues();
  if (!array_key_exists('show_on_slides', $values)) {
    return;
  }

  $form_state->set('disign_theme_show_on_slides', (bool) $values['show_on_slides']);
  $form_state->unsetValue('show_on_slides');
}

/**
 * Submit handler for disign_theme theme settings.
 */
function disign_theme_theme_settings_submit(array &$form, FormStateInterface $form_state): void {
  $config_key = $form_state->getValue('config_key');
  if (!$config_key) {
    return;
  }

  $show_on_slides = $form_state->get('disign_theme_show_on_slides');
  if ($show_on_slides === NULL) {
    return;
  }

  $config = \Drupal::configFactory()->getEditable($config_key);
  $config
    ->set('logo.show_on_slides', (bool) $show_on_slides)
    ->save();
}
