<?php

namespace Planaday\Config\FeatureToggles;

use Planaday\Config\BasicEnumMappedSetting;
use Planaday\Config\EnumMappedSettings;
use Planaday\Config\GlobalSetting;
use Planaday\Config\HiddenFromNodeTags;
use Planaday\Config\SettingsLoader;
use Planaday\Config\SettingsWithForm;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Utility\DependencyInjection\Attributes\Scoped;
use Planaday\Utility\FormBuilder\InputFields\BoolInput;
use Planaday\Utility\FormBuilder\Structure;
use Planaday\Utility\FormBuilder\Structures\BasicGroup;

#[Scoped]
class FeatureToggleSettings implements EnumMappedSettings, GlobalSetting, SettingsWithForm, HiddenFromNodeTags {
	use BasicEnumMappedSetting;

	protected bool $shortcodeSettings = false;

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

	public function getTitle(): string {
		return $this->translator->translate('Feature Toggles');
	}

	public function getEnumMapping(): array {
		return [
			"values" => [
				FeatureToggleEnum::ShortcodeSettings->value => &$this->shortcodeSettings,
			],
			"subGroups" => [],
			"subGroupAliases" => []
		];
	}

	public static function getIndex(): string {
		return '-feature-toggles';
	}

	public function save(): void {
		$this->settingsDatabase->saveData($this);
	}

	public function getFormGroup(): Structure {
		$t = function ( string $string ) {
			return $this->translator->translate( $string );
		};

		return ( new BasicGroup() )
			->setIndex( FeatureToggleEnum::class )
			->addContents(
				( new BoolInput() )
					->setIndex( FeatureToggleEnum::ShortcodeSettings->value )
					->setName( $t("Shortcodes") )
					->setDescription( $t("Toon de instellingen voor de shortcodes bij de instellingen.") )
					->setDefaultValue( $this->shortcodeSettings )
					->addSetCallback( function ( $value ) {
						$this->shortcodeSettings = $value ?? $this->shortcodeSettings;
					}),
			);
	}

	public function showShortcodeSettings(): bool {
		return $this->shortcodeSettings;
	}
}