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

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

class JavaScriptElement extends AbstractElement {

	protected function _displayElementInfo( Bundle $bundle ) {
		return array(
			'type'             => 'content',
			'label'            => _x( 'JavaScript', 'display element name', 'directories-pro' ),
			'description'      => __( 'Renders raw JavaScript code', 'directories-pro' ),
			'default_settings' => array(
				'js'      => '',
				'no_tags' => false,
			),
			'designable'       => false,
			'icon'             => 'far fa-file-code',
			'headingable'      => false,
		);
	}

	public function displayElementSettingsForm( Bundle $bundle, array $settings, Display\Model\Display $display, array $parents = array(), $tab = null, $isEdit = false, array $submitValues = array() ) {
		$desc = $this->_application->H( __( 'Enter raw JavaScript code without <script> tags.', 'directories-pro' ) );
		if ( $display->type === 'entity' ) {
			$desc .= ' ' . $this->_application->System_Util_availableTags(
				$this->_application->Entity_Tokens(
					$bundle,
					true,
					array(
						'site'           => true,
						'current_user'   => true,
						'auto_increment' => true,
					)
				)
			);
		}
		return array(
			'js'      => array(
				'#type'                  => 'editor',
				'#language'              => 'javascript',
				'#default_value'         => $settings['js'],
				'#required'              => true,
				'#description'           => $desc,
				'#description_no_escape' => true,
			),
			'no_tags' => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Do not add <script> tags', 'directories-pro' ),
				'#default_value' => ! empty( $settings['no_tags'] ),
				'#horizontal'    => true,
			),
		);
	}

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

		if ( empty( $element['settings']['no_tags'] ) ) {
			$js = '<script type="text/javascript">' . $js . '</script>';
		}
		return array( 'raw' => $js );
	}

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

	protected function _displayElementReadableInfo( Bundle $bundle, Display\Model\Element $element ) {
		$settings = $element->data['settings'];
		$ret      = array(
			'js'      => array(
				'label'   => 'JavaScript',
				'value'   => '<code>' . $this->_application->H( $settings['js'] ) . '</code>',
				'is_html' => true,
			),
			'no_tags' => array(
				'label'   => __( 'Do not add <script> tags', 'directories-pro' ),
				'value'   => ! empty( $settings['no_tags'] ),
				'is_bool' => true,
			),
		);
		return array( 'settings' => array( 'value' => $ret ) );
	}
}
