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

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

class LabelsElement extends AbstractElement {

	protected function _displayElementInfo( Entity\Model\Bundle $bundle ) {
		return array(
			'type'             => 'content',
			'label'            => _x( 'Labels', 'display element name', 'directories-pro' ),
			'description'      => __( 'Small tags for adding context', 'directories-pro' ),
			'default_settings' => array(
				'labels' => array(),
				'style'  => null,
			),
			'inlineable'       => true,
			'icon'             => 'fas fa-tags',
			'designable'       => array( 'margin', 'font' ),
		);
	}

	public function displayElementSettingsForm( Entity\Model\Bundle $bundle, array $settings, Display\Model\Display $display, array $parents = array(), $tab = null, $isEdit = false, array $submitValues = array() ) {
		switch ( $tab ) {
			case 'labels':
				return $this->_application->Display_Labels_settingsForm( $bundle, $settings['labels'], $parents, array( 'general', 'settings', 'arrangement' ) );
			default:
				return array(
					'#tabs'       => array(
						'labels' => _x( 'Labels', 'settings tab', 'directories-pro' ),
					),
					'arrangement' => array(
						'#type'          => 'sortablecheckboxes',
						'#title'         => __( 'Label display order', 'directories-pro' ),
						'#horizontal'    => true,
						'#options'       => $options = $this->_application->Display_Labels_options( $bundle ),
						'#default_value' => isset( $settings['arrangement'] ) ? $settings['arrangement'] : array_keys( $options ),
					),
					'style'       => array(
						'#title'         => __( 'Label style', 'directories-pro' ),
						'#type'          => 'select',
						'#horizontal'    => true,
						'#options'       => array(
							''     => __( 'Default', 'directories-pro' ),
							'pill' => __( 'Oval', 'directories-pro' ),
						),
						'#default_value' => $settings['style'],
					),
				);
		}
	}

	protected function _displayElementSupports( Entity\Model\Bundle $bundle, Display\Model\Display $display ) {
		if ( $display->type !== 'entity'
			|| ! empty( $bundle->info['is_taxonomy'] )
		) {
			return false;
		}

		$labels = $this->_application->Display_Labels( $bundle );
		return ! empty( $labels );
	}

	public function displayElementRender( Entity\Model\Bundle $bundle, array $element, $var, ?array $context = null ) {
		$settings = $element['settings'];
		return $this->_application->Display_Labels_renderLabels( $bundle, $settings, $settings['labels'], $var );
	}

	protected function _displayElementReadableInfo( Entity\Model\Bundle $bundle, Display\Model\Element $element ) {
		$settings = $element->data['settings'];
		if ( empty( $settings['arrangement'] ) ) {
			return;
		}

		$ret = array(
			'labels' => array(
				'label' => __( 'Labels', 'directories-pro' ),
				'value' => implode( ', ', $this->_application->Display_Labels_labelLabels( $bundle, $settings['arrangement'] ) ),
			),
		);
		return array( 'settings' => array( 'value' => $ret ) );
	}
}
