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

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

class TextRenderer extends AbstractRenderer {

	protected function _fieldRendererInfo() {
		return array(
			'field_types'      => array( 'text' ),
			'default_settings' => array(
				'trim'            => false,
				'trim_length'     => 200,
				'trim_marker'     => '...',
				'trim_link'       => false,
				'disallowed_tags' => array(),
				'_separator'      => ' ',
			),
		);
	}

	protected function _fieldRendererSettingsForm( IField $field, array $settings, array $parents = array() ) {
		return array(
			'trim'            => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Show excerpt', 'directories-pro' ),
				'#default_value' => ! empty( $settings['trim'] ),
			),
			'trim_length'     => array(
				'#title'         => __( 'Max number of characters', 'directories-pro' ),
				'#type'          => 'number',
				'#integer'       => true,
				'#min_value'     => 1,
				'#default_value' => $settings['trim_length'],
				'#size'          => 5,
				'#states'        => array(
					'visible' => array(
						sprintf( 'input[name="%s[trim]"]', $selector_prefix = $this->_application->Form_Form_fieldName( $parents ) ) => array(
							'type'  => 'checked',
							'value' => true,
						),
					),
				),
			),
			'trim_marker'     => array(
				'#title'         => __( 'Suffix text', 'directories-pro' ),
				'#type'          => 'textfield',
				'#default_value' => $settings['trim_marker'],
				'#size'          => 10,
				'#states'        => array(
					'visible' => array(
						sprintf( 'input[name="%s[trim]"]', $selector_prefix ) => array(
							'type'  => 'checked',
							'value' => true,
						),
					),
				),
				'#no_trim'       => true,
			),
			'trim_link'       => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Link suffix to content page', 'directories-pro' ),
				'#default_value' => ! empty( $settings['trim_link'] ),
				'#states'        => array(
					'visible' => array(
						sprintf( 'input[name="%s[trim]"]', $selector_prefix ) => array(
							'type'  => 'checked',
							'value' => true,
						),
					),
				),
			),
			'disallowed_tags' => array(
				'#type'          => 'textfield',
				'#title'         => __( 'Disallowed HTML tags', 'directories-pro' ),
				'#description'   => __( 'Enter HTML tag names separated with commas.', 'directories-pro' ),
				'#default_value' => $settings['disallowed_tags'],
				'#states'        => array(
					'visible' => array(
						sprintf( 'input[name="%s[trim]"]', $selector_prefix ) => array(
							'type'  => 'checked',
							'value' => false,
						),
					),
				),
				'#placeholder'   => 'a,img,table',
				'#separator'     => ',',
				'#alnum'         => true,
			),
		);
	}

	protected function _fieldRendererRenderField( IField $field, array &$settings, Entity\Type\IEntity $entity, array $values, ?array $context = null, $more = 0 ) {
		$ret = array();
		foreach ( array_keys( $values ) as $i ) {
			if ( ! empty( $settings['trim'] ) ) {
				$marker = isset( $settings['trim_marker'] ) ? $settings['trim_marker'] : '...';
				$ret[]  = $this->_getTrimmedContent( $values[ $i ], $settings['trim_length'], $marker, ! empty( $settings['trim_link'] ), $settings, $entity );
			} else {
				$ret[] = $this->_getContent( $values[ $i ], $settings, $entity );
			}
		}
		return isset( $settings['_separator'] ) ? implode( $settings['_separator'], $ret ) : $ret[0];
	}

	protected function _getContent( $value, array $settings, Entity\Type\IEntity $entity ) {
		$value = is_array( $value ) ? $value['value'] : $value;
		return $this->_application->Htmlize( $this->_application->Entity_Tokens_replace( $value, $entity ), false, empty( $settings['disallowed_tags'] ) ? array() : $settings['disallowed_tags'] );
	}

	protected function _getTrimmedContent( $value, $length, $marker, $link, array $settings, Entity\Type\IEntity $entity ) {
		$value = $this->_application->Entity_Tokens_replace( is_array( $value ) ? $value['value'] : $value, $entity );

		if ( ! empty( $link ) ) {
			return $this->_application->System_Text_summarize( $value, $length - $this->_application->System_MB_strlen( $marker ), '' )
				. $this->_application->Entity_Permalink(
					$entity,
					array(
						'title' => $marker,
						'class' => 'drts-trim-marker',
					)
				);
		}
		return $this->_application->System_Text_summarize( $value, $length, $marker );
	}

	protected function _fieldRendererReadableSettings( IField $field, array $settings ) {
		$ret = array(
			'trim' => array(
				'label'   => __( 'Show excerpt', 'directories-pro' ),
				'value'   => ! empty( $settings['trim'] ),
				'is_bool' => true,
			),
		);
		if ( ! empty( $settings['trim'] ) ) {
			$ret += array(
				'trim_length' => array(
					'label' => __( 'Max number of characters', 'directories-pro' ),
					'value' => $settings['trim_length'],
				),
				'trim_marker' => array(
					'label' => __( 'Suffix text', 'directories-pro' ),
					'value' => $settings['trim_marker'],
				),
				'trim_link'   => array(
					'label'   => __( 'Link suffix to content page', 'directories-pro' ),
					'value'   => ! empty( $settings['trim_link'] ),
					'is_bool' => true,
				),
			);
		} elseif ( ! empty( $settings['disallowed_tags'] ) ) {
				$ret += array(
					'disallowed_tags' => array(
						'label' => __( 'Disallowed HTML tags', 'directories-pro' ),
						'value' => implode( ',', $settings['disallowed_tags'] ),
					),
				);
		}
		return $ret;
	}
}
