<?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 InstancesDataGrid extends BaseMmoGameDataGrid
{
    public function __construct(Translator $translator, GameFacade $gameFacade)
    {
        parent::__construct($translator, $gameFacade);
        $this->enableAdder();
    }


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

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

    public function setupAdder(Container $container)
    {
        $container->addText('name', '');
        $container->addSelect('type', '', $this->gameFacade->getInstanceReader()->getInstanceTypeNamePairs($this->game))
            ->setTranslator(null);
    }

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

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


}