<?php


namespace App\AdminModule\Presenters;

use App\Components\DataGrids\Factories\IGamesAdminDataGridFactory;
use App\Components\DataGrids\Factories\IPlatformsDataGridFactory;
use App\Components\DataGrids\Factories\IPublishersDataGridFactory;
use App\Components\DataGrids\Factories\IStudiosDataGridFactory;
use App\Components\DataGrids\GamesAdminDataGrid;
use App\Components\DataGrids\PlatformsDataGrid;
use App\Components\DataGrids\PublishersDataGrid;
use App\Components\DataGrids\StudiosDataGrid;
use App\Components\Forms\BootstrapForm;
use App\Components\Forms\GameFormFactory;
use App\Model\Facades\GameFacade;
use Nette\Http\IResponse;
use xhulav\BootstrapForms\Rendering\BootstrapHorizontalFormRenderer;

class GamePresenter extends AdminBasePresenter
{
    /** @var  GameFacade */
    protected $gameFacade;
    /** @var  GameFormFactory */
    protected $gameFormFactory;

    /** @var  IGamesAdminDataGridFactory */
    protected $gamesDataGridFactory;
    /** @var  IPlatformsDataGridFactory */
    protected $platformsDataGridFactory;
    /** @var  IPublishersDataGridFactory */
    protected $publishersDataGridFactory;
    /** @var  IStudiosDataGridFactory */
    protected $studiosDataGridFactory;

    public function injectFacades(GameFacade $gameFacade)
    {
        $this->gameFacade = $gameFacade;
    }

    public function injectForms(GameFormFactory $gameFormFactory)
    {
        $this->gameFormFactory = $gameFormFactory;
    }

    public function injectDataGrids(IGamesAdminDataGridFactory $gamesDataGridFactory,
                                    IPlatformsDataGridFactory $platformDataGridFactory,
                                    IPublishersDataGridFactory $publishersDataGridFactory,
                                    IStudiosDataGridFactory $studiosDataGridFactory)
    {
        $this->gamesDataGridFactory = $gamesDataGridFactory;
        $this->platformsDataGridFactory = $platformDataGridFactory;
        $this->publishersDataGridFactory = $publishersDataGridFactory;
        $this->studiosDataGridFactory = $studiosDataGridFactory;
    }

    protected function startup()
    {
        parent::startup();

        if (!$this->user->isAllowed($this->site, 'manage_games')) {
            $this->error(null, IResponse::S403_FORBIDDEN);
        }
    }


    public function renderList()
    {
        $this->template->games = $this->gameFacade->getGameReader()->getGames();
    }

    public function renderPlatforms()
    {
        $this->template->platforms = $this->gameFacade->getPlatformReader()->getPlatforms();
    }

    public function renderPublishers()
    {
        $this->template->publishers = $this->gameFacade->getPublisherReader()->getPublishers();
    }

    public function renderStudios()
    {
        $this->template->studios = $this->gameFacade->getStudioReader()->getStudios();
    }

    /**
     * @return BootstrapForm
     */
    protected function createComponentGameForm() : BootstrapForm
    {
        $form = $this->gameFormFactory->create();
        $form->setRenderer(new BootstrapHorizontalFormRenderer());
        $form->onSuccess[] = function () {
            $this->redirect('this');
        };

        return $form;
    }

    /**
     * @return GamesAdminDataGrid
     */
    protected function createComponentGamesGrid() : GamesAdminDataGrid
    {
        return $this->gamesDataGridFactory->create();
    }

    /**
     * @return PlatformsDataGrid
     */
    protected function createComponentPlatformsGrid() : PlatformsDataGrid
    {
        return $this->platformsDataGridFactory->create();
    }

    /**
     * @return PublishersDataGrid
     */
    protected function createComponentPublishersGrid() : PublishersDataGrid
    {
        return $this->publishersDataGridFactory->create();
    }

    /**
     * @return StudiosDataGrid
     */
    protected function createComponentStudiosGrid() : StudiosDataGrid
    {
        return $this->studiosDataGridFactory->create();
    }
}