<?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;

class CardElement extends AbstractElement {

	protected function _displayElementInfo( Bundle $bundle ) {
		return array(
			'type'               => 'utility',
			'label'              => _x( 'Card', 'display element name', 'directories-pro' ),
			'description'        => __( 'Show content with card styling', 'directories-pro' ),
			'default_settings'   => array(
				'title_field'     => empty( $bundle->info['is_user'] ) ? ( empty( $bundle->info['is_taxonomy'] ) ? 'post_title' : 'term_title' ) : 'user_name',
				'title_font'      => array(),
				'title_align'     => 'center',
				'subtitle_field'  => null,
				'subtitle_font'   => array(),
				'subtitle_align'  => 'center',
				'cover_field'     => null,
				'cover_height'    => 170,
				'thumbnail_field' => null,
				'thumbnail_width' => 60,
				'thumbnail_align' => 'center',
				'icon_font'       => array(),
				'icon_color'      => array(),
				'content_align'   => 'start',
				'image_overlay'   => false,
				'no_border'       => false,
				'stretch_link'    => true,
				'add_shadow'      => 'md',
			),
			'child_element_type' => array( 'field', 'content' ),
			'containable'        => true,
			'icon'               => 'far fa-address-card',
			'designable'         => array( 'margin' ),
			'labellable'         => true,
		);
	}

	protected function _displayElementSupports( Bundle $bundle, Display $display ) {
		return $display->type === 'entity'
			&& $this->_application->Entity_Field_options(
				$bundle,
				array(
					'interface'       => 'Entity\FieldType\ITitleFieldType',
					'return_disabled' => false,
				)
			);
	}

