<?php

namespace Planaday\Html\NodeTags\Course;

use DOMNode;
use Planaday\Config\Course\General\GeneralSettings;
use Planaday\Entities\Course\CourseUtility;
use Planaday\Entities\Course\DTO\Course;
use Planaday\Html\Nodes\ParserNode;
use Planaday\Html\NodeTags\Tag;
use Planaday\Html\NodeTags\TagCore;
use Planaday\Html\NodeTags\TagNotFullyInitializedException;
use Planaday\Utility\DependencyInjection\Attributes\Transient;

#[Transient]
class DayPartAmount implements Tag {
	use TagCore;

	public function __construct(
		protected CourseUtility $courseUtility,
		protected GeneralSettings $settings
	) {
		$this->config = [
			"includeELearning" => function () { return $this->settings->isIncludeELearningDayParts();},
		];
	}

	public function getNewNode(): DOMNode&ParserNode {
		if ( ! isset( $this->originalNode ) ) {
			throw new TagNotFullyInitializedException( "The OriginalNode is not set" );
		}

		$outputFragment = $this->originalNode->getUsableDocumentFragment();

		$course = $this->attributes["course"] ?? $this->nodeData->data["course"] ?? null;

		if ( ! ( $course instanceof Course ) ) {
			throw new TagNotFullyInitializedException( "The course is not set" );
		}

		$outputText = $this->courseUtility->getDayPartCount(
			$course,
			$this->attributes["include-e-learning"] ?? $this->settings->isIncludeELearningDayParts()
		);

		$textNode = $outputFragment->ownerDocument->createTextNode( (string) $outputText );

		$outputFragment->prepend( $textNode );

		return $outputFragment;
	}

	public function getTagDir(): string {
		return __DIR__;
	}
}