<?php


namespace App\Components\DataGrids;


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

class InstanceTypesDataGrid extends BaseMmoGameDataGrid
{
    public function __construct(Translator $translator, GameFacade $gameFacade)
    {
        parent::__construct($translator, $gameFacade);
        $this->enableAdder();
    }

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

    protected function setupColumns(DataGrid $grid)
    {
        $this->columnNamePreset($grid, true);
        $grid->addColumnText('type', 'Type', 'isPvE')->setSortable()->setSortableResetPagination()
            ->setReplacement([InstanceType::PVE => 'PvE', InstanceType::PVP => 'PvP']);
    }

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

    public function setupAdder(Container $container)
    {
        $container->addText('name', '');
        $container->addSelect('type', '', [InstanceType::PVE => 'PvE', InstanceType::PVP => 'PvP'])
            ->setTranslator(null);
    }

    protected function processAdder($values)
    {
        $this->gameFacade->getInstanceWriter()->createInstanceType($this->game, $values->name, $values->type);
    }


}