<?php
namespace GI\MentorFinder\Controller;

use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * Class SearchController
 * @package GI\MentorFinder\Controller
 */

class SearchController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
    /**
     * FrontendUserRepository
     *
     * @var \GI\MentorFinder\Domain\Repository\FrontendUserRepository
     * @TYPO3\CMS\Extbase\Annotation\Inject
     */
    protected $frontendUserRepository = null;

    /**
     * initialize list action
     *
     * @return void
     */
    public function initializeListAction() {
        if ($this->arguments->hasArgument('demand')) {
            $propertyMappingConfiguration = $this->arguments['demand']->getPropertyMappingConfiguration();
            $propertyMappingConfiguration->allowAllProperties();
            $propertyMappingConfiguration->setTypeConverterOption('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter',
                \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED, TRUE);
        }
    }

    /**
     * initialize form action
     *
     * @return void
     */
    public function initializeFormAction() {
        if ($this->arguments->hasArgument('contact')) {
            $propertyMappingConfiguration = $this->arguments['contact']->getPropertyMappingConfiguration();
            $propertyMappingConfiguration->allowAllProperties();
            $propertyMappingConfiguration->setTypeConverterOption('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter',
                \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED, TRUE);
        }
    }

    /**
     * action List
     *
     * @param \GI\MentorFinder\Domain\Model\Demand $demand
     * @return void
     */
    public function listAction(\GI\MentorFinder\Domain\Model\Demand $demand = NULL)
    {
        if ($demand) {
            if ($demand->getShowAll()) {
                $mentors = $this->frontendUserRepository->findAll();
            } else {
                $mentors = $this->frontendUserRepository->findAllByDemand($demand);

                if ($mentors->count() == 0) {
                    $mentors = $this->frontendUserRepository->findAll();
                    $this->view->assign('noResult', $demand);
                }
            }

            if ($GLOBALS['TSFE']->fe_user->user) {
                $this->view->assign('loggedIn', true);
            }

            $this->view->assign('demand', $demand);
            $this->view->assign('mentors', $mentors);
        }
    }

    /**
     * action Form
     *
     * @param \GI\MentorFinder\Domain\Model\Contact $contact
     * @return void
     */
    public function formAction(\GI\MentorFinder\Domain\Model\Contact $contact = NULL){
        $this->view->assign('contact', $contact);
    }

    /**
     * action Send
     *
     * @param \GI\MentorFinder\Domain\Model\Contact $contact
     * @return void
     */
    public function sendAction(\GI\MentorFinder\Domain\Model\Contact $contact = NULL){

        if ($contact->getType() == 'contact') {
            $mentor = $this->frontendUserRepository->findByUid($contact->getMentor());

            if ($mentor->getEmail() && $contact->getEmail()) {
                $this->sendMail($this->settings['mail']['systemMailAddress'],$mentor->getEmail(),'Mentor',$mentor,$contact);
            }

            if ($this->settings['mail']['systemMailAddress'] && $contact->getEmail()) {
                $this->sendMail($this->settings['mail']['systemMailAddress'],$contact->getEmail(),'Mentee',$contact);
            }

            $requests = $mentor->getRequests();
            $requests = $requests+1;
            $mentor->setRequests($requests);
            $this->frontendUserRepository->update($mentor);
        } else if ($contact->getType() == 'request') {

            if ($this->settings['mail']['adminMailAddress'] && $contact->getEmail()) {
                $this->sendMail($this->settings['mail']['systemMailAddress'],$this->settings['mail']['adminMailAddress'],'RequestMentor',$contact);
            }

            if ($this->settings['mail']['systemMailAddress'] && $contact->getEmail()) {
                $this->sendMail($this->settings['mail']['systemMailAddress'],$contact->getEmail(),'RequestMentee',$contact);
            }

        }

        $this->view->assign('contact', $contact);
    }

    /**
     * send mail
     *
     * @param string $from
     * @param string $to
     * @param string $type
     * @param \GI\MentorFinder\Domain\Model\Contact $contact
     * @param mixed $data
     * @return bool
     */
    protected function sendMail($from,$to,$type,$contact,$data = null)
    {
        $mailTemplate = $this->objectManager->get(\TYPO3\CMS\Fluid\View\StandaloneView::class);
        $mailTemplate->assignMultiple([
            'contact' => $contact,
            'data' => $data
        ]);

        $mailTemplate->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($this->settings['mail']['templateFolder'].'Email/'.$type.'Html.html'));
        $mailTemplate->setFormat('html');
        $html = $mailTemplate->render();

        $mailTemplate->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($this->settings['mail']['templateFolder'].'Email/'.$type.'Plain.html'));
        $mailTemplate->setFormat('plain');
        $plain = $mailTemplate->render();

        $mail = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);
        $mail->setFrom($from);
        $mail->setTo($to);
        $mail->setSubject(LocalizationUtility::translate($type.'.subject','mentor_finder'));
        $mail->setBody($plain);
        $mail->addPart($html, 'text/html');

        return $mail->send();
    }
}