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

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

class GroupElement extends AbstractElement {

	protected function _displayElementInfo( Entity\Model\Bundle $bundle ) {
		return array(
			'type'             => 'utility',
			'label'            => _x( 'Group', 'display element name', 'directories-pro' ),
			'description'      => __( 'Group multiple display elements', 'directories-pro' ),
			'default_settings' => array(
				'inline'           => false,
				'separator'        => null,
				'separator_margin' => true,
				'nowrap'           => false,
			),
			'containable'      => true,
			'icon'             => 'far fa-object-group',
			'designable'       => array( 'margin', 'padding', 'font', 'z-index' ),
		);
	}

	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(
			'inline'           => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Display inline', 'directories-pro' ),
				'#default_value' => ! empty( $settings['inline'] ),
				'#horizontal'    => true,
			),
			'separator'        => array(
				'#type'          => 'textfield',
				'#title'         => __( 'Element separator', 'directories-pro' ),
				'#default_value' => false !== strpos( $settings['separator'], '&nbsp;' ) ? strtr( $settings['separator'], array( '&nbsp;' => '&amp;nbsp;' ) ) : $settings['separator'],
				'#horizontal'    => true,
				'#no_trim'       => true,
			),
			'separator_margin' => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Add horizontal margin to separator', 'directories-pro' ),
				'#default_value' => ! isset( $settings['separator_margin'] ) || $settings['separator_margin'],
				'#horizontal'    => true,
				'#states'        => $inline_separator_states = array(
					'visible' => array(
						sprintf( 'input[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'inline' ) ) ) ) => array(
							'type'  => 'checked',
							'value' => true,
						),
						sprintf( 'input[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'separator' ) ) ) ) => array(
							'type'  => 'filled_no_trim',
							'value' => true,
						),
					),
				),
			),
			'nowrap'           => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Do not wrap elements', 'directories-pro' ),
				'#default_value' => $settings['nowrap'],
				'#horizontal'    => true,
				'#states'        => $inline_separator_states,
			),
		);
	}

	public function displayElementRender( Entity\Model\Bundle $bundle, array $element, $var, ?array $context = null ) {
		if ( ! $html = $this->_renderChildren( $bundle, $element['children'], $var, $context ) ) {
			return;
		}

		$settings        = $element['settings'];
		$separator       = PHP_EOL;
		$separator_class = 'drts-display-group-element-separator';
		if ( ! empty( $settings['inline'] ) ) {
			$class = 'drts-display-group-inline';
			if ( ! empty( $settings['nowrap'] ) ) {
				$class .= ' drts-display-group-inline-nowrap';
			}
			if ( isset( $settings['separator_margin'] )
				&& ! $settings['separator_margin']
			) {
				$separator_class .= ' ' . DRTS_BS_PREFIX . 'mx-0';
			}
			$separator = '<div class="' . $separator_class . '">' . $settings['separator'] . '</div>';
		} else {
			$class = '';
			if ( strlen( $settings['separator'] ) ) {
				$separator = '<div class="' . $separator_class . '">' . $settings['separator'] . '</div>';
			}
		}

		return array(
			'html'  => implode( $separator, $html ),
			'style' => '',
			'class' => $class,
		);
	}

	protected function _displayElementReadableInfo( Entity\Model\Bundle $bundle, Display\Model\Element $element ) {
		$settings = $element->data['settings'];
		$ret      = array(
			'inline'           => array(
				'label'   => __( 'Display inline', 'directories-pro' ),
				'value'   => ! empty( $settings['inline'] ),
				'is_bool' => true,
			),
			'separator'        => array(
				'label' => __( 'Element separator', 'directories-pro' ),
				'value' => $settings['separator'],
			),
			'separator_margin' => array(
				'label'   => __( 'Add horizontal margin to separator', 'directories-pro' ),
				'value'   => ! empty( $settings['separator_margin'] ),
				'is_bool' => true,
			),
			'nowrap'           => array(
				'label'   => __( 'Do not wrap elements', 'directories-pro' ),
				'value'   => ! empty( $settings['nowrap'] ),
				'is_bool' => true,
			),
		);
		return array( 'settings' => array( 'value' => $ret ) );
	}
}
