<?php

namespace Planaday\Config\Course\Calendar;

use Planaday\Config\BasicEnumMappedSetting;
use Planaday\Config\BasicSettingsLog;
use Planaday\Config\EnumMappedSettings;
use Planaday\Config\FormGroupable;
use Planaday\Config\SettingsLoader;
use Planaday\Config\SettingsWithForm;
use Planaday\Config\SettingsWithUsageLog;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Utility\DependencyInjection\Attributes\Scoped;
use Planaday\Utility\FormBuilder\DataTypes\IntType;
use Planaday\Utility\FormBuilder\InputFields\BoolInput;
use Planaday\Utility\FormBuilder\InputFields\RadioInput;
use Planaday\Utility\FormBuilder\InputFields\StringInput;
use Planaday\Utility\FormBuilder\LayoutItems\Text;
use Planaday\Utility\FormBuilder\Structures\BasicGroup;
use Planaday\Utility\FormBuilder\Validators\RegexValidator;
use Planaday\Utility\RegexUtility;

#[Scoped]
class CalendarSettings implements EnumMappedSettings, SettingsWithForm, SettingsWithUsageLog {
	use FormGroupable;
	use BasicEnumMappedSetting;
	use BasicSettingsLog;

	protected bool $showMouseoverText = true;
	protected ?string $textColor = null;
	protected ?string $backgroundColor = null;
	protected int $calendarVersion = 1;
	protected bool $showAllDayParts = false;

	public function __construct(
		protected LocalizationController $translator,
		protected SettingsLoader $settingsDatabase
	) {
		$this->settingsDatabase->reloadData( $this, CalendarOptionsEnum::class );
	}

	protected function createFormGroup(): BasicGroup {
		$t = function ( string $string ) {
			return $this->translator->translate( $string );
		};

		$colorValidator = new RegexValidator('hexColor', RegexUtility::HEX_COLOR_REGEX, $t('Dit veld moet een HEX kleur zijn'));

		return ( new BasicGroup() )
			->setIndex( CalendarOptionsEnum::class )
			->addContents(
				new Text($t('Instellingen voor de kalender functie')),
				( new StringInput() )
					->setIndex( CalendarOptionsEnum::TEXT_COLOR->value )
					->setName( $t("Tekst kleur") )
					->setDescription( $t("Tekst kleur van de kalender, vul deze waarde in als HEX. Bijvoorbeeld: #FFFFFF") )
					->setDefaultValue( $this->textColor )
					->setPlaceholder( $t('#FFFFFF') )
					->addSetCallback( function ( $value ) {
						$this->textColor = $value ?? $this->textColor;
					})
					->addValidator( $colorValidator ),
				( new StringInput() )
					->setIndex( CalendarOptionsEnum::BACKGROUND_COLOR->value )
					->setName( $t("Achtergrond kleur") )
					->setDescription( $t("Achtergrond kleur van de kalender, vul deze waarde in als HEX. Bijvoorbeeld: #CCCCCC") )
					->setDefaultValue( $this->backgroundColor )
					->setPlaceholder( $t('#CCCCCC') )
					->addSetCallback( function ( $value ) {
						$this->backgroundColor = $value ?? $this->backgroundColor;
					})
					->addValidator( $colorValidator ),
				( new BoolInput() )
					->setIndex( CalendarOptionsEnum::SHOW_MOUSEOVER_TEXT->value )
					->setName( $t("Toon tekst bij hover") )
					->setDescription( $t("Toon tekst bij hover") )
					->setDefaultValue( $this->showMouseoverText )
					->addSetCallback( function ( $value ) {
						$this->showMouseoverText = $value ?? $this->showMouseoverText;
					}),
				( new BoolInput() )
					->setIndex( CalendarOptionsEnum::SHOW_ALL_DAY_PARTS->value )
					->setName( $t("Alle dagdelen tonen") )
					->setDescription( $t("Alle dagdelen van de cursus tonen ipv alleen het eerste dagdeel?") )
					->setDefaultValue( $this->showAllDayParts )
					->addSetCallback( function ( $value ) {
						$this->showAllDayParts = $value ?? $this->showAllDayParts;
					}),
				( new RadioInput( new IntType() ) )
					->setIndex( CalendarOptionsEnum::CALENDAR_VERSION->value )
					->setName( $t("Kalender versie") )
					->setDescription($t("Welke versie van de kalender moet worden gebruikt?"))
					->setDefaultValue( $this->calendarVersion )
					->addSetCallback( function ( $value ) {
						$this->calendarVersion = $value ?? $this->calendarVersion;
					})
					->setOptions([
						[
							"value" => 0,
							"label" => "v3 (legacy)"
						],
						[
							"value" => 1,
							"label" => "v6"
						]
					])
			);
	}

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values"          => [
				CalendarOptionsEnum::SHOW_MOUSEOVER_TEXT->value => &$this->showMouseoverText,
				CalendarOptionsEnum::TEXT_COLOR->value          => &$this->textColor,
				CalendarOptionsEnum::BACKGROUND_COLOR->value    => &$this->backgroundColor,
				CalendarOptionsEnum::CALENDAR_VERSION->value    => &$this->calendarVersion,
				CalendarOptionsEnum::SHOW_ALL_DAY_PARTS->value  => &$this->showAllDayParts,
			],
			"subGroups"       => [],
			"subGroupAliases" => []
		];
	}

	public function isShowMouseoverText(): bool {
		$this->logUse(CalendarOptionsEnum::SHOW_MOUSEOVER_TEXT);
		return $this->showMouseoverText;
	}

	public function getTextColor(): ?string {
		$this->logUse(CalendarOptionsEnum::TEXT_COLOR);
		if ( $this->textColor === null ) {
			return null;
		}

		if (!str_starts_with($this->textColor, '#')) return '#' . $this->textColor;

		return $this->textColor;
	}

	public function getBackgroundColor(): ?string {
		$this->logUse(CalendarOptionsEnum::BACKGROUND_COLOR);
		if ( $this->backgroundColor === null ) {
			return null;
		}

		if (!str_starts_with($this->backgroundColor, '#')) return '#' . $this->backgroundColor;

		return $this->backgroundColor;
	}

	public function getCalendarVersion(): int {
		$this->logUse(CalendarOptionsEnum::CALENDAR_VERSION);
		return $this->calendarVersion;
	}

	public function isShowAllDayParts(): bool {
		$this->logUse(CalendarOptionsEnum::SHOW_ALL_DAY_PARTS);
		return $this->showAllDayParts;
	}
}