<?php
namespace SabaiApps\Directories\Component\Voting\FieldFilter;

use SabaiApps\Directories\Component\Entity\Type\Query;
use SabaiApps\Directories\Component\Field\IField;
use SabaiApps\Directories\Component\Field\Filter\IConditionable;
use SabaiApps\Directories\Component\Field\Filter\ConditionableNumberTrait;

class RatingFieldFilter extends AbstractFieldFilter implements IConditionable {

	use ConditionableNumberTrait;

	protected function _fieldFilterInfo() {
		return parent::_fieldFilterInfo() + array(
			'default_settings' => array(
				'type'       => 'radios',
				'columns'    => 1,
				'hide_count' => false,
			),
			'facetable'        => true,
		);
	}

	public function fieldFilterSettingsForm( IField $field, array $settings, array $parents = array() ) {
		$form = array(
			'type'    => array(
				'#title'         => __( 'Form field type', 'directories-pro' ),
				'#type'          => 'select',
				'#options'       => array(
					'checkboxes' => __( 'Checkboxes', 'directories-pro' ),
					'radios'     => __( 'Radio buttons', 'directories-pro' ),
					'select'     => __( 'Select list', 'directories-pro' ),
				),
				'#default_value' => $settings['type'],
			),
			'columns' => array(
				'#type'          => 'select',
				'#title'         => __( 'Number of columns', 'directories-pro' ),
				'#options'       => array(
					1 => 1,
					2 => 2,
					3 => 3,
					6 => 6,
				),
				'#default_value' => $settings['columns'],
				'#states'        => array(
					'invisible' => array(
						sprintf( '[name="%s[type]"]', $this->_application->Form_Form_fieldName( $parents ) ) => array( 'value' => 'select' ),
					),
				),
			),
		);
		if ( $this->_application->getComponent( 'View' )->getConfig( 'filters', 'facet_count' ) ) {
			$form['hide_count'] = array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Hide count', 'directories-pro' ),
				'#default_value' => $settings['hide_count'],
			);
		}
		return $form;
	}

	public function fieldFilterForm( IField $field, $filterName, array $settings, $request = null, ?Query $query = null, ?array $current = null, $autoSubmit = true, $enableFacetCount = false, array $parents = array() ) {
		if ( $enableFacetCount
			&& empty( $settings['hide_count'] )
		) {
			// Clone field query and exclude queries for the rating field and use it to fetch facets
			$field_query = clone $query->getFieldQuery();
			$vote_name   = $this->_getVoteName( $settings );
			$field_query->removeNamedCriteria( $field->getFieldName() . '__' . $vote_name );
			$facets = $this->_application->Entity_Facets(
				$field,
				$field_query,
				array(
					'column'  => $this->_valueColumn,
					'filters' => array(
						'name' => $vote_name,
					),
				)
			);
		}

		if ( ! isset( $current ) ) {
			switch ( $settings['type'] ) {
				case 'select':
					$options = $this->_application->Voting_RenderRating_options( true, '' );
					break;
				case 'checkboxes':
					$options = $this->_application->Voting_RenderRating_options( false, null, false );
					break;
				default:
					$options = $this->_application->Voting_RenderRating_options( false );
			}
			$current = array(
				'#type'                    => $settings['type'],
				'#options'                 => $options,
				'#option_no_escape'        => true,
				'#columns'                 => $settings['columns'],
				'#empty_value'             => '',
				'#entity_filter_form_type' => $settings['type'],
			);
		}

		if ( isset( $facets ) ) {
			$request = (array) $request;
			if ( $settings['type'] === 'checkboxes' ) {
				for ( $i = 5; $i >= 1; $i-- ) {
					$current['#options'][ $i ] = array(
						'#title' => $current['#options'][ $i ],
						'#count' => empty( $facets[ $i ] ) ? 0 : $facets[ $i ],
					);
					if ( empty( $facets[ $i ] )
						&& ! in_array( $i, $request )
					) {
						$current['#options_disabled'][] = $i;
					}
				}
			} else {
				$request = (int) $request;
				$sum     = 0;
				for ( $i = 5; $i >= 1; $i-- ) {
					if ( ! empty( $facets[ $i ] ) ) {
						$sum += $facets[ $i ];
					}
					$current['#options'][ $i ] = array(
						'#title' => $current['#options'][ $i ],
						'#count' => $sum,
					);
					if ( empty( $sum )
						&& $i !== $request
					) {
						$current['#options_disabled'][] = $i;
					}
				}
			}
		}

		return $current;
	}

	public function fieldFilterSupports( IField $field ) {
		return $field->getFieldName() === 'voting_rating';
	}
}
