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

use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Exception;
use SabaiApps\Directories\Component\Display\Model\Display;
use SabaiApps\Directories\Component\Field\IField;

class ParentFieldDisplayElement extends FieldDisplayElement {

	protected function _doGetField( $bundle ) {
		if ( ! $parent_bundle = $this->_application->Entity_Bundle( $bundle->info['parent'] ) ) {
			throw new Exception\RuntimeException( 'Invalid entity parent bundle' );
		}
		$field_name = substr( $this->_name, 20 ); // remove entity_parent_field_ part
		return $this->_application->Entity_Field( $parent_bundle, $field_name );
	}

	protected function _displayElementSupports( Bundle $bundle, Display $display ) {
		return $display->type === 'entity';
	}

	protected function _displayElementInfo( Bundle $bundle ) {
		$field         = $this->_getField( $bundle );
		$parent_bundle = $this->_application->Entity_Bundle( $field->getBundleName() );

		return array(
			'type'             => 'parent',
			'label'            => $parent_bundle->getLabel( 'singular' ) . ' - ' . $field->getFieldLabel(),
			'description'      => sprintf( __( 'Field name: %s', 'directories-pro' ), $field->getFieldName() ),
			'class'            => 'drts-field-type-' . str_replace( '_', '-', $field->getFieldType() ) . ' drts-field-name-' . str_replace( '_', '-', $field->getFieldName() ),
			'default_settings' => array(
				'label'             => 'none',
				'label_custom'      => null,
				'label_icon'        => null,
				'label_icon_size'   => null,
				'renderer'          => null,
				'renderer_settings' => array(),
			),
			'icon'             => $this->_application->Field_Type( $field->getFieldType() )->fieldTypeInfo( 'icon' ),
			'headingable'      => false,
			'cacheable'        => $this->_application->Field_Type( $field->getFieldType() )->fieldTypeInfo( 'cacheable' ),
			'designable'       => array( 'margin', 'font' ),
		);
	}

	public function displayElementRender( Bundle $bundle, array $element, $var, ?array $context = null ) {
		if ( ! $entity = $this->_application->Entity_ParentEntity( $var ) ) {
			return;
		}

		return parent::displayElementRender( $bundle, $element, $entity, $context );
	}

	protected function _renderFieldLabel( IField $field, array $element, $var, ?array $context = null ) {
		if ( ! $parent_bundle = $this->_application->Entity_Bundle( $var ) ) {
			return '';
		}

		$label = $parent_bundle->getLabel( 'singular' );
		if ( $field->getFieldType() === 'entity_title' ) {
			return $label;
		}

		$field_label = parent::_renderFieldLabel( $field, $element, $var, $context );
		return strlen( $field_label ) ? $label . ' - ' . $field_label : $label;
	}

	public function displayElementPreRender( Bundle $bundle, array $element, &$var ) {
		$entities = array();
		if ( ! empty( $var['entities'] ) ) {
			foreach ( $var['entities'] as $entity ) {
				if ( ! $parent_id = $entity->getParentId() ) {
					continue;
				}

				if ( isset( $entities[ $parent_id ] ) ) {
					// Re-assign parent with already fetched parent so that parent entities with same IDs are updated at once
					$entity->setParent( $entities[ $parent_id ] );
					continue;
				}
				if ( $parent = $this->_application->Entity_ParentEntity( $entity ) ) {
					$entities[ $parent_id ] = $parent;
				}
			}
		}
		$_var['entities'] = $entities;
		parent::displayElementPreRender( $bundle, $element, $_var );
	}

	public function displayElementTitle( Bundle $bundle, array $data ) {
		$field = $this->_getField( $bundle );
		return $this->_application->Display_Render_elementLabel(
			$data['settings'],
			null,
			$this->_application->Entity_Bundle( $bundle->info['parent'] )->getLabel( 'singular' ) . ' - ' . $field->getFieldLabel()
		);
	}
}
