<?php


namespace App\Components\DataGrids;


use App\Model\Entities\Game;
use App\Model\Exceptions\Components\RequiredDataNotSetException;
use App\Model\Facades\SiteFacade;
use App\Model\Translator;
use Kdyby\Doctrine\QueryBuilder;
use Ublaboo\DataGrid\DataGrid;

class GuildsDataGrid extends BaseDataGrid
{
    /** @var Game */
    protected $game;
    /** @var SiteFacade */
    protected $siteFacade;

    /**
     * GamesDataGridFactory constructor.
     * @param Translator $translator
     * @param SiteFacade $siteFacade
     */
    public function __construct(Translator $translator, SiteFacade $siteFacade)
    {
        parent::__construct($translator);
        $this->siteFacade = $siteFacade;
    }

    /**
     * @param Game $game
     */
    public function setGame(Game $game)
    {
        $this->game = $game;
    }

    protected function createComponentGrid() : DataGrid
    {
        if (!$this->game instanceof Game) {
            throw new RequiredDataNotSetException();
        }

        return parent::createComponentGrid();
    }

    protected function setupDataSource() : QueryBuilder
    {
        return $this->siteFacade->getSiteReader()->getGuildsGridDataSource($this->game);
    }

    protected function setupColumns(DataGrid $grid)
    {
        $this->columnNamePreset($grid);
        $grid->addColumnText('region', 'Region', 'server.region.name')
            ->setSortable('p.name')
            ->setSortableResetPagination();
        $grid->addColumnText('server', 'Server', 'server.name')
            ->setSortable('s.name')
            ->setSortableResetPagination();
        $grid->addColumnText('faction', 'Faction', 'faction.name')
            ->setSortable('f.name')
            ->setSortableResetPagination();

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

    protected function setupActions(DataGrid $grid)
    {
        $grid->addAction('detail', 'detail', 'Homepage:', ['siteId' => 'id'])->setClass('btn btn-danger btn-xs');
    }


}