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

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

    protected function setupColumns(DataGrid $grid)
    {
        $this->columnNamePreset($grid, true);
        $grid->addColumnText('region', 'Region', 'region.name')->setSortable()->setSortableResetPagination();
        $this->columnStatusPreset($grid);

        $grid->getColumn('name')->getElementPrototype('th')->class('col-xs-6');
        $grid->getColumn('region')->getElementPrototype('th')->class('col-xs-6');
    }

    protected function setupFilter(DataGrid $grid)
    {
        $grid->addFilterText('name', 'Name', 'name');
        $grid->addFilterSelect('region', 'Region', ['' => 'All'] + $this->gameFacade->getRegionReader()->getSelectBoxData($this->game, true), 'region.id');
    }


    public function setupAdder(Container $container)
    {
        $container->addText('name', '')
            ->setRequired();
        $container->addSelect('region', '', $this->gameFacade->getRegionReader()->getSelectBoxData($this->game))
            ->setRequired()
            ->setTranslator(null);
    }

    protected function processAdder($values)
    {
        $this->gameFacade->getServerWriter()->createServer($values->region, $values->name);
    }

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


    protected function statusChange(int $id, string $status)
    {
        $this->gameFacade->getServerWriter()->editState($id, $status);
    }
}