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

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

class RowElement extends AbstractElement {

	protected function _displayElementInfo( Entity\Model\Bundle $bundle ) {
		return array(
			'type'             => 'utility',
			'label'            => _x( 'Row', 'display element name', 'directories-pro' ),
			'description'      => __( 'Display multiple display elements in a row', 'directories-pro' ),
			'default_settings' => array(
				'justification' => 'between',
				'align_items'   => 'center',
				'allow_wrap'    => false,
				'stack_small'   => false,
			),
			'containable'      => true,
			'icon'             => 'fas fa-ellipsis-h',
			'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(
			'justification' => array(
				'#type'          => 'select',
				'#title'         => __( 'Content alignment', 'directories-pro' ),
				'#default_value' => $settings['justification'],
				'#horizontal'    => true,
				'#options'       => $this->_getJustificationLabels(),
			),
			'align_items'   => array(
				'#type'          => 'select',
				'#title'         => __( 'Vertical alignment', 'directories-pro' ),
				'#default_value' => $settings['align_items'],
				'#horizontal'    => true,
				'#options'       => $this->_getAlignItemsLabels(),
			),
			'allow_wrap'    => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Allow line wrapping', 'directories-pro' ),
				'#default_value' => ! empty( $settings['allow_wrap'] ),
				'#horizontal'    => true,
			),
			'stack_small'   => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Stack on small screens', 'directories-pro' ),
				'#default_value' => ! empty( $settings['stack_small'] ),
				'#horizontal'    => true,
			),
		);
	}

	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'];
		$classes  = array(
			DRTS_BS_PREFIX . ( empty( $settings['stack_small'] ) ? 'd-flex' : 'd-sm-flex' ),
			DRTS_BS_PREFIX . 'justify-content-' . $this->_getJustification( $settings['justification'] ),
			DRTS_BS_PREFIX . 'align-items-' . $this->_getAlignItems( $settings['align_items'] ),
			DRTS_BS_PREFIX . ( empty( $settings['allow_wrap'] ) ? 'flex-nowrap' : 'flex-wrap' ),
		);
		return array(
			'html' => '<div class="' . implode( ' ', $classes ) . '">' . implode( PHP_EOL, $html ) . '</div>',
		);
	}

	protected function _getJustification( $value ) {
		return in_array( $value, array( 'start', 'end', 'center', 'around' ) ) ? $value : 'between';
	}

	protected function _getJustificationLabels() {
		return array(
			'start'   => __( 'Left', 'directories-pro' ),
			'end'     => __( 'Right', 'directories-pro' ),
			'center'  => __( 'Center', 'directories-pro' ),
			'between' => __( 'Space between', 'directories-pro' ),
			'around'  => __( 'Space around', 'directories-pro' ),
		);
	}

	protected function _getAlignItems( $value ) {
		return in_array( $value, array( 'start', 'end' ) ) ? $value : 'center';
	}

	protected function _getAlignItemsLabels() {
		return array(
			'start'  => __( 'Top', 'directories-pro' ),
			'end'    => __( 'Bottom', 'directories-pro' ),
			'center' => __( 'Center', 'directories-pro' ),
		);
	}

	protected function _displayElementReadableInfo( Entity\Model\Bundle $bundle, Display\Model\Element $element ) {
		$settings = $element->data['settings'];
		$ret      = array(
			'justification' => array(
				'label' => __( 'Content alignment', 'directories-pro' ),
				'value' => $this->_getJustificationLabels()[ $this->_getJustification( $settings['justification'] ) ],
			),
			'align_items'   => array(
				'label' => __( 'Vertical alignment', 'directories-pro' ),
				'value' => $this->_getJustificationLabels()[ $this->_getAlignItems( $settings['align_items'] ) ],
			),
			'allow_wrap'    => array(
				'label'   => __( 'Allow line wrapping', 'directories-pro' ),
				'value'   => ! empty( $settings['allow_wrap'] ),
				'is_bool' => true,
			),
			'stack_small'   => array(
				'label'   => __( 'Stack on small screens', 'directories-pro' ),
				'value'   => ! empty( $settings['stack_small'] ),
				'is_bool' => true,
			),
		);
		return array( 'settings' => array( 'value' => $ret ) );
	}
}
