<?php


namespace App\FrontModule\Presenters;


use App\Components\DataGrids\Factories\IGuildsDataGridFactory;
use App\Components\DataGrids\GuildsDataGrid;
use App\Components\Forms\Factories\IGuildCreateFormFactory;
use App\Components\Forms\GuildCreateForm;
use App\Model\Entities\MmorpgGuild;
use Nette\Http\IResponse;

class GuildPresenter extends FrontBasePresenter
{
    /** @var  IGuildCreateFormFactory */
    protected $guildCreateFormFactory;
    /** @var  IGuildsDataGridFactory */
    protected $guildsDataGridFactory;

    public function injectForms(IGuildCreateFormFactory $guildCreateFormFactory)
    {
        $this->guildCreateFormFactory = $guildCreateFormFactory;
    }

    public function injectDataGrids(IGuildsDataGridFactory $guildsDataGridFactory)
    {
        $this->guildsDataGridFactory = $guildsDataGridFactory;
    }

    public function actionCreate()
    {
        if (!$this->user->isLoggedIn()) {
            $this->error(null, IResponse::S403_FORBIDDEN);
        }

    	$this->pageHeader = 'Create guild';
    }

    public function actionList()
    {
        $this->pageHeader = 'Guilds list';
    }

    protected function createComponentGuildCreateForm() : GuildCreateForm
    {
        $component = $this->guildCreateFormFactory->create($this->site);
        $component->onSave[] = function (GuildCreateForm $form, MmorpgGuild $guild) {
            $this->redirect('Homepage:', ['siteId' => $guild->getId()]);
        };

        return $component;
    }

    protected function createComponentGuildsGrid() : GuildsDataGrid
    {
        $component = $this->guildsDataGridFactory->create();
        $component->setGame($this->site);

        return $component;
    }
}