<?php //declare(strict_types = 1);

namespace App\Modules\Front\Home;

use App\Modules\Front\BaseFrontPresenter;
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;

final class HomePresenter extends BaseFrontPresenter
{

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

		$result = $this->doSearch('TermsFullQuery', $translation, $term, $clen);

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

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

		$result = $this->doSearch('TermsFullQuery', $translation, $term, null);

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

	public function actionPlay($id = null)
	{
		$pron = $this->em->getPronunciationRepository()->find(['id' => $id]);
		$basepath = $this->getHttpRequest()->url->basePath . "/media";
		$oggResp = new OggResponse($id, $pron->filename);
		return $this->sendResponse($oggResp);
	}


	public function actionDefault()
	{
		if ($this->getRequest()->isMethod('GET') && $this->term) {
			$this['searchForm']['string']->setValue($this->term);
			$t = $this->slugs[$this->slug];
			$result = $this->doSearch('TermsQuery',$t->id,$this->term);
			if ($result) $this->template->result = $result;
		} else {
			$this->term = $this['searchForm']['string']->getValue();
		}
		$this['searchForm']->onSuccess[] = array($this, 'searchFormSucceeded');
	}

	public function actionSelect($slug, $term = null)
	{
		$this['searchForm']->onSuccess[] = array($this, 'searchFormSucceededIpa');
		if ($this->getRequest()->isMethod('GET')) {
			if (isset($this->slugs[$slug])) {
				$t = $this->slugs[$slug];
				$this['searchForm']['translation']->setValue($t->id);
				$translation = $t;
			}
		} else {

			$id = $this['searchForm']['translation']->value;
			if (isset($this->translations[$id])) {
				$t = $this->translations[$id];
				$this->redirect('Front:Home:select', $t["slug"], $this['searchForm']['string']->value);
			}
		}
		if ($term) {
			$query = new TermsFullQuery($translation->dictionary, $term, $translation->direction);
			$result = $this->repoTerms->fetch($query);

			$this->template->result = $result;
			$this->template->translation = $translation;
		}
		$this->setView('ipa');
	}

	

	public function renderDefault()
	{
	}

	public function actionIpa()
	{
		if ($this->term)
			$this->redirect('Search:ipa',$this->translation,$this->term);
	}

	public function actionInteractive()
	{
		if ($this->getRequest()->isMethod('GET')) {
			if ($this->term) {
				$this['searchForm']['string']->setValue($this->term);
				$result = $this->doSearch('TermsFullQuery', $this->slugs[$this->slug]->id, $this->term);
				if ($result) $this->template->result = $result;
			}
		}

		$this['searchForm']->onSuccess[] = array($this, 'searchFormSucceededInteractive');
	}

	public function renderAutocomplete($whichData, $typedText = '', $translation = 1)
	{
		$translation = $this->repoTranslations->find($translation);

		$query = new TermsQuickQuery($translation->dictionary, $typedText, $translation->direction);
		$result = $this->repoTerms->fetch($query);

		$map = [];
		foreach ($result as $str) {
			$map[] = $str["string1"];
		}

		return $this->sendResponse(new JsonResponse($map));
	}

	public function renderIpa()
	{
	}

	public function renderMerge()
	{
	}
}
