<?php


namespace App\AdminModule\Presenters;


use App\Components\DataGrids\Factories\ILanguagesDataGridFactory;
use App\Components\Forms\TranslateGuiFormFactory;
use App\Model\Exceptions\Translator\UnsupportedLanguageException;
use Nette\Application\UI\Multiplier;
use Nette\Http\IResponse;
use Ublaboo\DataGrid\DataGrid;

class LocalizationPresenter extends AdminBasePresenter
{
    /** @var  TranslateGuiFormFactory */
    protected $translateGuiFormFactory;
    /** @var  ILanguagesDataGridFactory */
    protected $languagesDataGridFactory;

    protected $selectedLang;

    public function injectForms(TranslateGuiFormFactory $translateGuiFormFactory)
    {
        $this->translateGuiFormFactory = $translateGuiFormFactory;
    }

    public function injectDataGrids(ILanguagesDataGridFactory $languagesDataGridFactory)
    {
        $this->languagesDataGridFactory = $languagesDataGridFactory;
    }

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

    	$this->pageHeader = 'Languages';
    }

    public function renderLanguages()
    {
        $this->template->languages = $this->translatorFacade->getLanguageReader()->getLanguages();
    }

    public function actionTranslations($selectedLang = null)
    {
        if (!$this->user->isAllowed($this->site, 'manage_translations')) {
            $this->error(null, IResponse::S403_FORBIDDEN);
        }

        $this->selectedLang = $selectedLang;
        $this->pageHeader = 'Translations';
    }

    public function renderTranslations()
    {
        $defaultLanguage = $this->translatorFacade->getLanguageReader()->getDefaultLanguage();
        $english = $this->translatorFacade->getTranslationReader()->getGuiTranslations($defaultLanguage);
        $this->template->english = $english;

        if ($this->selectedLang !== null) {
            try {
                $lang = $this->translatorFacade->getLanguageReader()->findLanguage($this->selectedLang);
            } catch (UnsupportedLanguageException $e) {
                $lang = $defaultLanguage;
            }
        } else {
            $lang = $defaultLanguage;
        }

        $this->template->language = $lang;
        $this->template->translations = $this->translatorFacade->getTranslationReader()->getGuiTranslationsAssoc($lang);
        $this->template->languages = $this->translatorFacade->getLanguageReader()->getLanguages();
    }

    protected function createComponentTranslateGuiForm()
    {
        return new Multiplier([$this, 'translateGuiForm']);
    }

    public function translateGuiForm()
    {
        $form = $this->translateGuiFormFactory->create();
        $form->onSuccess[] = function () {
            $this->redirect('this', ['selectedLang' => $this->selectedLang]);
        };
        return $form;
    }

    /**
     * @return DataGrid
     */
    protected function createComponentLanguagesGrid()
    {
        return $this->languagesDataGridFactory->create();
    }
}