	public function displayElementSettingsForm( Bundle $bundle, array $settings, Display $display, array $parents = array(), $tab = null, $isEdit = false, array $submitValues = array() ) {
		$image_fields    = $this->_application->Entity_Field_options(
			$bundle,
			array(
				'interface'       => 'Field\Type\IImage',
				'return_disabled' => true,
			)
		);
		$icon_fields     = $this->_application->Entity_Field_options(
			$bundle,
			array(
				'type'            => 'icon',
				'return_disabled' => true,
			)
		);
		$title_fields    = $this->_application->Entity_Field_options(
			$bundle,
			array(
				'interface'       => 'Entity\FieldType\ITitleFieldType',
				'return_disabled' => true,
			)
		);
		$subtitle_fields = $this->_application->Entity_Field_options(
			$bundle,
			array(
				'interface'       => 'Field\Type\IHumanReadable',
				'return_disabled' => true,
			)
		);
		return array(
			'title_field'     => array(
				'#type'             => 'select',
				'#title'            => $title = __( 'Title field', 'directories-pro' ),
				'#options'          => array( '' => '— ' . __( 'Select field', 'directories-pro' ) . ' —' ) + $title_fields[0],
				'#options_disabled' => array_keys( $title_fields[1] ),
				'#default_value'    => $settings['title_field'],
				'#horizontal'       => true,
			),
			'title_font'      => array(
				'#states' => array(
					'visible' => array(
						sprintf( 'select[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'title_field' ) ) ) ) => array(
							'type'  => 'selected',
							'value' => true,
						),
					),
				),
			) + $this->_application->Display_ElementForm_fontSettingsForm(
				$settings['title_font'],
				array_merge( $parents, array( 'title_font' ) ),
				array(
					'prefix' => $title . ' - ',
					'bundle' => $bundle,
				)
			),
			'title_align'     => array(
				'#type'          => 'select',
				'#title'         => $title . ' - ' . __( 'Text alignment', 'directories-pro' ),
				'#options'       => $this->_getTextAlignOptions(),
				'#default_value' => $settings['title_align'],
				'#horizontal'    => true,
			),
			'subtitle_field'  => array(
				'#type'             => 'select',
				'#title'            => $title = __( 'Subtitle field', 'directories-pro' ),
				'#options'          => array( '' => '— ' . __( 'Select field', 'directories-pro' ) . ' —' ) + $subtitle_fields[0],
				'#options_disabled' => array_keys( $subtitle_fields[1] ),
				'#default_value'    => $settings['subtitle_field'],
				'#horizontal'       => true,
			),
			'subtitle_font'   => array(
				'#states' => array(
					'visible' => array(
						sprintf( 'select[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'subtitle_field' ) ) ) ) => array(
							'type'  => 'selected',
							'value' => true,
						),
					),
				),
			) + $this->_application->Display_ElementForm_fontSettingsForm(
				$settings['subtitle_font'],
				array_merge( $parents, array( 'subtitle_font' ) ),
				array(
					'prefix' => $title . ' - ',
					'bundle' => $bundle,
				)
			),
			'subtitle_align'  => array(
				'#type'          => 'select',
				'#title'         => $title . ' - ' . __( 'Text alignment', 'directories-pro' ),
				'#options'       => $this->_getTextAlignOptions(),
				'#default_value' => $settings['subtitle_align'],
				'#horizontal'    => true,
			),
			'cover_field'     => array(
				'#type'             => 'select',
				'#title'            => __( 'Cover image field', 'directories-pro' ),
				'#options'          => array( '' => '— ' . __( 'Select field', 'directories-pro' ) . ' —' ) + $image_fields[0],
				'#options_disabled' => array_keys( $image_fields[1] ),
				'#default_value'    => $settings['cover_field'],
				'#horizontal'       => true,
			),
			'cover_height'    => array(
				'#type'          => 'slider',
				'#title'         => __( 'Cover image height', 'directories-pro' ),
				'#default_value' => $settings['cover_height'],
				'#max_value'     => 300,
				'#min_value'     => 10,
				'#field_suffix'  => 'px',
				'#horizontal'    => true,
				'#states'        => array(
					'visible' => array(
						sprintf( 'select[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'cover_field' ) ) ) ) => array(
							'type'  => 'selected',
							'value' => true,
						),
					),
				),
			),
			'thumbnail_field' => array(
				'#type'             => 'select',
				'#title'            => $title = __( 'Thumbnail field', 'directories-pro' ),
				'#options'          => array( '' => '— ' . __( 'Select field', 'directories-pro' ) . ' —' ) + $image_fields[0] + $icon_fields[0],
				'#options_disabled' => array_keys( $image_fields[1] + $icon_fields[1] ),
				'#default_value'    => $settings['thumbnail_field'],
				'#horizontal'       => true,
			),
			'thumbnail_width' => array(
				'#type'          => 'slider',
				'#title'         => __( 'Thumbnail width', 'directories-pro' ),
				'#default_value' => $settings['thumbnail_width'],
				'#max_value'     => 100,
				'#min_value'     => 10,
				'#field_suffix'  => 'px',
				'#horizontal'    => true,
				'#states'        => array(
					'visible' => array(
						sprintf( 'select[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'thumbnail_field' ) ) ) ) => array(
							'type'  => 'one',
							'value' => array_keys( $image_fields[0] ),
						),
					),
				),
			),
			'icon_font'       => array(
				'#states' => array(
					'visible' => array(
						sprintf( 'select[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'thumbnail_field' ) ) ) ) => array(
							'type'  => 'one',
							'value' => array_keys( $icon_fields[0] ),
						),
					),
				),
			) + $this->_application->Display_ElementForm_fontSettingsForm(
				$settings['icon_font'],
				array_merge( $parents, array( 'icon_font' ) ),
				array(
					'prefix' => $title . ' - ',
					'bundle' => $bundle,
				),
				true
			),
			'thumbnail_align' => array(
				'#type'          => 'select',
				'#title'         => $title . ' - ' . __( 'Text alignment', 'directories-pro' ),
				'#options'       => $this->_getTextAlignOptions(),
				'#default_value' => $settings['thumbnail_align'],
				'#horizontal'    => true,
				'#states'        => array(
					'visible' => array(
						sprintf( 'select[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'thumbnail_field' ) ) ) ) => array(
							'type'  => 'selected',
							'value' => true,
						),
					),
				),
			),
			'content_align'   => array(
				'#type'          => 'select',
				'#title'         => __( 'Content alignment', 'directories-pro' ),
				'#options'       => $this->_getTextAlignOptions(),
				'#default_value' => $settings['content_align'],
				'#horizontal'    => true,
			),
			'image_overlay'   => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Use cover image as background', 'directories-pro' ),
				'#default_value' => ! empty( $settings['image_overlay'] ),
				'#horizontal'    => true,
			),
			'no_border'       => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Remove border', 'directories-pro' ),
				'#default_value' => ! empty( $settings['no_border'] ),
				'#horizontal'    => true,
			),
			'stretch_link'    => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Make entire card clickable', 'directories-pro' ),
				'#default_value' => ! empty( $settings['stretch_link'] ),
				'#horizontal'    => true,
			),
		);
	}

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

	public function displayElementRender( Bundle $bundle, array $element, $var, ?array $context = null ) {
		$settings = $element['settings'];
		$options  = array(
			'image_overlay'    => ! empty( $settings['image_overlay'] ),
			'no_border'        => ! empty( $settings['no_border'] ),
			'stretch_link'     => ! empty( $settings['stretch_link'] ),
			'cover_field'      => $settings['cover_field'],
			'cover_field_size' => $settings['cover_height'] >= 150 ? 'large' : 'medium',
			'cover_height'     => $settings['cover_height'],
			'thumbnail_field'  => $settings['thumbnail_field'],
			'thumbnail_width'  => $settings['thumbnail_width'] ?? null,
			'icon_font'        => $settings['icon_font'] ?? array(),
			'thumbnail_align'  => $settings['thumbnail_align'] ?? 'center',
			'title_field'      => $settings['title_field'],
			'title_font'       => $settings['title_font'],
			'title_align'      => $settings['title_align'],
			'subtitle_field'   => $settings['subtitle_field'],
			'subtitle_font'    => $settings['subtitle_font'],
			'subtitle_align'   => $settings['subtitle_align'],
			'content'          => ( $children_html = $this->_renderChildren( $bundle, $element['children'], $var, $context ) ) ? implode( PHP_EOL, $children_html ) : null,
			'content_align'    => $settings['content_align'],
		);
		return array(
			'html'  => $this->_application->Entity_Card( $var, $options ),
			'style' => 'height:100%;',
		);
	}

	protected function _displayElementReadableInfo( Bundle $bundle, Element $element ) {
		$settings = $element->data['settings'];
		$ret      = array(
			'title_field'     => array(
				'label' => __( 'Title field', 'directories-pro' ),
				'value' => ! empty( $settings['title_field'] ) && ( $field = $this->_application->Entity_Field( $bundle, $settings['title_field'] ) ) ? $field->getFieldLabel() : '',
			),
			'subtitle_field'  => array(
				'label' => __( 'Subtitle field', 'directories-pro' ),
				'value' => ! empty( $settings['subtitle_field'] ) && ( $field = $this->_application->Entity_Field( $bundle, $settings['subtitle_field'] ) ) ? $field->getFieldLabel() : '',
			),
			'cover_field'     => array(
				'label' => __( 'Cover image field', 'directories-pro' ),
				'value' => ! empty( $settings['cover_field'] ) && ( $field = $this->_application->Entity_Field( $bundle, $settings['cover_field'] ) ) ? $field->getFieldLabel() : '',
			),
			'cover_height'    => array(
				'label' => __( 'Cover image height', 'directories-pro' ),
				'value' => $settings['cover_height'] . 'px',
			),
			'thumbnail_field' => array(
				'label' => __( 'Thumbnail field', 'directories-pro' ),
				'value' => ! empty( $settings['thumbnail_field'] ) && ( $field = $this->_application->Entity_Field( $bundle, $settings['thumbnail_field'] ) ) ? $field->getFieldLabel() : '',
			),
			'thumbnail_width' => array(
				'label' => __( 'Thumbnail width', 'directories-pro' ),
				'value' => $settings['thumbnail_width'] . 'px',
			),
			'content_align'   => array(
				'label' => __( 'Content alignment', 'directories-pro' ),
				'value' => $this->_getTextAlignOptions()[ $settings['content_align'] ],
			),
			'image_overlay'   => array(
				'label'   => __( 'Enable image overlay', 'directories-pro' ),
				'value'   => ! empty( $settings['image_overlay'] ),
				'is_bool' => true,
			),
			'no_border'       => array(
				'label'   => __( 'Remove border', 'directories-pro' ),
				'value'   => ! empty( $settings['no_border'] ),
				'is_bool' => true,
			),
			'stretch_link'    => array(
				'label'   => __( 'Make entire card clickable', 'directories-pro' ),
				'value'   => ! empty( $settings['stretch_link'] ),
				'is_bool' => true,
			),
		);
		return array( 'settings' => array( 'value' => $ret ) );
	}
}
