<?php

namespace SugarCalendarBookings\Availability;

use DateTimeZone;
use SugarCalendarBookings\Employees\EmployeeModel;

/**
 * Working period rule.
 *
 * Stored in original time zone.
 * Original time zone is the time zone in which working schedule is defined.
 *
 * @since 1.0.0
 */
class WorkingPeriodRule {

	/**
	 * Start time.
	 *
	 * @since 1.0.0
	 *
	 * @var int
	 */
	private $start;

	/**
	 * End time.
	 *
	 * @since 1.0.0
	 *
	 * @var int
	 */
	private $end;

	/**
	 * Time zone.
	 *
	 * The time zone in which the working period is defined.
	 *
	 * @since 1.0.0
	 *
	 * @var DateTimeZone Time zone.
	 */
	private $time_zone;

	/**
	 * Employee model.
	 *
	 * The owner of the working period.
	 *
	 * @since 1.0.0
	 *
	 * @var EmployeeModel
	 */
	private $employee;

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param int           $start     Start time.
	 * @param int           $end       End time.
	 * @param DateTimeZone  $time_zone Time zone.
	 * @param EmployeeModel $employee  Employee model.
	 */
	public function __construct( $start, $end, $time_zone, $employee ) {

		$this->start     = $start;
		$this->end       = $end;
		$this->time_zone = $time_zone;
		$this->employee  = $employee;
	}

	/**
	 * Get start time.
	 *
	 * @since 1.0.0
	 *
	 * @return int Start time.
	 */
	public function start() {

		return $this->start;
	}

	/**
	 * Get end time.
	 *
	 * @since 1.0.0
	 *
	 * @return int End time.
	 */
	public function end() {

		return $this->end;
	}

	/**
	 * Get time zone.
	 *
	 * @since 1.0.0
	 *
	 * @return DateTimeZone Time zone.
	 */
	public function get_time_zone() {

		return $this->time_zone;
	}

	/**
	 * Get employee model.
	 *
	 * @since 1.0.0
	 *
	 * @return EmployeeModel Employee model.
	 */
	public function get_employee() {

		return $this->employee;
	}

	/**
	 * Create a working period from an array.
	 *
	 * @since 1.0.0
	 *
	 * @param array         $data      Data.
	 * @param DateTimeZone  $time_zone Time zone.
	 * @param EmployeeModel $employee  Employee model.
	 *
	 * @return WorkingPeriodRule|null Working period.
	 */
	public static function from_array( $data, $time_zone, $employee ) {

		if (
			! isset( $data['start'], $data['end'] ) ||
			! is_numeric( $data['start'] ) ||
			! is_numeric( $data['end'] )
		) {
			return null;
		}

		return new self( (int) $data['start'], (int) $data['end'], $time_zone, $employee );
	}
}
