<?php

namespace Planaday\Config\Course;

use Planaday\Config\BasicEnumMappedSetting;
use Planaday\Config\BasicSettingsLog;
use Planaday\Config\Course\Calendar\CalendarOptionsEnum;
use Planaday\Config\Course\Calendar\CalendarSettings;
use Planaday\Config\Course\Detail\DetailOptionsEnum;
use Planaday\Config\Course\Detail\DetailSettings;
use Planaday\Config\Course\General\GeneralOptionsEnum;
use Planaday\Config\Course\General\GeneralSettings;
use Planaday\Config\Course\Overview\OverviewOptionsEnum;
use Planaday\Config\Course\Overview\OverviewSettings;
use Planaday\Config\Course\Search\SearchOptionsEnum;
use Planaday\Config\Course\Search\SearchSettings;
use Planaday\Config\EnumMappedSettings;
use Planaday\Config\FeatureToggles\FeatureToggleSettings;
use Planaday\Config\FormGroupable;
use Planaday\Config\GlobalSetting;
use Planaday\Config\SettingsLoader;
use Planaday\Config\SettingsWithForm;
use Planaday\Config\SettingsWithUsageLog;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Platform\PlatformAbstraction\UrlHandler;
use Planaday\Utility\DataGrouping\Collection;
use Planaday\Utility\DependencyInjection\Attributes\Scoped;
use Planaday\Utility\FormBuilder\Structures\BasicGroup;
use Planaday\Utility\FormBuilder\Structures\TabbedGroups;

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

	public static function getIndex(): string {
		return '-course-options';
	}

	public function __construct(
		protected LocalizationController $translator,
		protected SettingsLoader $settingsDatabase,
		protected UrlHandler $urlHandler,
		protected CalendarSettings $calendarSettings,
		protected DetailSettings $detailSettings,
		protected GeneralSettings $generalSettings,
		protected OverviewSettings $overviewSettings,
		protected SearchSettings $searchSettings,
		protected FeatureToggleSettings $featureToggleSettings
	) {
		$this->settingsDatabase->reloadData( $this, CourseOptionsEnum::class );
	}

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

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

		$tabs = new TabbedGroups();
		$tabs->addTab(
			$t('Algemeen'),
			$this->generalSettings->getFormGroup()
		);

		if ($this->featureToggleSettings->showShortcodeSettings()) {
			$tabs->addTab(
				$t('Overzicht'),
				$this->overviewSettings->getFormGroup()
			);
		}

		$tabs->addTab(
			$t('Detail'),
			$this->detailSettings->getFormGroup()
		);

		if ($this->featureToggleSettings->showShortcodeSettings()) {
			$tabs->addTab(
				$t( 'Kalender' ),
				$this->calendarSettings->getFormGroup()
			)->addTab(
				$t( 'Zoek' ),
				$this->searchSettings->getFormGroup()
			);
		}

		return ( new BasicGroup() )
			->setIndex( CourseOptionsEnum::class )
			->addContents(
				$tabs
			);
	}

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values"          => [],
			"subGroups"       => [
				CourseOptionsEnum::CALENDAR_SETTINGS->value => $this->calendarSettings->getEnumMapping(),
				CourseOptionsEnum::DETAIL_SETTINGS->value   => $this->detailSettings->getEnumMapping(),
				CourseOptionsEnum::GENERAL_SETTINGS->value  => $this->generalSettings->getEnumMapping(),
				CourseOptionsEnum::OVERVIEW_SETTINGS->value => $this->overviewSettings->getEnumMapping(),
				CourseOptionsEnum::SEARCH_SETTINGS->value   => $this->searchSettings->getEnumMapping(),
			],
			"subGroupAliases" => [
				CalendarOptionsEnum::class => CourseOptionsEnum::CALENDAR_SETTINGS->value,
				DetailOptionsEnum::class   => CourseOptionsEnum::DETAIL_SETTINGS->value,
				GeneralOptionsEnum::class => CourseOptionsEnum::GENERAL_SETTINGS->value,
				OverviewOptionsEnum::class => CourseOptionsEnum::OVERVIEW_SETTINGS->value,
				SearchOptionsEnum::class => CourseOptionsEnum::SEARCH_SETTINGS->value,
			]
		];
	}

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

	public function getCalendarSettings(): CalendarSettings {
		return $this->calendarSettings;
	}

	public function getDetailSettings(): DetailSettings {
		return $this->detailSettings;
	}

	public function getGeneralSettings(): GeneralSettings {
		return $this->generalSettings;
	}

	public function getOverviewSettings(): OverviewSettings {
		return $this->overviewSettings;
	}

	public function getSearchSettings(): SearchSettings {
		return $this->searchSettings;
	}

	public function getUsageLog(): Collection {
		return new Collection(
			...$this->calendarSettings->getUsageLog(),
			...$this->detailSettings->getUsageLog(),
			...$this->generalSettings->getUsageLog(),
			...$this->overviewSettings->getUsageLog(),
			...$this->searchSettings->getUsageLog()
		);
	}
}