<?php

namespace Planaday\Database\InMemoryDatabase;

use ArrayAccess;
use Countable;
use Planaday\Entities\Location\DTO\Location;
use Planaday\Entities\Location\LocationDatabase;
use Planaday\Utility\DataGrouping\Collection;
use Planaday\Utility\DataGrouping\Map;
use Traversable;

class InMemoryLocationDB implements LocationDatabase {
	/** @var Map<Location> */
	public Map $locations;

	public function __construct() {
		$this->locations = new Map();
	}

	public function setLocation( Location $location ): void {
		$this->locations[$location->getId()] = $location;
	}

	public function getLocationById( string $id ): ?Location {
		return $this->locations[$id] ?? null;
	}

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

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

	public function getAllCities(): Collection {
		$cities = [];
		foreach ($this->locations as $location){
			$cities[] = $location->getCity();
		}
		return new Collection($cities);
	}

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

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