<?php

namespace Planaday\Config\OptionGroups\Internal;

use Planaday\Config\OptionGroups\OptionGroup;
use Planaday\Config\OptionTypes\IntOption;
use Planaday\Config\OptionTypes\StringOption;
use Planaday\Platform\PlatformAbstraction\DataHandler;

class InternalOptions extends OptionGroup {
	private const NAME = 'Internal Options';
	public const INDEX = '-internal-options';

	/** @var DataHandler */
	private $dataHandler;

	/** @var IntOption<int> */
	private $lastRefreshLocation;

	/** @var IntOption<int> */
	private $lastRefreshDayPart;

	/** @var IntOption<int> */
	private $lastRefreshCourse;

	/** @var StringOption<string> */
	private $globalCss;

	/** @var StringOption<null> */
	private $lastSelectedApi;

	/** @var IntOption<null> */
	private $startKnownTimeSlot;

	/** @var IntOption<null> */
	private $endKnownTimeSlot;

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

		$this->lastRefreshCourse = new IntOption(
			InternalOptionsEnum::LAST_COURSE_REFRESH,
			'laatste cursus refresh',
			"int van de tijd van de laatste cursus refresh",
			$data[ InternalOptionsEnum::LAST_COURSE_REFRESH ] ?? null,
			0
		);

		$this->lastRefreshDayPart = new IntOption(
			InternalOptionsEnum::LAST_DAY_PART_REFRESH,
			'laatste dagdeel refresh',
			"int van de tijd van de laatste dagdeel refresh",
			$data[ InternalOptionsEnum::LAST_DAY_PART_REFRESH ] ?? null,
			0
		);

		$this->lastRefreshLocation = new IntOption(
			InternalOptionsEnum::LAST_LOCATION_REFRESH,
			'Laatste locatie refresh',
			"int van de tijd van de laatste locatie refresh",
			$data[ InternalOptionsEnum::LAST_LOCATION_REFRESH ] ?? null,
			0
		);

		$this->lastSelectedApi =  new StringOption(
			InternalOptionsEnum::LAST_SELECTED_API,
			'Laatste geselecteerde API',
			'Meest recente API gebruikt voor een database refresh',
			$data[ InternalOptionsEnum::LAST_SELECTED_API ] ?? null,
			null
		);

		$this->globalCss = new StringOption(
			InternalOptionsEnum::GLOBAL_CSS,
			'Global CSS',
			"CSS die op alle pagina's wordt geladen",
			$data[ InternalOptionsEnum::GLOBAL_CSS ] ?? null,
			'/* Voeg hier jouw eigen CSS toe */'
		);

		$this->startKnownTimeSlot = new IntOption(
			InternalOptionsEnum::START_KNOWN_TIME_SLOT,
			'Start bekende cursussen',
			'Start van het tijdvak van bekende cursussen',
			$data[ InternalOptionsEnum::START_KNOWN_TIME_SLOT ] ?? null,
			null
		);

		$this->endKnownTimeSlot = new IntOption(
			InternalOptionsEnum::END_KNOWN_TIME_SLOT,
			'Eind bekende cursussen',
			'Eind van het tijdvak van bekende cursussen',
			$data[ InternalOptionsEnum::END_KNOWN_TIME_SLOT ] ?? null,
			null
		);

		$layout = [
			$this->lastRefreshCourse,
			$this->lastRefreshDayPart,
			$this->lastRefreshLocation
		];

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

	/**
	 * @return IntOption<int>
	 */
	public function lastRefreshLocation(): IntOption {
		return $this->lastRefreshLocation;
	}

	/**
	 * @return IntOption<int>
	 */
	public function lastRefreshDayPart(): IntOption {
		return $this->lastRefreshDayPart;
	}

	/**
	 * @return IntOption<int>
	 */
	public function lastRefreshCourse(): IntOption {
		return $this->lastRefreshCourse;
	}

	public function setLastRefreshLocation( int $lastRefresh ): void {
		$data                                               = $this->dataHandler->getOption( self::INDEX );
		$data[ InternalOptionsEnum::LAST_LOCATION_REFRESH ] = $lastRefresh;

		$this->dataHandler->setOption( self::INDEX, $data );
		$this->lastRefreshLocation->updateValue( $lastRefresh );
	}

	public function setLastRefreshDayPart( int $lastRefresh ): void {
		$data                                               = $this->dataHandler->getOption( self::INDEX );
		$data[ InternalOptionsEnum::LAST_DAY_PART_REFRESH ] = $lastRefresh;

		$this->dataHandler->setOption( self::INDEX, $data );
		$this->lastRefreshDayPart->updateValue( $lastRefresh );
	}

	public function setLastRefreshCourse( int $lastRefresh ): void {
		$data                                             = $this->dataHandler->getOption( self::INDEX );
		$data[ InternalOptionsEnum::LAST_COURSE_REFRESH ] = $lastRefresh;

		$this->dataHandler->setOption( self::INDEX, $data );
		$this->lastRefreshCourse->updateValue( $lastRefresh );
	}

	/**
	 * @return StringOption<null>
	 */
	public function lastSelectedApi(): StringOption {
		return $this->lastSelectedApi;
	}

	public function setLastSelectedApi( ?string $api ): void {
		$data = $this->dataHandler->getOption( self::INDEX );
		$data[ InternalOptionsEnum::LAST_SELECTED_API ] = $api;

		$this->dataHandler->setOption( self::INDEX, $data );
		$this->lastSelectedApi->updateValue( $api );
	}

	public function globalCss(): StringOption {
		return $this->globalCss;
	}

	public function setGlobalCss( ?string $css ): void {
		$data = $this->dataHandler->getOption( self::INDEX );
		$data[ InternalOptionsEnum::GLOBAL_CSS ] = $css;

		$this->dataHandler->setOption( self::INDEX, $data );
		$this->globalCss->updateValue( $css );
	}

	public function startKnownTimeSlot(): IntOption {
		return $this->startKnownTimeSlot;
	}

	public function setStartKnownTimeSlot( ?int $startKnownTimeSlot ): void {
		$data = $this->dataHandler->getOption( self::INDEX );
		$data[ InternalOptionsEnum::START_KNOWN_TIME_SLOT ] = $startKnownTimeSlot;

		$this->dataHandler->setOption( self::INDEX, $data );
		$this->startKnownTimeSlot->updateValue( $startKnownTimeSlot );
	}

	public function endKnownTimeSlot(): IntOption {
		return $this->endKnownTimeSlot;
	}

	public function setEndKnownTimeSlot( ?int $endKnownTimeSlot ): void {
		$data = $this->dataHandler->getOption( self::INDEX );
		$data[ InternalOptionsEnum::END_KNOWN_TIME_SLOT ] = $endKnownTimeSlot;

		$this->dataHandler->setOption( self::INDEX, $data );
		$this->endKnownTimeSlot->updateValue( $endKnownTimeSlot );
	}
}