<?php

// No direct access
defined('_JEXEC') or die;


// Include the syndicate functions only once
JLoader::register('ModEmergencyConsultantHelper', __DIR__ . '/helper.php');
$userid = JFactory::getUser()->get('id', '0');

$document = JFactory::getDocument();
//$document->addStyleSheet('modules/mod_emergencyconsultant/tmpl/assets/css/bootstrap.rtl.min.css');
//$document->addScript('modules/mod_emergencyconsultant/tmpl/assets/js/bootstrap.min.js');

$user = jfactory::getuser();
if ((!file_exists(JPATH_SITE . '/libraries/CBLib/CBLib/Core/CBLib.php')) || (!file_exists(JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php')))
{
	echo 'CB not installed';
	exit();
}
else
{
	include_once(JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php');

	$current_user_id = $user->get('id', 0, 'int');

	$cbUser  = &CBuser::getInstance((int) $current_user_id);
	$cb_user = $cbUser->getUserData();
}

$app    = jfactory::getapplication();
$input  = $app->input;
$step_0 = $input->get('step_0', null);
$do     = $input->get('do', null);
// $sendto= $input->get('sendto', 0);
// if ($sendto)
// {
// echo '<pre>';
// var_dump('its ok');
// var_dump($sendto);
// echo '</pre>';
// exit();
// }

$emergency_step = (int) $cb_user->get('cb_emergency_step', 0);

if ( (isset($step_0) && $emergency_step != 0) or (!isset($do) && !isset($step_0) && $emergency_step != 0) )
{
    $res= ModEmergencyConsultantHelper::is_time_over_reset_it($cb_user);
     if ($res) $emergency_step= 0;
}

if (isset($do))
{
	$selectedConsultant = $input->get('selectedConsultant', 0, 'STRING');
	ModEmergencyConsultantHelper::do_step2($do, $cb_user, $selectedConsultant);
}

if (isset($step_0) && $emergency_step == 0)
{
	ModEmergencyConsultantHelper::do_step0($input, $cb_user);
}

$emergency_step = ModEmergencyConsultantHelper::calc_step($cb_user);

use Joomla\CMS\Date\Date;
switch ($emergency_step)
{
	case 0:
        $nextStepButtonCssClass = 'button-eram-next';
        $is15MinPastSinceUserLastEmrgReq = true;
		if ($cb_user->cb_emergency_request_time)
		{
            $cb_user_emrg_creat_datetime = new DateTime(date('Y-m-d H:i:s', $cb_user->cb_emergency_request_time));
            $config = JFactory::getConfig();
            $configTz       = $config->get('offset');
            $configTzObj    = new DateTimeZone($configTz);
            $cb_user_emrg_creat_datetime->setTimezone($configTzObj);
            $cb_user_emrg_creat_datetime_plus_15_min = $cb_user_emrg_creat_datetime->modify("+15 minutes");

            $currentTime = new Date('now', $configTzObj);
			if ($currentTime < $cb_user_emrg_creat_datetime) {
                $nextStepButtonCssClass = 'button-eram-next-disabled';
			}

            $is15MinPastSinceUserLastEmrgReq = boolval($currentTime > $cb_user_emrg_creat_datetime);
			$cb_user_emrg_creat_date_plus_15min = $cb_user_emrg_creat_datetime_plus_15_min->format('Y-m-d');
			$cb_user_emrg_creat_time_plus_15min = $cb_user_emrg_creat_datetime_plus_15_min->format('H:i');
			$document->addStyleSheet(JUri::base() . 'modules/mod_emergencyconsultant/tmpl/assets/css/countdown.css');
			$document->addScript(JUri::base() . 'modules/mod_emergencyconsultant/tmpl/assets/js/countdown.js');
		}
		$topics = ModEmergencyConsultantHelper::get_topics($cb_user);
		require JModuleHelper::getLayoutPath('mod_emergencyconsultant', 'default');
		break;
	case 1:
		$configTz       = JFactory::getConfig()->get('offset');
		$configTzObj    = new DateTimeZone($configTz);
		$currentTime = new Date('now +15 minutes', $configTzObj);
		$countdownDateTime = $currentTime->format(DateTime::RFC3339_EXTENDED, true);

		$document->addStyleSheet(JUri::base() . 'modules/mod_emergencyconsultant/tmpl/assets/css/countdown.css');
		$document->addScript(JUri::base() . 'modules/mod_emergencyconsultant/tmpl/assets/js/countdown.js');
		require JModuleHelper::getLayoutPath('mod_emergencyconsultant', 'step_1');
		break;
	case 2:
		list($emergency_consultant_id, $emergency_consultant_plan) = ModEmergencyConsultantHelper::emergency_consultant($cb_user);
		require JModuleHelper::getLayoutPath('mod_emergencyconsultant', 'step_2');
		break;
}

