<?php


namespace App\AdminModule\Presenters;


use App\Components\DataGrids\BossesDataGrid;
use App\Components\DataGrids\CharacterClassesDataGrid;
use App\Components\DataGrids\FactionsDataGrid;
use App\Components\DataGrids\Factories\IBossesDataGridFactory;
use App\Components\DataGrids\Factories\ICharacterClassesDataGridFactory;
use App\Components\DataGrids\Factories\IFactionsDataGridFactory;
use App\Components\DataGrids\Factories\IInstancesDataGridFactory;
use App\Components\DataGrids\Factories\IInstanceTypesDataGridFactory;
use App\Components\DataGrids\Factories\IRacesDataGridFactory;
use App\Components\DataGrids\Factories\IRegionsDataGridFactory;
use App\Components\DataGrids\Factories\IServersDataGridFactory;
use App\Components\DataGrids\InstancesDataGrid;
use App\Components\DataGrids\InstanceTypesDataGrid;
use App\Components\DataGrids\RacesDataGrid;
use App\Components\DataGrids\RegionsDataGrid;
use App\Components\DataGrids\ServersDataGrid;
use App\Components\Sidebar\SidebarMenu\SidebarMenu;
use App\Model\Entities\Game;
use App\Model\Exceptions\Db\RecordNotFoundException;
use App\Model\Facades\GameFacade;
use App\Model\Facades\WowFacade;
use Nette\Application\BadRequestException;
use Nette\Http\IResponse;

class GameContentPresenter extends AdminBasePresenter
{
    /** @persistent */
    public $gameId;
    /** @var  Game */
    protected $game;
    /** @var  GameFacade */
    protected $gameFacade;

    /** @var  WowFacade */
    protected $wowFacade;
    /** @var  IBossesDataGridFactory */
    protected $bossesDataGridFactory;

    /** @var  ICharacterClassesDataGridFactory */
    protected $characterClassesDataGridFactory;
    /** @var  IFactionsDataGridFactory */
    protected $factionsDataGridFactory;
    /** @var  IRacesDataGridFactory */
    protected $racesDataGridFactory;
    /** @var  IRegionsDataGridFactory */
    protected $regionsDataGridFactory;
    /** @var  IServersDataGridFactory */
    protected $serversDataGridFactory;
    /** @var  IInstanceTypesDataGridFactory */
    protected $instanceTypesDataGridFactory;
    /** @var  IInstancesDataGridFactory */
    protected $instancesDataGridFactory;

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

    public function injectDataGrids(IBossesDataGridFactory $bossesDataGridFactory,
                                    ICharacterClassesDataGridFactory $characterClassesDataGridFactory,
                                    IFactionsDataGridFactory $factionsDataGridFactory,
                                    IRacesDataGridFactory $racesDataGridFactory,
                                    IRegionsDataGridFactory $regionsDataGridFactory,
                                    IServersDataGridFactory $serversDataGridFactory,
                                    IInstanceTypesDataGridFactory $instanceTypesDataGridFactory,
                                    IInstancesDataGridFactory $instancesDataGridFactory)
    {
        $this->bossesDataGridFactory = $bossesDataGridFactory;
        $this->characterClassesDataGridFactory = $characterClassesDataGridFactory;
        $this->factionsDataGridFactory = $factionsDataGridFactory;
        $this->racesDataGridFactory = $racesDataGridFactory;
        $this->regionsDataGridFactory = $regionsDataGridFactory;
        $this->serversDataGridFactory = $serversDataGridFactory;
        $this->instanceTypesDataGridFactory = $instanceTypesDataGridFactory;
        $this->instancesDataGridFactory = $instancesDataGridFactory;
    }

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

        try {
            $this->game = $this->gameFacade->getGameReader()->findGameById($this->gameId);
        } catch (RecordNotFoundException $e) {
            $this->error('Requested game not found.', IResponse::S404_NOT_FOUND);
        }

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

    public function beforeRender()
    {
        parent::beforeRender();

    	$this->template->game = $this->game;
    }

