<?php


namespace App\Model\Services;


use App\Model\Entities\Game;
use App\Model\Entities\GameData\Mmorpg\Faction;
use App\Model\Entities\Security\User;
use Kdyby\Doctrine\EntityRepository;
use Kdyby\Doctrine\QueryBuilder;

class FactionService extends BaseService
{
    /** @var  EntityRepository */
    protected $factions;

    protected function startup()
    {
        parent::startup();

        $this->factions = $this->getRepository(Faction::class);
    }

    public function createFaction(User $createdBy, Game $game, $name) : Faction
    {
        $faction = new Faction($createdBy);
        $faction->setGame($game);
        $faction->setName($name);

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

    /**
     * @param Game $game
     * @return QueryBuilder
     */
    public function getFactionsGridDataSource(Game $game) : QueryBuilder
    {
        return $this->factions->createQueryBuilder('f')->where('f.game = :game')->setParameter('game', $game);
    }

    public function getFactions(Game $game)
    {
        return $this->factions->findBy(['game' =>  $game]);
    }

    public function findFactionById(int $id)
    {
        return $this->factions->find($id);
    }

    public function getSelectBoxPairs(Game $game)
    {
        $criteria = ['game' => $game];
        $orderBy = ['name' => 'ASC'];

        return $this->factions->findPairs($criteria, 'name', $orderBy);
    }

}