<?php

namespace Drupal\restaurant\Date;

use Drupal\restaurant\App\Misc;

class Date
{

  //protected $db;

  protected $conn;

  protected $service;

  public function __construct()
  {
    //$this->db = new Db();
    $this->conn = \Drupal::database();
    $this->service = new Misc();
    //$this->service = \Drupal::service('restaurant.misc');
  }
	/**
	 * get a list of regions for timezones, or timezones within a specified region
	 * @param string $region
	 * @return array
	 */
	public static function getTimezones($region = '')
	{
		$list = DateTimeZone::listIdentifiers();

		if (empty ($region)) {
			$regions = array();
			foreach ($list as $key => $val) {
				if (strpos($val, '/') !== false) {
					$parts = explode('/', $val);
					$reg = $parts[0];
				} else {
					$reg = $val;
				}
				if (! in_array($reg, $regions)) {
					$regions[$reg] = $reg;
				}
			}
		} else {
			$regions = array();
			foreach ($list as $key => $val) {
				if (strpos($val, $region) !== false) {
					$str = str_replace($region, '', $val);
					$str = ltrim($str, '/');
					$regions[$val] = $str;
				}
			}
		}
		return $regions;
	}

	/**
	 * Get the date format for the jquery datepicker
	 * @param string $format
	 */
	public static function datePickerFormat($format)
	{
		$format = str_replace('d', 'dd', $format);
		$format = str_replace('m', 'mm', $format);
		$format = str_replace('Y', 'yy', $format);
		$format = str_replace('j', 'd', $format);
		return $format;
	}

	public function setTimezone()
	{
    date_default_timzeone_set($this->service->getSetting('timezone'));
	}

	public function revertTimezone()
	{
		//date_default_timezone_set(Cms_ConfigGet::get('defaultsettings/timezone'));
	}


	/**
	 * return the current time in the users timezone
	 *
	 * @param array $options
	 * @return string
	 */
	public function getTime($options = array())
	{
		list($format, $date, $dateformat, $timeformat, $br) = $this->getOptions($options);

		$date = $this->_getDate($date);
		//$settings = new Cms_Settings($this->_cid, $this->_sid);
		if (empty ($date)) {
			$date = time();
		} else {
			if (! is_int($date)) {
				$date = strtotime($date);
			}
		}
		switch ($format) {
			case 'db':
				$format = 'H:i:s';
				break;
			case 'H:i':
				$format = 'H:i';
				break;
			case 'h:ia':
				$format = 'h:ia';
				break;
			default:
				$format = $this->service->getSetting('timeformat'); //$settings->get('timeformat');

		}
		$time = date($format, $date);
		return $time;
	}

	/**
	 * Return the date & time
	 * @param $options
	 * @return string
	 */
	public function getDateTime($options = array())
	{
		list($format, $date, $dateformat, $timeformat, $br) = $this->_getOptions($options);
		$date = $this->getDate($date);
		if (($format == 'db') || ($dateformat == 'db')) {
			return $this->getDate(array('format' => 'db', 'date' => $date)) . ' ' . $this->getTime(array('format' => 'db', 'date' => $date));
		}
		return $this->getDate(array('format' => $dateformat, 'date' => $date)) . $br . $this->getTime(array('format' => $timeformat, 'date' => $date));
	}

	/**
	 * Return a date string
	 * @param string $format
	 */
	public function getDate($options = array())
	{
		list($format, $date, $dateformat, $timeformat, $br) = $this->getOptions($options);
		$format = strtolower($format);
		$date = $this->getDate($date);

		switch ($format) {
			case 'db':
				$format = 'Y-m-d';
				break;
			case 'long':
				$format = $this->service->getSetting('longdate'); //$settings->get('longdate');
				break;
			case 'medium': case '':
				$format = $this->service->getSetting('mediumdate'); //$settings->get('mediumdate');
				break;
			case 'short':
				$format = $this->service->getSetting('shortdate'); //$settings->get('shortdate');
				break;
			default:
				$format = $format;
		}
		$date = date($format, $date);
		return $date;
	}

	/**
	 * Return days of the week
	 * @return array
	 */
	public static function getDays()
	{
		return array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
	}

	/**
	 * Get an array of times 30 mins apart for a day
	 * @return multitype:NULL
	 */
	public static function getTimes()
	{
		$times = array();
		$time = strtotime("00:00");
		$times[] = date("h:ia",$time);
		for($i = 1;$i < 48;$i++) {
			$time = strtotime("+ 30 minutes",$time);
			$times[] = date("h:ia",$time);
		}
		return $times;
	}

	/**
	 * Return timestamp from supplied date
	 * @param string|int $date
	 * @return int
	 */
	private function _getDate($date)
	{
		if (empty ($date)) {
			$date = time();
		} else {
			if (! is_int($date)) {
				$date = strtotime($date);
			}
		}
		return $date;
	}

	/**
	 * Return an array of options
	 *
	 * @param unknown_type $options
	 * @return multitype:string Ambigous <multitype:>
	 */
	private function getOptions($options)
	{
		if (! is_array($options)) {
			$options = array('format' => $options);
		}
		$return = array();
		$return[] = (isset($options['format']))? $options['format'] : '';
		$return[] = (isset($options['date'])) ? $options['date'] : '';
		$return[] = (isset($options['dateformat'])) ? $options['dateformat'] : '';
		$return[] = (isset($options['timeformat'])) ? $options['timeformat'] : '';
		$return[] = (isset($options['br'])) ? $options['br'] : ' ';
		return $return;
	}
}
