<?php


namespace App\Model\Facades\Readers;

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

class FactionReader extends BaseReader
{
    /** @var FactionService */
    protected $factionService;
    /** @var GameReader */
    protected $gameReader;

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

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

        return $this->factionService->getFactionsGridDataSource($gameEntity);
    }

    /**
     * @param $game
     * @return Faction[]
     * @throws RecordNotFoundException
     */
    public function getFactions($game) : array
    {
        $gameEntity = $this->gameReader->validateGame($game);

        return $this->factionService->getFactions($gameEntity);
    }

    /**
     * @param int $id
     * @return Faction
     */
    public function findFActionById(int $id) : Faction
    {
        return $this->factionService->findFactionById($id);
    }

    /**
     * @param int|Faction $faction
     * @return Faction
     */
    public function validateFaction($faction) : Faction
    {
        if ($faction instanceof Faction) {
            return $faction;
        }

        return $this->findFActionById($faction);
    }

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

        return $this->factionService->getSelectBoxPairs($gameEntity);
    }

}