<?php

Lib::loadClass('RouteToolBase');
Lib::loadClass('UI');
Lib::loadClass('Response');
Lib::loadClass('Theme');

require_once dirname(__FILE__) . '/../auth.php'; // Theme_Auth_panel_biall_net

// class name must have the same name as file
// index.php?_route=UrlAction_RemindPasswd  - uruchamia defaultAction
class RouteTool_RemindPasswd extends RouteToolBase {

	function handleAuth() {
		// return (!User::logged())
		// ? $this->remindPasswdAction()
		// : $this->sendAlreadyLoggedIn()
		// ;
		$task = V::get('_task', '', $_GET);
		switch ($task) {
			case 'rp': return $this->rpAction();
			default: return $this->remindPasswdAction();
		}
	}

	function remindPasswdAction() {
		if ('remind' == V::get('_postTask', '', $_POST)) {
			try {
				$email = V::get('ADM_ACCOUNT', '', $_POST);
				$this->remindPasswd($email);
			} catch (Exception $e) {
				$this->sendRemindPasswdForm([ 'errors' => [ $e->getMessage() ] ]);
			}
			return $this->sendRemindPasswdSent();
		}
		$this->sendRemindPasswdForm();
		exit;
	}
	function remindPasswd($email) {
		if (empty($email)) throw new Exception("Proszę podać adres email");
		if (!filter_var($email, FILTER_VALIDATE_EMAIL)) throw new Exception("Proszę podać poprawny adres email");

		// BŁĄD: Brak zarejestrowanego użytkownika o wprowadzonym adresie email.

		Theme_Auth_panel_biall_net::fetchUser($email);
		$remindKey = Theme_Auth_panel_biall_net::generateRemindKey($email);
		$resetLink = $this->getLink('rp', [ 'login' => $email, 'key' => $remindKey ]);
		Theme_Auth_panel_biall_net::sendRemindPasswd($email, $resetLink);
	}

	function rpAction() {
		if ('set' == V::get('_postTask', '', $_POST)) {
			try {
				$email = V::get('ADM_ACCOUNT', '', $_POST);
				$remindKey = V::get('REMIND_KEY', '', $_POST);
				$newPasswd = V::get('ADM_PASSWD', '', $_POST);
				Theme_Auth_panel_biall_net::setPasswd($email, $newPasswd, $remindKey);
			} catch (Exception $e) {
				$this->sendNewPasswdForm(array_merge($_GET, [ 'errors' => [ $e->getMessage() ] ]));
			}
			return $this->sendRemindPasswdSet();
		}
		$this->sendNewPasswdForm($_GET);
		exit;
	}
	function sendNewPasswdForm($args = []) {
		UI::gora();
		UI::tryCatchView([ $this, 'setNewPassForm' ], [ 'args' => $args ]);
		UI::dol();
		exit;
	}
	function sendRemindPasswdSet() {
		UI::gora();
		Theme::remindNewPasswordSet($data = [ 'msg' => "Twoje nowe hasło zostało zapisane." ]);
		UI::dol();
		exit;
	}

	function setNewPassForm($args) {
		$login = V::get('login', '', $args);
		$remindKey = V::get('key', '', $args);
		if (empty($login)) throw new Exception("Missing login!");
		if (empty($remindKey)) throw new Exception("Missing key!");

		Theme::remindSetNewPassword($args);
	}

	function sendRemindPasswdForm($data = []) {
		UI::gora();
		Theme::remind($data);
		UI::dol();
		exit;
	}

	function sendRemindPasswdSent() {
		UI::gora();
		Theme::remindSent($data = []);
		UI::dol();
		exit;
	}

	function sendAlreadyLoggedIn() {
		UI::gora();
		// Theme::top();
		echo UI::h('h1', [], "TODO: Already logged in");
		UI::dol();
		exit;
	}

	function defaultAction() {
		UI::gora();
		// Theme::top();
		echo '<h1>ReminPasswd Tool</h1>';
		// UI::inlineJS(__FILE__ . '.example.js', [
		// 	'URL_TEST_AJAX_ACTION' => $this->getLink('testAjax'),
		// ]);
		UI::dol();
	}

	// function testAjaxAction() {
	// 	Response::sendTryCatchJson(array($this, 'testAjax'), $_REQUEST); // args from request
	// 	// Response::sendTryCatchJson(array($this, 'testAjax'), $args = 'JSON_FROM_REQUEST_BODY'); // args from json request
	// }
	// function testAjax($args) { // args given by sendTryCatchJson
	// 	$items = [
	// 		[ 'ID' => 1, 'name' => 'x', 'desc' => 'a' ],
	// 		[ 'ID' => 2, 'name' => 'y', 'desc' => 'b' ],
	// 		[ 'ID' => 3, 'name' => 'z',  'desc' => 'c' ],
	// 	];

	// 	return [
	// 		'type' => 'success',
	// 		'msg' => 'OK',
	// 		'body' => [
	// 			'items' => $items,
	// 		]
	// 	];
	// }

}