<?php

namespace Planaday\Html\Tags\Admin\Pages\Database;

use Planaday\Config\Internal\InternalSettings;
use Planaday\Entities\Booking\BookingDatabase;
use Planaday\Entities\Course\CourseDatabase;
use Planaday\Entities\DatabaseRefresher;
use Planaday\Entities\DayPart\DayPartDatabase;
use Planaday\Entities\Location\LocationDatabase;
use Planaday\Entities\PostEntities;
use Planaday\Html\Tags\Tag;
use Planaday\Html\Tags\Utility\ArrayDisplay\ArrayDisplay;
use Planaday\Utility\DateAndTimeUtility;
use Planaday\Utility\DependencyInjection\Container;
use Planaday\Utility\DependencyInjection\InjectionImpossibleException;

class Database extends Tag {
	const TAG = 'admin-page-database';
	const DEFAULT_TEMPLATE = 'database';

	protected CourseDatabase $courseDatabase;
	protected DayPartDatabase $dayPartDatabase;
	protected LocationDatabase $locationDatabase;
	protected BookingDatabase $bookingDatabase;
	protected DatabaseRefresher $databaseRefresher;
	protected PostEntities $postEntities;
	protected DateAndTimeUtility $dateUtil;
	protected InternalSettings $internalData;

	/**
	 * @param Container $registry
	 *
	 * @throws InjectionImpossibleException
	 */
	public function __construct(Container $registry) {
		$this->courseDatabase = $registry->getInstance(CourseDatabase::class);
		$this->dayPartDatabase = $registry->getInstance(DayPartDatabase::class);
		$this->locationDatabase = $registry->getInstance(LocationDatabase::class);
		$this->bookingDatabase = $registry->getInstance(BookingDatabase::class);
		$this->databaseRefresher = $registry->getInstance(DatabaseRefresher::class);
		$this->postEntities = $registry->getInstance(PostEntities::class);
		$this->dateUtil = $registry->getInstance(DateAndTimeUtility::class);
		$this->internalData = $registry->getInstance(InternalSettings::class);

		parent::__construct(__DIR__, self::DEFAULT_TEMPLATE, $registry);
	}

	public function buildTag(): string {
		$parser = $this->getNewParser();

		if (in_array('reload databases', $this->postEntities->getActions())){
			$this->databaseRefresher->refreshDatabases();
			$parser->setCondition('success-block', true);
			$parser->setData('successMessage', 'Database succesvol herladen.');
		}

		$parser->addContext(ArrayDisplay::ARRAY_DISPLAY_CONTEXT,
			[
				'course' => $this->courseDatabase->getAllRaw(),
				'dayPart' => $this->dayPartDatabase->getAllRaw(),
				'location' => $this->locationDatabase->getAllRaw(),
			]
		);

		$lastUpdateTime = $this->internalData->getLastRefreshCourse();

		$parser->setData('courseCount', $this->courseDatabase->getCountAll());
		$parser->setData('dayPartCount', $this->dayPartDatabase->getCountAll());
		$parser->setData('locationCount', $this->locationDatabase->getCountAll());
		$parser->setData('bookingCount', $this->bookingDatabase->getCountAll());
		$parser->setData('lastUpdate', $this->dateUtil->dateTimeAt($lastUpdateTime));
		$parser->setData('lastUpdateAge', $this->dateUtil->getTimeDifferenceAsString(time(), $lastUpdateTime));

		return $parser->parse($this->getTemplate());
	}
}