<?php
namespace SabaiApps\Directories\Component\Display\Element;

use SabaiApps\Directories\Component\Entity;
use SabaiApps\Directories\Component\Display;
use SabaiApps\Directories\Context;

class TextElement extends AbstractElement {

	protected function _displayElementInfo( Entity\Model\Bundle $bundle ) {
		return array(
			'type'             => 'content',
			'label'            => _x( 'Text', 'display element name', 'directories-pro' ),
			'description'      => __( 'Adds a block of custom text', 'directories-pro' ),
			'default_settings' => array(
				'text'    => null,
				'htmlize' => true,
			),
			'icon'             => 'fas fa-bars',
			'designable'       => array( 'margin', 'padding', 'font' ),
		);
	}

	public function displayElementSettingsForm( Entity\Model\Bundle $bundle, array $settings, Display\Model\Display $display, array $parents = array(), $tab = null, $isEdit = false, array $submitValues = array() ) {
		return array(
			'text'    => array(
				'#title'                 => __( 'Text content', 'directories-pro' ),
				'#type'                  => 'textarea',
				'#horizontal'            => true,
				'#default_value'         => $settings['text'],
				'#required'              => true,
				'#description'           => $display->type === 'entity' ? $this->_application->System_Util_availableTags(
					$this->_application->Entity_Tokens(
						$bundle,
						true,
						array(
							'site'           => true,
							'current_user'   => true,
							'auto_increment' => true,
						)
					)
				) : null,
				'#description_no_escape' => true,
			),
			'htmlize' => array(
				'#title'         => __( 'Auto-convert to HTML', 'directories-pro' ),
				'#type'          => 'checkbox',
				'#default_value' => ! empty( $settings['htmlize'] ),
				'#horizontal'    => true,
			),
		);
	}

	protected function _getTextFormType() {
		if ( $this->_application->isComponentLoaded( 'WordPress' ) ) {
			return 'wp_editor';
		}
		return 'text';
	}

	public function displayElementRender( Entity\Model\Bundle $bundle, array $element, $var, ?array $context = null ) {
		$text = $this->_translateString( $element['settings']['text'], $bundle->name, $element['display'], $element['_element_id'], 'text' );
		if ( $var instanceof Entity\Type\IEntity ) {
			$text = $this->_application->Entity_Tokens_replace( $text, $var, true, array( 'escape' => false ) );
		}

		if ( ! isset( $element['settings']['htmlize'] ) || $element['settings']['htmlize'] ) {
			$text = $this->_application->Htmlize( $text );
		}
		return $text;
	}

	public function displayElementTitle( Entity\Model\Bundle $bundle, array $data ) {
		return $this->_application->H( $data['settings']['text'] );
	}

	protected function _displayElementReadableInfo( Entity\Model\Bundle $bundle, Display\Model\Element $element ) {
		$settings = $element->data['settings'];
		$ret      = array(
			'text' => array(
				'label' => __( 'Text content', 'directories-pro' ),
				'value' => $settings['text'],
			),
		);
		return array( 'settings' => array( 'value' => $ret ) );
	}

	public function displayElementOnSaved( Entity\Model\Bundle $bundle, Display\Model\Element $element ) {
		$this->_registerString( $element->data['settings']['text'], $bundle->name, $element->Display->name, $element->element_id, 'text' );
	}
}
