<?php
namespace SabaiApps\Directories\Component\Field\Widget;

use SabaiApps\Directories\Component\Entity;
use SabaiApps\Directories\Component\Field\IField;

class TimeWidget extends AbstractWidget {

	protected function _fieldWidgetInfo() {
		return array(
			'label'            => __( 'Time picker', 'directories-pro' ),
			'field_types'      => array( 'time' ),
			'default_settings' => array(
				'current_time_selected' => false,
				'time_format'           => '',
				'time_step'             => 15,
			),
			'repeatable'       => true,
		);
	}

	public function fieldWidgetSettingsForm( $fieldType, Entity\Model\Bundle $bundle, array $settings, array $parents = array(), array $rootParents = array() ) {
		return array(
			'current_time_selected' => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Set current time selected by default', 'directories-pro' ),
				'#default_value' => ! empty( $settings['current_time_selected'] ),
			),
			'time_format'           => array(
				'#type'          => 'select',
				'#title'         => __( 'Time format', 'directories-pro' ),
				'#options'       => array(
					'' => __( 'Default', 'directories-pro' ),
					12 => sprintf( __( '%d hour', 'directories-pro' ), 12 ),
					24 => sprintf( __( '%d hour', 'directories-pro' ), 24 ),
				),
				'#default_value' => $settings['time_format'],
			),
			'time_step'             => array(
				'#type'          => 'slider',
				'#title'         => __( 'Time step interval', 'directories-pro' ),
				'#min_value'     => 5,
				'#max_value'     => 30,
				'#step'          => 5,
				'#field_suffix'  => _x( 'min', 'minutes', 'directories-pro' ),
				'#default_value' => $settings['time_step'],
			),
		);
	}

	public function fieldWidgetForm( IField $field, array $settings, $value = null, ?Entity\Type\IEntity $entity = null, array $parents = array(), $language = null ) {
		$field_settings = $field->getFieldSettings();
		return array(
			'#type'                  => 'timepicker',
			'#current_time_selected' => ! empty( $settings['current_time_selected'] ),
			'#default_value'         => $value,
			'#disable_day'           => empty( $field_settings['enable_day'] ),
			'#disable_end'           => empty( $field_settings['enable_end'] ),
			'#start_of_week'         => $this->_application->getPlatform()->getStartOfWeek(),
			'#time_format'           => isset( $settings['time_format'] ) && in_array( $settings['time_format'], array( 12, 24 ) ) ? (int) $settings['time_format'] : null,
			'#time_step'             => $settings['time_step'],
		);
	}
}
