<?php

namespace Planaday\Database\InMemoryDatabase;

use ArrayAccess;
use Countable;
use Planaday\Entities\DayPart\DayPartDatabase;
use Planaday\Entities\DayPart\DTO\DayPart;
use Planaday\Utility\DataGrouping\Collection;
use Traversable;

class InMemoryDayPartDB implements DayPartDatabase{
	/** @var Collection<DayPart> */
	public Collection $dayParts;

	public function __construct() {
		$this->dayParts = new Collection();
	}

	public function setDayPart( DayPart $dayPart ): void {
		$this->dayParts[$dayPart->getId()] = $dayPart;
	}

	public function getDayPartsByCourseId( int $courseId ): ArrayAccess&Traversable&Countable {
		$dayParts = [];
		foreach ($this->dayParts as $dayPart){
			if($dayPart->getCourseId() == $courseId){
				$dayParts[] = $dayPart;
			}
		}
		return Collection::fromArray($dayParts);
	}

	public function getDayPartById( int $id ): ?DayPart {
		return $this->dayParts[$id] ?? null;
	}

	public function setAll( Countable&Traversable&ArrayAccess $dayParts, bool $wipeDB = false ): void {
		if( $wipeDB ) $this->dayParts = new Collection();
		foreach ($dayParts as $dayPart){
			$this->dayParts[$dayPart->getId()] = $dayPart;
		}
	}

	public function getAll(): ArrayAccess&Traversable&Countable {
		return $this->dayParts;
	}

	public function getAllRaw(): ArrayAccess&Traversable&Countable {
		return $this->dayParts;
	}

	public function getCountAll(): int {
		return count($this->dayParts);
	}
}