<?php
namespace SabaiApps\Directories\Component\DirectoryPro\FieldWidget;

use SabaiApps\Directories\Component\Entity;
use SabaiApps\Directories\Component\Field\IField;
use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\Field\Widget\TimeWidget;

class OpeningHoursFieldWidget extends TimeWidget {

	protected function _fieldWidgetInfo() {
		$info                      = parent::_fieldWidgetInfo();
		$info['label']             = __( 'Opening Hours', 'directories-pro' );
		$info['field_types']       = array( $this->_name );
		$info['default_settings'] += array(
			'range' => null,
		);
		return $info;
	}

	public function fieldWidgetSettingsForm( $fieldType, Entity\Model\Bundle $bundle, array $settings, array $parents = array(), array $rootParents = array() ) {
		$form = parent::fieldWidgetSettingsForm( $fieldType, $bundle, $settings, $parents, $rootParents );
		unset( $form['current_time_selected'] );

		$form['range'] = array(
			'#title'         => __( 'Limit time range', 'directories-pro' ),
			'#type'          => 'timepicker',
			'#default_value' => $settings['range'],
			'#disable_day'   => true,
			'#disable_end'   => false,
			'#time_format'   => null,
			'#time_step'     => 30,
			'#multiple'      => true,
		);

		return $form;
	}

	public function fieldWidgetForm( IField $field, array $settings, $value = null, ?IEntity $entity = null, array $parents = array(), $language = null ) {
		$form = array(
			'#current_time_selected' => false,
			'#default_value'         => $value,
			'#disable_day'           => false,
			'#disable_end'           => false,
			'#allow_empty_day'       => false,
			'#all_day_options'       => array(
				'closed'      => _x( 'Closed', 'opening hours', 'directories-pro' ),
				'appointment' => __( 'Appointment only', 'directories-pro' ),
			),
			'#enable_day_bulk'       => true,
			'#time_range_min'        => isset( $settings['range']['start'] ) ? (int) $settings['range']['start'] : null,
			'#time_range_max'        => isset( $settings['range']['end'] ) ? (int) $settings['range']['end'] : null,
		);

		return $form + parent::fieldWidgetForm( $field, $settings, $value, $entity, $parents, $language );
	}
}
