<?php

namespace Drupal\dfg_3dviewer\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\dfg_3dviewer\Service\Dfg3dViewerConfigApplier;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 *
 */
class DFG3dViewerConfigForm extends FormBase {

  public function __construct(
    protected Dfg3dViewerConfigApplier $configApplier,
  ) {}

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('dfg_3dviewer.config_applier'),
    );
  }

  /**
   *
   */
  public function getFormId() {

    return 'dfg_3dviewer_settings_form';
  }

  /**
   *
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
	
    $default_config = \Drupal::config('dfg_3dviewer.settings');
    $default_settings = [
      'entity_bundle' => $default_config->get('dfg_3dviewer_entitybundle'),
      'viewer_file_upload' => $default_config->get('dfg_3dviewer_viewer_file_upload'),
      'image_generation' => $default_config->get('dfg_3dviewer_image_generation'),
      'viewer_file_name' => $default_config->get('dfg_3dviewer_viewer_file_name'),
      'api_3d_file_field' => $default_config->get('dfg_3dviewer_api_3d_file_field') ?? $default_config->get('api_3d_file_field'),
      'field_df' => $default_config->get('dfg_3dviewer_field_df'),
      'main_url' => $default_config->get('dfg_3dviewer_main_url') ?? $default_config->get('main_url'),
      'metadata_url' => $default_config->get('dfg_3dviewer_metadata_url') ?? $default_config->get('metadata_url'),
      'json_export_base_url' => $default_config->get('dfg_3dviewer_json_export_base_url') ?? $default_config->get('json_export_base_url'),
      'basenamespace' => $default_config->get('dfg_3dviewer_basenamespace'),
      'container' => $default_config->get('dfg_3dviewer_container'),
      'lightweight' => $default_config->get('dfg_3dviewer_lightweight'),
      'scale_container_x' => $default_config->get('dfg_3dviewer_scale_container_x'),
      'scale_container_y' => $default_config->get('dfg_3dviewer_scale_container_y'),
      'gallery_container' => $default_config->get('dfg_3dviewer_gallery_container'),
      'gallery_image_class' => $default_config->get('dfg_3dviewer_gallery_image_class'),
      'gallery_image_id' => $default_config->get('dfg_3dviewer_gallery_image_id'),
      'base_module_path' => $default_config->get('dfg_3dviewer_base_module_path') ?: '/libraries/dfg-3dviewer/assets',
      'entity_id_uri' => $default_config->get('dfg_3dviewer_entity_id_uri'),
      'view_entity_path' => $default_config->get('dfg_3dviewer_view_entity_path'),
      'attribute_id' => $default_config->get('dfg_3dviewer_attribute_id'),
	  'export_viewer' => $default_config->get('dfg_3dviewer_export_viewer'),
	  'export_viewer_url' => $default_config->get('dfg_3dviewer_export_viewer_url'),
    ];

	$form['#attached']['library'][] = 'dfg_3dviewer/dfg_3dviewer.config_form';

	$full_mode_states = [
		'visible' => [
			':input[name="dfg_3dviewer_lightweight"]' => ['checked' => FALSE],
		],
	];

	$form['dfg_3dviewer_lightweight'] = [
		'#default_value' => $default_settings['lightweight'],
		'#type' => 'checkbox',
		'#title' => $this->t('<b>Lightweight</b> version. If checked, 3D Viewer will provide only basic operations.'),
		'#required' => false,
    ];

	$form['dfg_3dviewer_main_url'] = [
		'#default_value' => $default_settings['main_url'],
		'#type' => 'textfield',
		'#title' => $this->t('Main URL'),
		'#required' => true,
		'#description' => 'Change <b>main URL</b> for used repository',
    ];

	$form['dfg_3dviewer_basenamespace'] = [
		'#default_value' => $default_settings['basenamespace'],
		'#type' => 'textfield',
		'#title' => $this->t('Default base namespace'),
		'#description' => $this->t('(if different than Main URL)'),
    ];

	$form['dfg_3dviewer_metadata_url'] = [
		'#default_value' => $default_settings['metadata_url'],
		'#type' => 'textfield',
		'#title' => $this->t('Metadata URL'),
		'#description' => '<b>URL</b> of the instance that serves metadata content',
		'#states' => $full_mode_states,
    ];

	$form['dfg_3dviewer_json_export_base_url'] = [
		'#default_value' => $default_settings['json_export_base_url'],
		'#type' => 'textfield',
		'#title' => $this->t('JSON Export Base URL'),
		'#description' => '<b>Base URL</b> of the instance that serves JSON export, e.g. https://repository.covher.eu',
		'#states' => $full_mode_states,
    ];

	$form['dfg_3dviewer_container'] = [
		'#default_value' => $default_settings['container'],
		'#type' => 'textfield',
		'#title' => $this->t('Container ID'),
		'#required' => true,
		'#description' => '<b>ID</b> of the main container for the Viewer',
    ];

	$form['dfg_3dviewer_entitybundle'] = [
		'#default_value' => $default_settings['entity_bundle'],
		'#type' => 'textfield',
		'#title' => $this->t('Entity Bundle ID'),
		'#required' => true,
		'#description' => '<b>ID</b> of the bundle for the entity given in wisski pathbuilder for 3d_model field',
    ];

	$form['dfg_3dviewer_viewer_file_upload'] = [
		'#default_value' => $default_settings['viewer_file_upload'],
		'#type' => 'textfield',
		'#title' => $this->t('Viewer File Upload ID'),
		'#required' => true,
		'#description' => '<b>ID</b> of the bundle for the entity given in wisski pathbuilder for 3d_upload field',
    ];

	$form['dfg_3dviewer_viewer_file_name'] = [
		'#default_value' => $default_settings['viewer_file_name'],
		'#type' => 'textfield',
		'#title' => $this->t('Viewer File Name ID'),
		'#required' => true,
		'#description' => '<b>ID</b> of the bundle for the entity given in wisski pathbuilder for viewer_file_name field',
    ];

	$form['dfg_3dviewer_api_3d_file_field'] = [
		'#default_value' => $default_settings['api_3d_file_field'],
		'#type' => 'textfield',
		'#title' => $this->t('API 3D File Field'),
		'#required' => false,
		'#description' => '<b>ID or machine name</b> of the field that should populate API `3D_file`',
		'#states' => $full_mode_states,
    ];

	$form['dfg_3dviewer_image_generation'] = [
		'#default_value' => $default_settings['image_generation'],
		'#type' => 'textfield',
		'#title' => $this->t('Image Generation'),
		'#description' => '<b>ID</b> of the bundle for the entity given in wisski pathbuilder for image_generation field',
		'#states' => $full_mode_states,
    ];

	$form['dfg_3dviewer_field_df'] = [
		'#default_value' => $default_settings['field_df'],
		'#type' => 'textfield',
		'#title' => $this->t('Field DF'),
		'#description' => 'Name of the field given for <b>field_df</b>',
		'#states' => $full_mode_states,
    ];

	$form['dfg_3dviewer_export_viewer'] = [
		'#default_value' => $default_settings['export_viewer'],
		'#type' => 'textfield',
		'#title' => $this->t('Export Viewer Field'),
		'#description' => 'Name of the field given for <b>export_viewer</b>',
		'#states' => $full_mode_states,
    ];

	$form['dfg_3dviewer_export_viewer_url'] = [
		'#default_value' => $default_settings['export_viewer_url'],
		'#type' => 'textfield',
		'#title' => $this->t('Export Viewer URL'),
		'#description' => 'URL for the export viewer',
		'#states' => $full_mode_states,
    ];

	$form['scale_wrapper'] = [
		'#type' => 'container',
		'#attributes' => [
			'class' => ['scale-fields-wrapper', 'flex-container'],
		],
	];

	$form['scale_wrapper']['dfg_3dviewer_scale_container_x'] = [
		'#default_value' => $default_settings['scale_container_x'],
		'#type' => 'textfield',
		'#title' => $this->t('Scale container X'),
		'#required' => true,
		'#description' => '<b>Width</b> scale of the container',
		'#attributes' => [
			'class' => ['half-width'],
		]
    ];

	$form['scale_wrapper']['dfg_3dviewer_scale_container_y'] = [
		'#default_value' => $default_settings['scale_container_y'],
		'#type' => 'textfield',
		'#title' => $this->t('Scale container Y'),
		'#required' => true,
		'#description' => '<b>Height</b> scale of the container',
		'#attributes' => [
			'class' => ['half-width'],
		],
    ];

	$form['gallery_wrapper'] = [
		'#type' => 'container',
		'#attributes' => [
			'class' => ['gallery-fields-wrapper', 'gallery-container'],
		],
		'#states' => $full_mode_states,
	];

	$form['gallery_wrapper']['dfg_3dviewer_gallery_container'] = [
		'#default_value' => $default_settings['gallery_container'],
		'#type' => 'textfield',
		'#title' => $this->t('Gallery container element name'),
		'#required' => false,
		'#description' => '<b>Name</b> of the element with gallery URLs',
    ];

	$form['gallery_wrapper']['dfg_3dviewer_gallery_image_class'] = [
		'#default_value' => $default_settings['gallery_image_class'],
		'#type' => 'textfield',
		'#title' => $this->t('Gallery class name for images'),
		'#required' => false,
		'#description' => '<b>Class</b> name for gallery images',
    ];

	$form['gallery_wrapper']['dfg_3dviewer_gallery_image_id'] = [
		'#default_value' => $default_settings['gallery_image_id'],
		'#type' => 'textfield',
		'#title' => $this->t('Gallery ID name for images'),
		'#required' => false,
		'#description' => '<b>ID</b> name for gallery images',
    ];

	$form['dfg_3dviewer_base_module_path'] = [
		'#default_value' => $default_settings['base_module_path'],
		'#type' => 'textfield',
		'#title' => $this->t('Path for the Viewer module'),
		'#required' => true,
		'#description' => 'Real <b>path</b> for the Viewer module',
    ];

	$form['dfg_3dviewer_entity_id_uri'] = [
		'#default_value' => $default_settings['entity_id_uri'],
		'#type' => 'textfield',
		'#title' => $this->t('Regex for entity ID'),
		'#required' => false,
		'#description' => '<b>Regex</b> that allows get ID of the entity',
    ];

	$form['dfg_3dviewer_view_entity_path'] = [
		'#default_value' => $default_settings['view_entity_path'],
		'#type' => 'textfield',
		'#title' => $this->t('Path with navigate content'),
		'#required' => true,
		'#description' => '<b>Path</b> that allows navigate to the entity',
    ];

	$form['dfg_3dviewer_attribute_id'] = [
		'#default_value' => $default_settings['attribute_id'],
		'#type' => 'textfield',
		'#title' => $this->t('Attribute ID with WissKI content'),
		'#required' => true,
		'#description' => '<b>ID</b> that allows get more specific data from WissKI',
    ];

    $form['submit'] = [
		'#type' => 'submit',
		'#value' => $this->t('Submit'),
    ];
    return $form;
  }

	public function validateForm(array &$form, FormStateInterface $form_state) {
		if ($form_state->getValue('dfg_3dviewer_lightweight')) {
			foreach (Dfg3dViewerConfigApplier::LIGHTWEIGHT_OPTIONAL_KEYS as $field) {
				$form_state->setValue($field, '');
			}
		}
	}

  /**
   *
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->configApplier->apply($form_state->getValues());

    $this->messenger()->addStatus($this->t('Changed DFG 3D Viewer settings successfully'));
    $form_state->setRedirect('system.admin_config');
  }

}
