<?php


namespace App\Model\Facades\Readers;


use App\Model\Entities\Game;
use App\Model\Entities\GameData\Mmorpg\CharacterClass;
use App\Model\Services\CharacterClassesService;
use Kdyby\Doctrine\QueryBuilder;

class CharacterClassReader extends BaseReader
{
    /** @var CharacterClassesService */
    protected $characterClassesService;
    /** @var GameReader */
    protected $gameReader;

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

    /**
     * @param $id
     * @return CharacterClass
     */
    public function findClassById($id) : CharacterClass
    {
        return $this->characterClassesService->findClassById($id);
    }

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

        return $this->characterClassesService->getCharacterClassesGridDataSource($gameEntity);
    }

    /**
     * @param int|CharacterClass $class
     * @return CharacterClass
     */
    public function validateCharacterClass($class) : CharacterClass
    {
        if ($class instanceof CharacterClass) {
            return $class;
        }

        return $this->findClassById($class);
    }

}