<?php

use Joomla\CMS\HTML\HTMLHelper;

defined('_JEXEC') or die('Restricted access');

abstract class CPollHelper
{
    public static function getDateSelection($startDate='', $endDate='')
    {
		if (empty($startDate)) $startDate = JDate::getInstance( '00:01' );
		if (empty($endDate))   $endDate   = JDate::getInstance( '23:59' );

		$startAmPmSelect = "";
		$endAmPmSelect = "";
		$hours = array();

		$config		= CFactory::getConfig();

		if($config->get('pollshowampm'))
		{
			for($i = 1; $i <= 12; $i++)
			{
				$hours[] = HTMLHelper::_('select.option',  $i, "$i" );
			}

			// Cannot user ->Format('%p') since it is dependent on current locale
			// and would return a null if the system is configured for 24H
			$startAmPm 		= $startDate->format('H') >= 12 ? 'PM' : 'AM';
			$endAmPm		= $endDate->format('H') >= 12 ? 'PM' : 'AM';

			$amPmSelect		= array();
			$amPmSelect[]		= HTMLHelper::_('select.option',  'AM', "am" );
			$amPmSelect[]		= HTMLHelper::_('select.option',  'PM', "pm" );

			$startAmPmSelect	= HTMLHelper::_('select.genericlist',  $amPmSelect , 'starttime-ampm', array('class'=>'required input-mini'), 'value', 'text', $startAmPm , false );
			$endAmPmSelect		= HTMLHelper::_('select.genericlist',  $amPmSelect , 'endtime-ampm', array('class'=>'required input-mini'), 'value', 'text', $endAmPm , false );

			$selectedStartHour 	= intval($startDate->format('g'));
			$selectedEndHour 	= intval($endDate->format('g'));
		}
		else
		{
			for($i = 0; $i <= 23; $i++)
			{
				$hours[] = HTMLHelper::_('select.option',  $i, sprintf( "%02d" ,$i) );
			}

			$selectedStartHour 	= intval($startDate->Format('H'));
			$selectedEndHour 	= intval($endDate->Format('H'));
		}
		$startHourSelect		= HTMLHelper::_('select.genericlist',  $hours, 'starttime-hour', array('class'=>'required input-mini'), 'value', 'text', $selectedStartHour , false );
		$endHourSelect			= HTMLHelper::_('select.genericlist',  $hours, 'endtime-hour', array('class'=>'required input-mini'), 'value', 'text', $selectedEndHour , false );

		$minutes	= array();
		$minutes[]	= HTMLHelper::_('select.option',  0, "00" );
		$minutes[]	= HTMLHelper::_('select.option',  15, "15" );
		$minutes[]	= HTMLHelper::_('select.option',  30, "30" );
		$minutes[] 	= HTMLHelper::_('select.option',  45, "45" );

		$startMinSelect		= HTMLHelper::_('select.genericlist',  $minutes , 'starttime-min', array('class'=>'required input-mini'), 'value', 'text', $startDate->Format('i') , false );
		$endMinSelect		= HTMLHelper::_('select.genericlist',  $minutes , 'endtime-min', array('class'=>'required input-mini'), 'value', 'text', $endDate->Format('i' ) , false );

		$html = new stdClass();
		$html->startDate = $startDate;
		$html->endDate   = $endDate;
		$html->startHour = $startHourSelect;
		$html->endHour   = $endHourSelect;
		$html->startMin  = $startMinSelect;
		$html->endMin    = $endMinSelect;
		$html->startAmPm = $startAmPmSelect;
		$html->endAmPm   = $endAmPmSelect;

		return $html;
	}
}