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

use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\Display\Model\Display;
use SabaiApps\Directories\Component\Display\Model\Element;
use SabaiApps\Directories\Context;

class TabElement extends AbstractElement {

	protected function _displayElementInfo( Bundle $bundle ) {
		return array(
			'type'                => 'utility',
			'label'               => $label = _x( 'Tab', 'display element name', 'directories-pro' ),
			'default_settings'    => array(
				'label'      => $label,
				'hash'       => null,
				'hide_empty' => true,
			),
			'containable'         => true,
			'parent_element_name' => 'tabs',
			'icon'                => 'far fa-folder',
			'headingable'         => false,
			'designable'          => array( 'padding' ),
		);
	}

	public function displayElementSettingsForm( Bundle $bundle, array $settings, Display $display, array $parents = array(), $tab = null, $isEdit = false, array $submitValues = array() ) {
		return array(
			'label'      => array(
				'#title'                 => __( 'Tab label', 'directories-pro' ),
				'#type'                  => 'textfield',
				'#default_value'         => $settings['label'],
				'#horizontal'            => true,
				'#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,
			),
			'hash'       => array(
				'#title'         => __( 'URL hash', 'directories-pro' ),
				'#type'          => 'textfield',
				'#default_value' => $settings['hash'],
				'#horizontal'    => true,
				'#field_prefix'  => '#',
				'#description'   => __( 'Enter a custom URL hash or leave empty for default.', 'directories-pro' ),
			),
			'hide_empty' => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Hide if empty', 'directories-pro' ),
				'#default_value' => ! empty( $settings['hide_empty'] ),
				'#horizontal'    => true,
			),
		);
	}

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

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

	public function displayElementRender( Bundle $bundle, array $element, $var, ?array $context = null ) {
		$force_output = isset( $element['settings']['hide_empty'] ) && empty( $element['settings']['hide_empty'] );
		if ( ! $html = $this->_renderChildren( $bundle, $element['children'], $var, $context ) ) {
			if ( ! $force_output ) {
				return;
			}

			$html = '';
		} else {
			$html = implode( PHP_EOL, $html );
		}

		return array(
			'html'         => $html,
			'force_output' => $force_output,
		);
	}
}
