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

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

class DateRenderer extends Field\Renderer\AbstractRenderer {

	protected function _fieldRendererInfo() {
		return array(
			'field_types'      => array( $this->_name ),
			'default_settings' => array(
				'format'        => 'date',
				'custom_format' => '',
				'_separator'    => ', ',
			),
			'inlineable'       => true,
		);
	}

	protected function _fieldRendererSettingsForm( Field\IField $field, array $settings, array $parents = array() ) {
		return array(
			'format'        => array(
				'#type'          => 'select',
				'#title'         => __( 'Date/time format', 'directories-pro' ),
				'#options'       => $this->_getDateTimeFormatOptions(),
				'#default_value' => $settings['format'],
			),
			'custom_format' => array(
				'#type'          => 'textfield',
				'#required'      => function ( $form ) use ( $parents ) {
					return $form->getValue( array_merge( $parents, array( 'format' ) ) ) === 'custom'; },
				'#placeholder'   => 'Y-m-d',
				'#default_value' => $settings['custom_format'],
				'#description'   => __( 'Enter the data/time format string suitable for input to PHP date() function.', 'directories-pro' ),
				'#states'        => array(
					'visible' => array(
						sprintf( '[name="%s[format]"]', $this->_application->Form_Form_fieldName( $parents ) ) => array( 'value' => 'custom' ),
					),
				),
			),
		);
	}

	protected function _getDateTimeFormatOptions() {
		return array(
			'datetime' => __( 'Show date/time', 'directories-pro' ),
			'date'     => __( 'Show date', 'directories-pro' ),
			'custom'   => __( 'Custom date format', 'directories-pro' ),
		);
	}

	protected function _fieldRendererRenderField( Field\IField $field, array &$settings, Entity\Type\IEntity $entity, array $values, ?array $context = null, $more = 0 ) {
		$ret = array();
		foreach ( $values as $value ) {
			$rendered = $this->_renderField( $field, $settings, $entity, $value );
			if ( strlen( $rendered ) ) {
				$ret[] = $rendered;
			}
		}
		return implode( $settings['_separator'], $ret );
	}

	protected function _renderField( Field\IField $field, array &$settings, Entity\Type\IEntity $entity, $value ) {
		switch ( $settings['format'] ) {
			case 'custom':
				return $this->_application->System_Date( $value, true, $settings['custom_format'] );
			case 'datetime':
				return $this->_application->System_Date_datetime( $value, true );
			case 'date':
			default:
				return $this->_application->System_Date( $value, true );
		}
	}

	protected function _fieldRendererReadableSettings( Field\IField $field, array $settings ) {
		$format_options = $this->_getDateTimeFormatOptions();
		$ret            = array(
			'format' => array(
				'label' => __( 'Date/time format', 'directories-pro' ),
				'value' => isset( $format_options[ $settings['format'] ] ) ? $format_options[ $settings['format'] ] : __( 'Show date', 'directories-pro' ),
			),
		);
		if ( $settings['format'] === 'custom' ) {
			$ret['custom_format'] = array(
				'label' => __( 'Custom date format', 'directories-pro' ),
				'value' => $settings['custom_format'],
			);
		}

		return $ret;
	}
}
