<?php

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

use Planaday\Config\Internal\InternalSettings;
use Planaday\Entities\Course\CourseDatabase;
use Planaday\Entities\DayPart\DayPartDatabase;
use Planaday\Entities\Location\LocationDatabase;
use Planaday\Html\Tags\Tag;
use Planaday\Utility\DateAndTimeUtility;
use Planaday\Utility\Debug\Log\Logging;
use Planaday\Utility\DependencyInjection\Container;
use Planaday\Utility\DependencyInjection\InjectionImpossibleException;
use Planaday\Utility\PluginData;

class Welcome extends Tag {
	use Logging;
	const TAG = 'admin-page-welcome';
	const DEFAULT_TEMPLATE = 'welcome';

	protected PluginData $pluginData;
	protected DateAndTimeUtility $dateUtil;
	protected InternalSettings $internalData;
	protected CourseDatabase $courseDB;
	protected DayPartDatabase $dayPartDB;
	protected LocationDatabase $locationDB;

	/**
	 * @param Container $registry
	 *
	 * @throws InjectionImpossibleException
	 */
	public function __construct(Container $registry) {
		parent::__construct(__DIR__, self::DEFAULT_TEMPLATE, $registry);

		$this->pluginData = $registry->getInstance(PluginData::class);
		$this->dateUtil = $registry->getInstance(DateAndTimeUtility::class);
		$this->internalData = $registry->getInstance(InternalSettings::class);
		$this->courseDB = $registry->getInstance(CourseDatabase::class);
		$this->dayPartDB = $registry->getInstance(DayPartDatabase::class);
		$this->locationDB = $registry->getInstance(LocationDatabase::class);

		$this->logInfo('Welcome page tag created');
	}

	public function buildTag(): string {
		$this->logInfo('Welcome page tag building');
		$parser = $this->getNewParser();
		$lastUpdateTime = $this->internalData->getLastRefreshCourse();

		$parser->setData('version', implode('.', $this->pluginData->getPluginVersion()));
		$parser->setData('lastApiUpdate', $this->dateUtil->dateTimeAt($lastUpdateTime));
		$parser->setData('lastUpdateAge', $this->dateUtil->getTimeDifferenceAsString(time(), $lastUpdateTime));

		$parser->setData('courseCount', $this->courseDB->getCountAll());
		$parser->setData('dayCount', $this->dayPartDB->getCountAll());
		$parser->setData('locationCount', $this->locationDB->getCountAll());

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