<?php


namespace App\Components\DataGrids;


use App\Model\Facades\GameFacade;
use App\Model\Translator;
use Kdyby\Doctrine\QueryBuilder;
use Nette\Forms\Container;
use Ublaboo\DataGrid\DataGrid;

class FactionsDataGrid extends BaseMmoGameDataGrid
{
    public function __construct(Translator $translator, GameFacade $gameFacade)
    {
        parent::__construct($translator, $gameFacade);

        $this->enableAdder();
    }

    protected function setupDataSource() : QueryBuilder
    {
        return $this->gameFacade->getFactionReader()->getGridDataSource($this->game);
    }

    protected function setupColumns(DataGrid $grid)
    {
        $this->columnNamePreset($grid, true);
    }

    public function setupAdder(Container $container)
    {
        $container->addText('name', '')->setRequired();
    }

    protected function processAdder($values)
    {
        $this->gameFacade->getFactionWriter()->createFaction($this->game, $values->name);
    }

    protected function editName(int $id, string $name)
    {
        $this->gameFacade->getFactionWriter()->editName($id, $name);
    }
}