<?php declare(strict_types = 1);

namespace App\Model\Database\Facade;

use App\Model\Database\Entity\Dictionary;
use App\Model\Database\Entity\Term;
use App\Model\Database\Entity\Translation;
use App\Model\Database\EntityManager;


class TermFacade
{

	/** @var EntityManager */
	private $em;
    private $direction;

	public function __construct(
		EntityManager $em
	)
	{
		$this->em = $em;
	}

    public function setDirection(int $direction)
    {
        $this->direction = $direction;
    }
	/**
	 * @param mixed[] $data
	 */
 	public function findByString(String $term,Dictionary $d,String $clen = null)
	{
		$query = $this->em->createQueryBuilder('t')->select('t')
		    ->addSelect('s1')
		    ->addSelect('s2')
            ->from(Term::class,'t')
		    ->leftJoin('t.suffix1','s1')
		    ->leftJoin('t.suffix2','s2');
		if ($this->direction == 1) {
		    $query->where('t.string1 LIKE :str')->setParameter('str', $term."%");
		    $query->orderBy('t.string1,t.id','ASC');
		} else	if ($this->direction == 2) {
		    $query->where('t.string2 LIKE :str')->setParameter('str', $term."%");
		    $query->orderBy('t.string2','ASC');
		    $query->addOrderBy('t.order2','ASC');
		}
		$query->andWhere('t.dictionary = :dict')->setParameter(':dict',$d->id);
		if ($clen) 
		  $query->andWhere('t.member LIKE :clen')->setParameter('clen','%'.$clen.'%');

		return $query->getQuery();
	}	

}
