<?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 IconsElement extends AbstractElement {

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

	public function displayElementSettingsForm( Bundle $bundle, array $settings, Display $display, array $parents = array(), $tab = null, $isEdit = false, array $submitValues = array() ) {
		switch ( $tab ) {
			case 'icons':
				return $this->_application->Display_Icons_settingsForm( $bundle, $settings['icons'], $display, $parents, array( 'general', 'settings', 'arrangement' ) );
			default:
				list($options, $defaults) = $this->_application->Display_Icons_options( $bundle, $display, true );
				return array(
					'#tabs'       => array(
						'icons' => _x( 'Icons', 'settings tab', 'directories-pro' ),
					),
					'arrangement' => array(
						'#type'          => 'sortablecheckboxes',
						'#title'         => __( 'Icon display order', 'directories-pro' ),
						'#horizontal'    => true,
						'#options'       => $options,
						'#default_value' => isset( $settings['arrangement'] ) ? $settings['arrangement'] : $defaults,
					),
					'size'        => array(
						'#title'         => __( 'Icon size', 'directories-pro' ),
						'#type'          => 'select',
						'#horizontal'    => true,
						'#options'       => $this->_application->System_Icon_sizeOptions(),
						'#default_value' => isset( $settings['size'] ) ? $settings['size'] : 'sm',
					),
				);
		}
	}

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

		foreach ( array_keys( $icons ) as $icon_name ) {
			if ( ( ! $icon = $this->_application->Display_Icons_impl( $bundle, $icon_name, true ) )
				|| ! $icon->displayIconSupports( $bundle, $display )
			) {
				unset( $icons[ $icon_name ] );
			}
		}

		return ! empty( $icons );
	}

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

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

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