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

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Display\Model\Display;
use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\Entity\Type\IEntity;

class TermEntitiesDisplayElement extends AbstractEntitiesDisplayElement {

	protected $_contentBundleName;

	public function __construct( Application $application, $name ) {
		parent::__construct( $application, $name );
		$this->_contentBundleName = substr( $name, 19 ); // remove 'view_term_entities_' prefix
	}

	protected function _displayElementInfo( Bundle $bundle ) {
		return parent::_displayElementInfo( $bundle ) + array(
			'label'       => $label = $this->_application->Entity_Bundle( $this->_contentBundleName )->getLabel(),
			'description' => sprintf( __( 'Displays %s of the current term', 'directories-pro' ), strtolower( $label ), $label ),
		);
	}

	public function displayElementSupports( Bundle $bundle, Display $display ) {
		return parent::displayElementSupports( $bundle, $display )
			&& ! empty( $bundle->info['is_taxonomy'] );
	}

	public function displayElementRender( Bundle $bundle, array $element, $var, ?array $context = null ) {
		if ( ( $content_bundle = $this->_application->Entity_Bundle( $this->_contentBundleName ) )
			&& $this->_application->Entity_IsRoutable( $content_bundle, 'list', $var )
		) {
			return parent::displayElementRender( $bundle, $element, $var, $context );
		}
	}

	protected function _getEntitiesBundle( $entityOrBundle, array $settings ) {
		return $this->_application->Entity_Bundle( $this->_contentBundleName );
	}

	protected function _getListEntitiesSettings( Bundle $bundle, array $element, IEntity $entity ) {
		$settings = parent::_getListEntitiesSettings( $bundle, $element, $entity );
		$settings['view_settings']['query']['fields'][ $entity->getBundleType() ] = $entity->getId();
		return $settings;
	}
}
