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

use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\Display\Model\Display;
use SabaiApps\Directories\Context;

class HtmlElement extends AbstractElement {

	protected function _displayElementInfo( Bundle $bundle ) {
		return array(
			'type'             => 'content',
			'label'            => _x( 'HTML', 'display element name', 'directories-pro' ),
			'description'      => __( 'Renders raw HTML', 'directories-pro' ),
			'default_settings' => array(
				'html' => '',
			),
			'icon'             => 'fas fa-code',
			'designable'       => array( 'margin', 'padding' ),
		);
	}

	public function displayElementSettingsForm( Bundle $bundle, array $settings, Display $display, array $parents = array(), $tab = null, $isEdit = false, array $submitValues = array() ) {
		return array(
			'html' => array(
				'#type'                  => 'editor',
				'#language'              => 'xml',
				'#default_value'         => $settings['html'],
				'#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,
			),
		);
	}

	public function displayElementRender( Bundle $bundle, array $element, $var, ?array $context = null ) {
		$html = $element['settings']['html'];
		if ( $var instanceof IEntity
			&& strlen( $html )
		) {
			$html = $this->_application->Entity_Tokens_replace( $html, $var, true );
		}
		return $html;
	}

	public function displayElementTitle( Bundle $bundle, array $element ) {
		return $this->_application->H( $element['settings']['html'] );
	}

	public function displayElementReadableSettings( Bundle $bundle, array $settings ) {
		$ret = array(
			'html' => array(
				'label'   => 'HTML',
				'value'   => '<code>' . $this->_application->H( $settings['html'] ) . '</code>',
				'is_html' => true,
			),
		);
		return array( 'settings' => array( 'value' => $ret ) );
	}
}
