<?php

namespace module\login\controllers;

use autoload, language, urlController, twig;
use config, DateTime, DateTimeZone;
use module\login\models\LoginModel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Validation;

class LoginController
{
	/** @var  twig */
	protected $twig;
	/** @var  language */
	protected $language;
	/** @var  urlController */
	protected $urlController;
	/** @var  Request */
	protected $request;
	/** @var  Validation */
	protected $validator;
	/** @var  LoginModel */
	protected $loginModel;
	/** @var  config */
	protected $config;
	/** @var  Session */
	protected $session;
	/** @var  DateTime */
	protected $dateTime;

	public function __construct()
	{
		$autoload = new autoload();
		$this->config = new config();
		$this->request = new Request(array(), $_POST, array(), array(), array(), $_SERVER);
		$this->loginModel = new LoginModel();
		$this->session = new Session();
		$this->dateTime = new DateTime();
		$this->validator = Validation::createValidator();
		$this->twig = $autoload->class_load('twig');
		$this->language = $autoload->class_load('language');
		$this->urlController = $autoload->class_load('urlController');
		$this->twig->setModule('login');
	}

	public function showLoginForm()
	{
		$language = $this->language;
		$request = $this->request;
		$loginManager = $this->generateErrorMessage();
		$userSessionData = $this->session->get('user_login_data');
		$urlController = $this->urlController;
		if (isset($userSessionData)) {
			header(
				'Location: ' .
				$urlController->getCompanyBaseUrl() . $urlController->changeUrl('beallitasok'),
				302
			);
		} else {
			if (is_array($loginManager)) {
				return $this->twig->render('login.html', array(
					'loginLang' => $language->get('Bejelentkezés'),
					'emailLang' => $language->get('Email cím'),
					'passwordLang' => $language->get('Jelszó'),
					'enterLang' => $language->get('Belép'),
					'ifYouDoNotRegisterLang' => $language->get('Ha még nem regisztráltál'),
					'signUpLang' => $language->get('Regisztráció'),
					'youCanDoItHereLang' => $language->get('megteheted itt'),
					'singUpUrl' => $urlController->changeUrl('regisztracio'),
					'errorMessages' => $loginManager,
					'emailPostValue' => $request->get('email'),
					'passwordPostValue' => $request->get('password'),
				));
			}
		}
	}

	protected function generateErrorMessage()
	{
		$errorMessage = [];
		$request = $this->request;
		$submit = $request->get('submit');
		if ($request->getMethod() === 'POST' && isset($submit)) {
			$validation = $this->validator;

			$email = $request->get('email');
			$emailValidator = $validation->validate($email, array(
				new Email(),
				new NotBlank()
			));
			$language = $this->language;
			if ($emailValidator->count() !== 0) {
				$errorMessage[] = $language->get('Az e-mail cím formátuma nem megfelelő!');
			}

			$password = $request->get('password');
			$passwordValidator = $validation->validate($password, array(
				new NotBlank(),
				new Length(array('min' => 3))
			));
			if ($passwordValidator->count() !== 0) {
				$errorMessage[] = $language->get('Minden mező kitöltése kötelező.');
			}

			if ($emailValidator->count() === 0 && $passwordValidator->count() === 0) {
				$loginModel = $this->loginModel;
				$account = $loginModel->passwordEmailCheckByDatabase($email, $password);
				if (!$account) {
					$errorMessage[] = $language->get('A megadott email cím vagy jelszó helytelen.');
				} else {
					$this->session->set('user_login_data', $account);
					$dateTime = $this->dateTime;
					$dateTime->setTimezone(new DateTimeZone('Europe/Budapest'));
					$loginModel->updateLastLoginDateTimeByUserId((int)$account['id'], $dateTime);
				}
			}
		}

		return $errorMessage;
	}
}