<?php declare(strict_types = 1);

namespace App\Modules\Front;

use App\Modules\Base\UnsecuredPresenter;
use Nette;
use Nette\Utils\Strings;
use Nette\Application\UI;
use Dict\OggResponse;
use Nette\Application\Responses\JsonResponse;
use AlesWita\VisualPaginator\VisualPaginator;
use AlesWita\VisualPaginator\VisualPaginatorFactory;
use App\Model\Database\EntityManager;
use App\Model\Database\Entity\Dictionary;
use App\Model\Database\Entity\Translation;
use App\Model\Database\Repository\TranslationRepository;
use App\Model\Database\Repository\TermsFullQuery;
use App\Model\Database\Repository\TermsQuickQuery;
use App\Model\Database\Facade\TermFacade;

abstract class BaseFrontPresenter extends UnsecuredPresenter
{
    /** @inject @var EntityManager */
	public EntityManager $em;
	/** @inject */
	public VisualPaginatorFactory $visualPaginatorFactory;
	/** @inject */
	public TermFacade $termFacade;

	/** @persistent */
	public $term;
	
	/** @persistent */
	public $slug;

    public $translation;
    public $translations;
    public $dictionaries;
    public $drsSelect;
    public $slugs;
    
	protected function startup()
	{
		
		$trs = $this->em->getRepository(Translation::class)->findBy([], ['id' => 'ASC', 'direction' => 'ASC']);
		$drs = $this->em->getRepository(Dictionary::class)->findPairs('id', 'name');
		$select = [];
        $translations = [];
		foreach ($trs as $t) {
			$lang1 = $t->getLangName1();
			$lang2 = $t->getLangName2();
			
			$lang = mb_substr($lang1, 0, -1);
			$lang .= "o-" . $lang2;
			$slug = Strings::webalize($lang);
			$select[$drs[$t->getDictionary()->id]][$t->id] = $lang;
			$translations[$t->id]["slug"] = $slug;
			$translations[$t->id]["lang"] = $lang;
			$translations[$t->id]["t"] = $t;
			$slugs[$slug] = $t;
		}
		$this->slug= $translations[1]["slug"];
		$this->translations = $translations;
		$this->dictionaries = $drs;
		$this->drsSelect = $select;
		$this->slugs = $slugs;

		parent::startup();
	}

    public function handleGetInfo($id)
	{
		$term = $this->repoTerms->find($id);
		$termFlags = $this->repoTermFlags->findAssoc([], 'id');

		$dictionary = $term->dictionary;

		$query = new TermsFullQuery($dictionary, $term->string1, 1, false);
		$result = $this->repoTerms->fetch($query);

		$this->template->result = $result;
		$this->template->termFlags = $termFlags;

		$this->setView('info');
	}

    public function beforeRender()
	{
		$dicttypes = $this->em->getDictTypeRepository()->findAssoc([], 'id');
		$this->template->dicttypes = $dicttypes;
		$this->template->dictionaries = $this->dictionaries;
		$this->template->translations = $this->translations;
		//   $this->template->paginator = $this->paginator;
	}

    protected function createComponentSearchForm()
	{
		$form = new UI\Form;
		$form->addText('string', 'Výraz')
			->setRequired('Zadajte výray');
		$form->addSelect('translation', 'Preklad', $this->drsSelect)
			->setDefaultValue(1)
			->setRequired('Vyberte slovnik');
		$form->addText('clen', 'Clen', 5);
		$form->addSubmit('search', 'Vyhaľadaj');
		$form->onSuccess[] = array($this, 'searchFormSucceeded');
		return $form;
	}

	public function searchFormSucceeded(UI\Form $form, $values)
	{
		$translation = $values->translation;
		$clen = $values->clen;
		$term = $values->string;

		$this->redirect('Search:default',$translation,$term,$clen);

	}
}
