<?php
namespace SabaiApps\Directories\Component\WordPressContent\FieldRenderer;

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

class TextFieldRenderer extends Field\Renderer\TextRenderer {

	protected function _fieldRendererInfo() {
		$ret                      = parent::_fieldRendererInfo();
		$ret['default_settings'] += array(
			'shortcode'            => false,
			'disable_excerpt_more' => true,
		);
		return $ret;
	}

	protected function _fieldRendererSettingsForm( Field\IField $field, array $settings, array $parents = array() ) {
		$ret              = parent::_fieldRendererSettingsForm( $field, $settings, $parents );
		$ret['shortcode'] = array(
			'#type'          => 'checkbox',
			'#title'         => __( 'Process shortcode(s)', 'directories-pro' ),
			'#default_value' => ! empty( $settings['shortcode'] ),
			'#states'        => array(
				'visible' => array(
					sprintf( 'input[name="%s[trim]"]', $field_name_prefix = $this->_application->Form_Form_fieldName( $parents ) ) => array(
						'type'  => 'checked',
						'value' => false,
					),
				),
			),
		);
		$ret['disable_excerpt_more'] = array(
			'#type'          => 'checkbox',
			'#title'         => __( 'Disable excerpt_more filter', 'directories-pro' ),
			'#default_value' => ! empty( $settings['disable_excerpt_more'] ),
			'#states'        => array(
				'visible' => array(
					sprintf( 'input[name="%s[trim]"]', $field_name_prefix ) => array(
						'type'  => 'checked',
						'value' => true,
					),
				),
			),
		);
		$visible_state_key           = sprintf( 'input[name="%s[disable_excerpt_more]"]', $field_name_prefix );
		$ret['trim_link']['#states']['visible'][ $visible_state_key ] = $ret['trim_marker']['#states']['visible'][ $visible_state_key ] = array(
			'type'  => 'checked',
			'value' => true,
		);
		return $ret;
	}

	protected function _getContent( $value, array $settings, Entity\Type\IEntity $entity ) {
		$value = is_array( $value ) ? $value['value'] : $value;
		if ( empty( $settings['shortcode'] ) ) {
			$value = strip_shortcodes( $value );
		}

		return parent::_getContent( $value, $settings, $entity );
	}

	protected function _getTrimmedContent( $value, $length, $marker, $link, array $settings, Entity\Type\IEntity $entity ) {
		$value = is_array( $value ) ? $value['value'] : $value;
		// Add WordPress trim marker
		$marker = ' ' . '[&hellip;]'; // set default trim marker of WordPress
		if ( isset( $settings['disable_excerpt_more'] )
			&& ! $settings['disable_excerpt_more']
		) {
			$marker = apply_filters( 'excerpt_more', $marker );
		} else {
			if ( isset( $settings['trim_marker'] ) ) {
				$marker = $settings['trim_marker'];
			}
			$link = ! empty( $settings['trim_link'] );
		}

		return parent::_getTrimmedContent( strip_shortcodes( $value ), $length, $marker, $link, $settings, $entity );
	}

	protected function _fieldRendererReadableSettings( Field\IField $field, array $settings ) {
		$ret = (array) parent::_fieldRendererReadableSettings( $field, $settings );
		if ( ! empty( $ret['trim'] ) ) {
			$ret += array(
				'disable_excerpt_more' => array(
					'label'   => __( 'Disable excerpt_more filter', 'directories-pro' ),
					'value'   => ! empty( $settings['disable_excerpt_more'] ),
					'is_bool' => true,
				),
			);
			unset( $ret['trim_marker'], $ret['trim_link'] );
		} else {
			$ret += array(
				'shortcode' => array(
					'label'   => __( 'Process shortcode(s)', 'directories-pro' ),
					'value'   => ! empty( $settings['shortcode'] ),
					'is_bool' => true,
				),
			);
		}
		return $ret;
	}
}
