<?php
namespace SabaiApps\Directories\Component\Entity\FieldRenderer;

use SabaiApps\Directories\Component\Entity;
use SabaiApps\Directories\Component\Field;

class ReferenceLinkFieldRenderer extends Field\Renderer\AbstractRenderer {

	protected function _fieldRendererInfo() {
		return array(
			'label'            => __( 'Link', 'directories-pro' ),
			'field_types'      => array( 'entity_reference' ),
			'default_settings' => array(
				'icon'       => false,
				'icon_size'  => 'sm',
				'no_link'    => false,
				'_separator' => ', ',
			),
			'inlineable'       => true,
		);
	}

	protected function _fieldRendererSettingsForm( Field\IField $field, array $settings, array $parents = array() ) {
		if ( ! $bundle = $this->_getReferencedBundle( $field ) ) {
			return;
		}

		return $this->_application->System_Icon_settingsForm( $bundle, $settings, $parents ) + array(
			'no_link' => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Do not link', 'directories-pro' ),
				'#default_value' => ! empty( $settings['no_link'] ),
				'#horizontal'    => true,
			),
		);
	}

	protected function _fieldRendererRenderField( Field\IField $field, array &$settings, Entity\Type\IEntity $entity, array $values, ?array $context = null, $more = 0 ) {
		if ( ! $bundle = $this->_getReferencedBundle( $field ) ) {
			return;
		}

		$entity_ids = array();
		foreach ( $values as $referenced_item ) {
			$entity_ids[] = $referenced_item->getId();
		}
		if ( empty( $entity_ids ) ) {
			return;
		}

		$entities = $this->_application->Entity_Entities( $bundle->entitytype_name, $entity_ids, ! empty( $settings['icon'] ), true );

		// Render permalinks
		$options            = $this->_application->System_Icon_settingsToPermalinkOptions( $bundle, $settings );
		$options['no_link'] = ! empty( $settings['no_link'] );
		$ret                = array();
		foreach ( array_keys( $entities ) as $i ) {
			$ret[] = $this->_application->Entity_Permalink( $entities[ $i ], $options );
		}

		return implode( $settings['_separator'], $ret );
	}

	protected function _getReferencedBundle( Field\IField $field ) {
		$field_settings = $field->getFieldSettings();
		if ( ! empty( $field_settings['bundle'] )
			&& ( $bundle = $this->_application->Entity_Bundle( $field_settings['bundle'] ) )
		) {
			return $bundle;
		}
	}
}
