<?php

namespace Planaday\Config\OptionGroups\Course;

use Planaday\Config\OptionGroups\Course\Calendar\CalendarOptions;
use Planaday\Config\OptionGroups\Course\Detail\DetailOptions;
use Planaday\Config\OptionGroups\Course\General\GeneralOptions;
use Planaday\Config\OptionGroups\Course\Overview\OverviewOptions;
use Planaday\Config\OptionGroups\Course\Search\SearchOptions;
use Planaday\Config\OptionGroups\OptionGroup;
use Planaday\Platform\PlatformAbstraction\DataHandler;
use Planaday\Utility\PluginData;

class CourseOptions extends OptionGroup {
	private const NAME = 'Course Options';
	public const INDEX = '-course-options';

	/** @var CalendarOptions */
	private $calenderOptions;

	/** @var DetailOptions */
	private $detailOptions;

	/** @var GeneralOptions */
	private $generalOptions;

	/** @var OverviewOptions */
	private $overviewOptions;

	/** @var SearchOptions */
	private $searchOptions;

	protected $dataHandler;
	protected $pluginData;

	public function __construct( DataHandler $dataHandler, PluginData $pluginData) {
		$this->dataHandler = $dataHandler;
		$this->pluginData = $pluginData;

		$this->reloadData();

		$layout = [
			$this->generalOptions,
			$this->overviewOptions,
			$this->detailOptions,
			$this->calenderOptions,
			$this->searchOptions
		];

		parent::__construct( self::INDEX, self::NAME, '', $layout );
	}

	public function reloadData(): void {
		$data = $this->dataHandler->getOption( self::INDEX );

		$this->calenderOptions = new CalendarOptions(
			CourseOptionsEnum::CALENDAR_SETTINGS,
			$data[ CourseOptionsEnum::CALENDAR_SETTINGS ] ?? []
		);

		$this->detailOptions = new DetailOptions(
			CourseOptionsEnum::DETAIL_SETTINGS,
			$data[CourseOptionsEnum::DETAIL_SETTINGS] ?? [],
			$this->pluginData
		);

		$this->generalOptions = new GeneralOptions(
			CourseOptionsEnum::GENERAL_SETTINGS,
			$data[ CourseOptionsEnum::GENERAL_SETTINGS ] ?? []
		);

		$this->overviewOptions = new OverviewOptions(
			CourseOptionsEnum::OVERVIEW_SETTINGS,
			$data[ CourseOptionsEnum::OVERVIEW_SETTINGS ] ?? []
		);

		$this->searchOptions = new SearchOptions(
			CourseOptionsEnum::SEARCH_SETTINGS,
			$data[CourseOptionsEnum::SEARCH_SETTINGS] ?? []
		);
	}

	public function calenderOptions(): CalendarOptions {
		return $this->calenderOptions;
	}

	public function detailOptions(): DetailOptions {
		return $this->detailOptions;
	}

	public function generalOptions(): GeneralOptions {
		return $this->generalOptions;
	}

	public function overviewOptions(): OverviewOptions {
		return $this->overviewOptions;
	}

	public function searchOptions(): SearchOptions {
		return $this->searchOptions;
	}
}