<?php

namespace Planaday\Html\Tags\Course\Id;

use Planaday\Html\Tags\Tag;
use Planaday\Platform\PlatformAbstraction\UrlHandler;
use Planaday\Utility\DependencyInjection\Container;
use Planaday\Utility\DependencyInjection\InjectionImpossibleException;
use Planaday\Utility\GeneralTermEnum;
use Planaday\Utility\PluginData;

class CourseId extends Tag {
	const TAG = 'legacy-course-id';

	const DEFAULT_TEMPLATE = 'courseId';

	protected UrlHandler $urlHandler;
	protected PluginData $pluginData;

	/**
	 * @param Container $registry
	 *
	 * @throws InjectionImpossibleException
	 */
	public function __construct( Container $registry ) {
		$this->urlHandler = $registry->getInstance(UrlHandler::class);
		$this->pluginData = $registry->getInstance(PluginData::class);
		parent::__construct( __DIR__, self::DEFAULT_TEMPLATE, $registry );
	}

	public function buildTag(): string {
		$parser = $this->getNewParser();
		$courseId = $this->urlHandler->getUrlQueryField($this->pluginData->getShortPrefix() . GeneralTermEnum::COURSE_ID_URL_QUERY);

		if ($courseId === null) {
			return $parser->parse( $this->getErrorTemplate('', 'noIdError'));
		}
		$parser->setData('courseId', $courseId);

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