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

use SabaiApps\Directories\Component\Field\Renderer\AbstractRenderer;
use SabaiApps\Directories\Component\Field\IField;
use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\Entity\Model\Bundle;

class FieldsetFieldRenderer extends AbstractRenderer {

	protected function _fieldRendererInfo() {
		return array(
			'field_types'      => array( $this->_name ),
			'default_settings' => array(
				'list'            => array(),
				'sort'            => array(),
				'pagination'      => array(),
				'view_add_border' => false,
			),
			'separatable'      => false,
		);
	}

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

		return array(
			'list'       => $this->_application->View_SettingsForm_mode( $bundle, 'fieldset_list', $settings['list'], array_merge( $parents, array( 'list' ) ) ),
			'sort'       => $this->_application->View_SettingsForm_feature( $bundle, 'sort', $settings['sort'], false, '', array_merge( $parents, array( 'sort' ) ) ),
			'pagination' => $this->_application->View_SettingsForm_feature( $bundle, 'pagination', $settings['pagination'], false, '', array_merge( $parents, array( 'pagination' ) ) ),
		);
	}

	protected function _getBundleViewPath( Bundle $bundle, IField $field, array &$settings ) {
		return array(
			'path'   => '/_drts/fieldset/' . $bundle->name,
			'params' => array(
				'bundle' => $field->Bundle->name,
				'field'  => $field->getFieldName(),
			),
		);
	}

	protected function _fieldRendererRenderField( IField $field, array &$settings, IEntity $entity, array $values, ?array $context = null, $more = 0 ) {
		if ( empty( $values ) ) {
			return;
		}

		$field_settings = $field->getFieldSettings();
		if ( empty( $field_settings['bundle'] )
			|| ( ! $bundle = $this->_application->Entity_Bundle( $field_settings['bundle'] ) )
		) {
			return;
		}

		$view_path     = array(
			'path'   => '/_drts/fieldset/' . $bundle->name,
			'params' => array(
				'bundle' => $field->Bundle->name,
				'field'  => $field->getFieldName(),
			),
		);
		$view_settings = $this->_getViewSettings( $field, $settings );
		$view_settings['query']['fields'][ $bundle->entitytype_name . '_id' ] = implode( ',', $values );
		$view_settings['query']['user_id']                                    = 0; // required to show non-public entities
		$view_settings['filter']['show']                                      = false;
		$view_settings['display']         = $settings['_entity_display_name'];
		$view_settings['view_add_border'] = ! empty( $settings['view_add_border'] );

		return $this->_application->getPlatform()->render(
			$view_path,
			array(
				'settings' => array(
					'mode'          => $this->_getViewMode( $field, $settings ),
					'push_state'    => false,
					'view_settings' => $view_settings,
					'url_params'    => $view_path['params'], // need this for routing.
					'no_json'       => true,
				),
			),
			array(
				'cache'         => false,
				'title'         => false,
				'container'     => null,
				'render_assets' => false,
			)
		);
	}

	protected function _getViewMode( IField $field, array &$settings ) {
		return 'fieldset_list';
	}

	protected function _getViewSettings( IField $field, array &$settings ) {
		return $settings['list'] + array(
			'pagination' => $settings['pagination'],
			'sort'       => $settings['sort'],
			'display'    => 'detailed',
		);
	}

	protected function _getFieldsetBundle( IField $field ) {
		$field_settings = $field->getFieldSettings();
		return ! empty( $field_settings['bundle'] ) && ( $bundle = $this->_application->Entity_Bundle( $field_settings['bundle'] ) ) ? $bundle : null;
	}

	protected function _fieldRendererReadableSettings( IField $field, array $settings ) {
		return array();
	}
}
