<?php
namespace SabaiApps\Directories\Component\Search\Field;

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\Entity\Type\Query;

class TermField extends AbstractField {

	protected $_bundleType;

	public function __construct( Application $application, $name, $bundleType ) {
		parent::__construct( $application, $name );
		$this->_bundleType = $bundleType;
	}

	protected function _searchFieldInfo() {
		return array(
			'label'            => sprintf(
				_x( '%s Selection Search', 'search settings label', 'directories-pro' ),
				$this->_application->Entity_BundleTypeInfo( $this->_bundleType, 'label_singular' )
			),
			'weight'           => 3,
			'default_settings' => array(
				'disabled'   => false,
				'hide_empty' => false,
				'hide_count' => false,
				'depth'      => 0,
				'no_fancy'   => true,
				'form'       => array(
					'order' => 3,
				),
			),
		);
	}

	public function searchFieldSupports( Bundle $bundle ) {
		return isset( $bundle->info['taxonomies'][ $this->_bundleType ] );
	}

	public function searchFieldSettingsForm( Bundle $bundle, array $settings, array $parents = array() ) {
		$ret = array(
			'depth'      => array(
				'#type'          => 'slider',
				'#title'         => __( 'Term hierarchy depth', 'directories-pro' ),
				'#default_value' => $settings['depth'],
				'#min_value'     => 0,
				'#max_value'     => 10,
				'#min_text'      => __( 'Unlimited', 'directories-pro' ),
				'#integer'       => true,
				'#weight'        => 1,
				'#horizontal'    => true,
			),
			'hide_empty' => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Hide empty terms', 'directories-pro' ),
				'#default_value' => ! empty( $settings['hide_empty'] ),
				'#horizontal'    => true,
				'#weight'        => 6,
			),
			'hide_count' => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Hide post counts', 'directories-pro' ),
				'#default_value' => ! empty( $settings['hide_count'] ),
				'#horizontal'    => true,
				'#weight'        => 7,
			),
			'no_fancy'   => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Disable fancy dropdown', 'directories-pro' ),
				'#default_value' => ! empty( $settings['no_fancy'] ),
				'#weight'        => 8,
				'#horizontal'    => true,
			),
		);

		return $ret;
	}

	public function searchFieldForm( Bundle $bundle, array $settings, $request = null, ?array $requests = null, array $parents = array() ) {
		$taxonomy_bundle = $this->_application->Entity_Bundle( $this->_bundleType, $bundle->group );
		$current_term_id = null;
		if ( isset( $GLOBALS['drts_entity'] ) && $GLOBALS['drts_entity']->getBundleType() === $this->_bundleType ) {
			$current_term_id = $GLOBALS['drts_entity']->getId();
		} elseif ( isset( $settings['default_term'] )
				&& ( $default_term = trim( $settings['default_term'] ) )
			) {

			if ( ! is_numeric( $default_term ) ) {
				if ( $current_term = $this->_application->Entity_Entity( $taxonomy_bundle->entitytype_name, array( $taxonomy_bundle->name, $default_term ) ) ) {
					$current_term_id = $current_term->getSlug();
				}
			} else {
				$current_term_id = $default_term;
			}
		}
		$_options = array(
			'depth'      => $settings['depth'],
			'hide_empty' => ! empty( $settings['hide_empty'] ),
			'hide_count' => ! empty( $settings['hide_count'] ),
			'prefix'     => '—',
			'parent'     => isset( $settings['parent'] ) ? (int) $settings['parent'] : 0,
		);

		$options = $this->_application->Entity_TaxonomyTerms_html(
			$taxonomy_bundle->name,
			array(
				'content_bundle' => $bundle->name,
				'count_no_html'  => true,
			) + $_options
		);
		if ( ! count( $options ) ) {
			return;
		}

		$default_text = $taxonomy_bundle->getLabel( 'select' );

		$form = array(
			'#type'            => 'select',
			'#select2'         => empty( $settings['no_fancy'] ),
			'#placeholder'     => $default_text,
			'#options'         => array( '' => $default_text ) + $options,
			'#default_value'   => isset( $current_term_id ) ? $current_term_id : $request,
			'#attributes'      => isset( $attributes ) ? $attributes : array(),
			'#required'        => ! empty( $settings['required'] ),
			'#error_no_output' => true,
			'#empty_value'     => '', // required for required validation to work
			// '#multiselect' => true,
			// '#multiple' => true,
		);

		return $form;
	}

	public function searchFieldIsSearchable( Bundle $bundle, array $settings, &$value, ?array $requests = null ) {
		return is_array( $value ) ? ! empty( $value ) : $value !== '';
	}

	public function searchFieldSearch( Bundle $bundle, Query $query, array $settings, $value, $sort, array &$sorts ) {
		$query->taxonomyTermIdIn(
			$this->_bundleType,
			is_array( $value ) ? $value : array( $value ),
			! $this->_application->Entity_BundleTypeInfo( $this->_bundleType, 'is_hierarchical' ),
			$this->_bundleType . '_search_term', // table alias
			$this->_bundleType . '_search_term' // criteria name
		);
	}

	public function searchFieldLabels( Bundle $bundle, array $settings, $value ) {
		$titles      = array();
		$entity_type = $this->_application->Entity_BundleTypeInfo( $this->_bundleType, 'entity_type' );
		foreach ( $this->_application->Entity_Entities( $entity_type, (array) $value, false ) as $entity ) {
			$titles[] = $entity->getTitle();
		}
		return array( $titles );
	}
}
