<?php


namespace App\Model\Facades\Readers;


use App\Model\Entities\GameData\Mmorpg\Server;
use App\Model\Exceptions\Db\RecordNotFoundException;
use App\Model\Services\ServerService;
use Kdyby\Doctrine\QueryBuilder;

class ServerReader extends BaseReader
{
    /** @var ServerService */
    protected $serverService;
    /** @var GameReader */
    protected $gameReader;
    /** @var RegionReader */
    protected $regionReader;

    public function __construct(ServerService $serverService, GameReader $gameReader, RegionReader $regionReader)
    {
        $this->serverService = $serverService;
        $this->gameReader = $gameReader;
        $this->regionReader = $regionReader;
    }

    /**
     * @param int $id
     * @return Server
     * @throws RecordNotFoundException
     */
    public function findServerById(int $id) : Server
    {
        $server = $this->serverService->findServerById($id);
        $this->validateEntity($server);

        return $server;
    }

    /**
     * @param int|Server $server
     * @return Server
     */
    public function validateServer($server) : Server
    {
        if ($server instanceof Server) {
            return $server;
        }

        return $this->findServerById($server);
    }

    /**
     * @param $game
     * @return QueryBuilder
     */
    public function getGridDataSource($game) : QueryBuilder
    {
        $gameEntity = $this->gameReader->validateGame($game);

        return $this->serverService->getServersGridDataSource($gameEntity);
    }

    public function getSelectBoxPairs($region) : array
    {
        $regionEntity = $this->regionReader->validateRegion($region);

        return $this->serverService->getSelectBoxPairs($regionEntity);
    }
}