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

use SabaiApps\Directories\Component\Field\Type\AbstractStringType;
use SabaiApps\Directories\Component\Field\Type\ILabellable;
use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\Field\IField;

class ShortcodeFieldType extends AbstractStringType implements ILabellable {

	protected function _fieldTypeInfo() {
		return array(
			'label'            => __( 'WordPress Shortcode', 'directories-pro' ),
			'default_settings' => array(
				'min_length'      => null,
				'max_length'      => null,
				'char_validation' => 'none',
			),
			'icon'             => 'fab fa-wordpress',
			'schema_type'      => 'string',
		);
	}

	public function fieldTypeSettingsForm( $fieldType, Bundle $bundle, array $settings, array $parents = array(), array $rootParents = array() ) {
		$form                              = parent::fieldTypeSettingsForm( $fieldType, $bundle, $settings, $parents, $rootParents );
		$form['char_validation']['#type']  = 'hidden';
		$form['char_validation']['#value'] = 'none';
		return $form;
	}

	public function fieldLabellableLabels( IField $field, IEntity $entity ) {
		return ( $values = $entity->getFieldValue( $field->getFieldName() ) ) ? $this->_processShortcodes( $values ) : array();
	}

	public function fieldHumanReadableText( IField $field, IEntity $entity, $separator = null, $key = null ) {
		if ( ! $values = $entity->getFieldValue( $field->getFieldName() ) ) {
			return '';
		}

		return implode( isset( $separator ) ? $separator : ', ', $this->_processShortcodes( $values ) );
	}

	protected function _processShortcodes( array $shortcodes ) {
		$ret = array();
		foreach ( $shortcodes as $shortcode ) {
			$ret[] = do_shortcode( $shortcode );
		}
		return $ret;
	}
}
