<?php


namespace App\Model\Facades\Readers;


use App\Model\Entities\Game;
use App\Model\Entities\GameData\Mmorpg\Region;
use App\Model\Exceptions\Db\RecordNotFoundException;
use App\Model\Services\RegionService;
use Kdyby\Doctrine\QueryBuilder;

class RegionReader extends BaseReader
{
    /** @var RegionService */
    protected $regionService;
    /** @var GameReader */
    protected $gameReader;

    public function __construct(RegionService $regionService, GameReader $gameReader)
    {
        $this->regionService = $regionService;
        $this->gameReader = $gameReader;
    }

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

        return $this->regionService->getRegionsGridDataSource($gameEntity);
    }

    /**
     * @param $id
     * @return Region
     * @throws RecordNotFoundException
     */
    public function findRegionById($id)
    {
        $region = $this->regionService->findRegionById($id);
        $this->validateEntity($region);

        return $region;
    }

    /**
     * @param int|Game $game
     * @return Region[]
     */
    public function getRegions($game)
    {
        $gameEntity = $this->gameReader->validateGame($game);

        return $this->regionService->getRegions($gameEntity);
    }

    /**
     * @param $region
     * @return Region
     */
    public function validateRegion($region) : Region
    {
        if ($region instanceof Region) {
            return $region;
        }

        return $this->findRegionById($region);
    }

    /**
     * @param int|Game $game
     * @return array
     */
    public function getRegionsIdentifiers($game) : array
    {
        $gameEntity = $this->gameReader->validateGame($game);

        return $this->regionService->getRegionsIdentifiers($gameEntity);
    }

    public function getSelectBoxData($game, $all = false) : array
    {
        $gameEntity = $this->gameReader->validateGame($game);

        return $this->regionService->getSelectBoxData($gameEntity, $all);
    }
}