<?php
namespace Dreamfox\DeliveryDate;

class TimeslotSettings
{
	private $_enabled    = false;
	private $_duration   = 2;
	private $_startTime  = '08:00';
	private $_endTime    = '20:00';
	private $_maxOrders  = 10;

	public function __construct( $data = null )
	{
		if ( $data ) {
			if ( isset( $data->enabled ) )   $this->setEnabled( $data->enabled );
			if ( isset( $data->duration ) )  $this->setDuration( $data->duration );
			if ( isset( $data->startTime ) ) $this->setStartTime( $data->startTime );
			if ( isset( $data->endTime ) )   $this->setEndTime( $data->endTime );
			if ( isset( $data->maxOrders ) ) $this->setMaxOrders( $data->maxOrders );
		}
	}

	public function getEnabled()
	{
		return (bool) $this->_enabled;
	}

	public function setEnabled( $value )
	{
		$this->_enabled = (bool) $value;
	}

	public function getDuration()
	{
		return max( 1, min( 12, (int) $this->_duration ) );
	}

	public function setDuration( $value )
	{
		$this->_duration = (int) $value;
	}

	public function getStartTime()
	{
		return $this->_startTime;
	}

	public function setStartTime( $value )
	{
		$this->_startTime = sanitize_text_field( $value );
	}

	public function getEndTime()
	{
		return $this->_endTime;
	}

	public function setEndTime( $value )
	{
		$this->_endTime = sanitize_text_field( $value );
	}

	public function getMaxOrders()
	{
		return max( 1, (int) $this->_maxOrders );
	}

	public function setMaxOrders( $value )
	{
		$this->_maxOrders = (int) $value;
	}

	public function toArray()
	{
		return array(
			'enabled'   => $this->getEnabled(),
			'duration'  => $this->getDuration(),
			'startTime' => $this->getStartTime(),
			'endTime'   => $this->getEndTime(),
			'maxOrders' => $this->getMaxOrders(),
		);
	}
}