    protected function createComponentSidebarMenu() : SidebarMenu
    {
        $menu = parent::createComponentSidebarMenu();

        $menu->addHeader('gameHeader', $this->game->getName());
        $menu->addLink('summary', 'Summary', 'GameContent:');
        $menu->addLink('regions', 'Regions', 'GameContent:regions');
        $menu->addLink('servers', 'Servers', 'GameContent:servers');
        $menu->addLink('factions', 'Factions', 'GameContent:factions');
        $menu->addLink('classes', 'Character classes', 'GameContent:classes');
        $menu->addLink('races', 'Character races', 'GameContent:races');
        $menu->addLink('instanceTypes', 'Instance types', 'GameContent:instanceTypes');
        $menu->addLink('instances', 'Instances', 'GameContent:instances');
//        $menu->addLink('modes', 'Instance modes', 'GameContent:modes');
//        $menu->addLink('bosses', 'Bosses', 'GameContent:bosses');
//        $menu->addLink('professions', 'Professions', 'GameContent:professions');

        return $menu;
    }

    public function handleImportWoWRegions()
    {
        $this->worldOfWarcraftOnly();

        $this->wowFacade->getWowApiWriter()->importRegions($this->game);
    }

    public function handleImportWowServers()
    {
        $this->worldOfWarcraftOnly();

        $this->wowFacade->getWowApiWriter()->importRealms($this->game);
    }

    public function handleImportWowCharacterClasses()
    {
        $this->worldOfWarcraftOnly();

        $this->wowFacade->getWowApiWriter()->importCharacterClasses($this->game);
    }

    public function handleImportWowRaces()
    {
        $this->worldOfWarcraftOnly();

        $this->wowFacade->getWowApiWriter()->importRaces($this->game);
    }

    public function handleImportWowFactions()
    {
        $this->worldOfWarcraftOnly();

        $this->wowFacade->getWowApiWriter()->importFactions($this->game);
    }

    public function handleImportWowInstances()
    {
        $this->worldOfWarcraftOnly();

        $this->wowFacade->getWowApiWriter()->importInstances($this->game);
    }

    public function handleImportWoWInstanceTypes()
    {
        $this->worldOfWarcraftOnly();

        $this->wowFacade->getWowApiWriter()->importInstanceTypes($this->game);
    }

    /**
     * @return RegionsDataGrid
     */
    protected function createComponentRegionsGrid() : RegionsDataGrid
    {
        $component = $this->regionsDataGridFactory->create();
        $component->setGame($this->game);

        return $component;
    }

    /**
     * @return ServersDataGrid
     */
    protected function createComponentServersGrid() : ServersDataGrid
    {
        $component = $this->serversDataGridFactory->create();
        $component->setGame($this->game);

        return $component;
    }

    /**
     * @return FactionsDataGrid
     */
    protected function createComponentFactionsGrid() : FactionsDataGrid
    {
        $component = $this->factionsDataGridFactory->create();
        $component->setGame($this->game);

        return $component;
    }

    /**
     * @return CharacterClassesDataGrid
     */
    protected function createComponentCharacterClassesGrid() : CharacterClassesDataGrid
    {
        $grid = $this->characterClassesDataGridFactory->create();
        $grid->setGame($this->game);

        return $grid;
    }

    /**
     * @return RacesDataGrid
     */
    protected function createComponentRacesGrid() : RacesDataGrid
    {
        $grid = $this->racesDataGridFactory->create();
        $grid->setGame($this->game);

        return $grid;
    }

    /**
     * @return InstancesDataGrid
     */
    protected function createComponentInstancesGrid() : InstancesDataGrid
    {
        $grid = $this->instancesDataGridFactory->create();
        $grid->setGame($this->game);

        return $grid;
    }

    /**
     * @return InstanceTypesDataGrid
     */
    protected function createComponentInstanceTypesGrid() : InstanceTypesDataGrid
    {
        $grid = $this->instanceTypesDataGridFactory->create();
        $grid->setGame($this->game);

        return $grid;
    }

    /**
     * @return BossesDataGrid
     */
    protected function createComponentBossesGrid() : BossesDataGrid
    {
        $grid = $this->bossesDataGridFactory->create();
        $grid->setGame($this->game);

        return $grid;
    }

    /**
     * @throws BadRequestException
     */
    protected function worldOfWarcraftOnly()
    {
        if (!$this->game->isWoW()) {
            $this->error('Import is available for Word of Warcraft only.', IResponse::S400_BAD_REQUEST);
        }
    }

}