<?php

namespace App\ImportedOrder;

/**
 * Stores info about a date and a time. Any combination is possible or any of the field may be missing.
 * The object  may contain only vague details (eg. "date in june, a weekend, with the time in the
 * morning" or "specific date, with the time between 13:00 and 20:00"). Both the date and the time
 * may be specified as an interval (extra details regarding them can be stored in the text fields).
 * @author Gabriel
 */
class DateTime extends EntityAbstract
{

	/**
	 * The start date.
	 * @var String - ISO date.
	 */
	public $dateStart;

	/**
	 * The end date.
	 * @var String - ISO date.
	 */
	public $dateEnd;

	/**
	 * The date details - text field (contains any manually inputted info).
	 * @var String
	 */
	public $dateDetails;

	/**
	 * The start time - HH:ii format
	 * @var String
	 */
	public $timeStart;

	/**
	 * The end time - HH:ii format
	 * @var String
	 */
	public $timeEnd;

	/**
	 * The time details - text field (contains any manually inputted info).
	 * @var String
	 */
	public $timeDetails;


	/**
	 * (non-PHPdoc)
	 * @see \App\ImportedOrder\EntityAbstract::validateEntityFields()
	 */
	protected function validateEntityFields(\App\MessagesCollector $messagesCollector): void
	{

		// check date start
		if ($this->dateStart and !validDate($this->dateStart)) {
			$messagesCollector->addInfo('The date start range is not valid.');
		}

		// check time start
		if ($this->timeStart and !\Engine\Validator::set($this->timeStart)->isTime()) {
			$messagesCollector->addInfo('The time start range is not valid.');
		}

		// check date end
		if ($this->dateEnd and !validDate($this->dateEnd)) {
			$messagesCollector->addInfo('The date end range is not valid.');
		}

		// check time end
		if ($this->timeEnd and !\Engine\Validator::set($this->timeEnd)->isTime()) {
			$messagesCollector->addInfo('The time end range is not valid.');
		}

	}


	/**
	 * Extract from an object \Engine\DateTime information
	 * @param \Engine\DateTime $dateTime
	 */
	public function importFromDateTime(\Engine\DateTime $dateTime)
	{
		if ($dateTime->isValid() and !$dateTime->isEmpty()) {
			$date = $dateTime->formatDateIso();
			$time = $dateTime->formatTimeIso();

			if ($date) {
				$this->dateStart = 	$date;
				$this->dateEnd =	$date;
			}

			if ($time) {
				$this->timeStart = 	$time;
				$this->timeEnd  =	$time;
			}
		}
	}

	/**
	 * Matches an exact date (if start and/or end dates are set and consistent).
	 * @return string - ISO date
	 */
	public function getMatchedDate()
	{
		// compose date from start/end
		if ($this->dateStart and $this->dateEnd) {
			if ($this->dateStart==$this->dateEnd) {
				$return = $this->dateStart;
			} else {
				$return = '';
			}
		} elseif ($this->dateStart) {
			$return = $this->dateStart;
		} elseif ($this->dateEnd) {
			$return = $this->dateEnd;
		} else {
			$return = '';
		}

		// clean date if it is not valid
		if (!validDate($return)) {
			$return = '';
		}

		return $return;

	}

	/**
	 * Matches the exact time from the received interval
	 * @return String - HH:ii time format
	 */
	public function getMatchedTime()
	{
		// compose date from start/end
		if ($this->timeStart and $this->timeEnd) {
			if ($this->timeStart==$this->timeEnd) {
				$return = $this->timeStart;
			} else {
				$return = '';
			}
		} elseif ($this->timeStart) {
			$return = $this->timeStart;
		} elseif ($this->timeEnd) {
			$return = $this->timeEnd;
		} else {
			$return = '';
		}

		// clean time if it is not valid
		if (!\Engine\Validator::set($return)->isTime()) {
			$return = '';
		}

		return $return;
	}

}
