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

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

class CheckboxesWidget extends RadioButtonsWidget {

	protected function _fieldWidgetInfo() {
		$info                      = parent::_fieldWidgetInfo();
		$info['label']             = __( 'Checkboxes', 'directories-pro' );
		$info['accept_multiple']   = true;
		$info['default_settings'] += array(
			'popup'  => false,
			'height' => 0,
		);
		return $info;
	}

	public function fieldWidgetSettingsForm( $fieldType, Bundle $bundle, array $settings, array $parents = array(), array $rootParents = array() ) {
		return parent::fieldWidgetSettingsForm( $fieldType, $bundle, $settings, $parents, $rootParents ) + array(
			'popup'  => array(
				'#title'         => __( 'Show option list in popup', 'directories-pro' ),
				'#type'          => 'checkbox',
				'#default_value' => ! empty( $settings['popup'] ),
				'#weight'        => 2,
			),
			'height' => array(
				'#title'         => __( 'Option list height', 'directories-pro' ),
				'#type'          => 'slider',
				'#min_value'     => 0,
				'#max_value'     => 500,
				'#min_text'      => __( 'Auto', 'directories-pro' ),
				'#field_suffix'  => 'px',
				'#integer'       => true,
				'#default_value' => $settings['height'],
				'#states'        => array(
					'visible' => array(
						sprintf( '[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'columns' ) ) ) ) => array( 'value' => 1 ),
						sprintf( '[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'popup' ) ) ) ) => array(
							'type'  => 'checked',
							'value' => 1,
						),
					),
				),
				'#weight'        => 3,
			),
		);
	}

	public function fieldWidgetForm( IField $field, array $settings, $value = null, ?IEntity $entity = null, array $parents = array(), $language = null ) {
		$form          = parent::fieldWidgetForm( $field, $settings, $value, $entity, $parents, $language );
		$form['#type'] = 'checkboxes';
		if ( $form['#max_selection'] = $field->getFieldMaxNumItems() ) {
			$form['#disable_select_all'] = true;
		}
		if ( ! empty( $settings['popup'] ) ) {
			$form['#type']               = 'select';
			$form['#multiple']           = true;
			$form['#multiselect']        = true;
			$form['#multiselect_height'] = ! empty( $settings['height'] ) && $settings['columns'] == 1 ? $settings['height'] : 0;
			$form['#placeholder']        = sprintf( __( 'Select %s', 'directories-pro' ), $field->getFieldLabel() );
		}

		return $form;
	}

	protected function _getOptionIcons( IField $field, array $settings, ?IEntity $entity = null, $language = null ) {
		if ( ! empty( $settings['popup'] ) ) {
			return array(); // icons can't be used for popup list
		}

		return parent::_getOptionIcons( $field, $settings, $entity, $language );
	}
}
