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

use SabaiApps\Directories\Component\Entity;
use SabaiApps\Directories\Component\Field\IField;

class CheckboxWidget extends AbstractWidget {

	protected function _fieldWidgetInfo() {
		return array(
			'label'            => __( 'Single checkbox', 'directories-pro' ),
			'field_types'      => array( 'boolean' ),
			'default_settings' => array(
				'checkbox_label'     => null,
				'checked'            => false,
				'switch'             => false,
				'hide_after_checked' => false,
			),
		);
	}

	public function fieldWidgetSettingsForm( $fieldType, Entity\Model\Bundle $bundle, array $settings, array $parents = array(), array $rootParents = array() ) {
		return array(
			'checkbox_label'     => array(
				'#type'          => 'textfield',
				'#title'         => __( 'Checkbox label', 'directories-pro' ),
				'#description'   => __( 'Enter the label displayed next to the checkbox.', 'directories-pro' ),
				'#default_value' => $settings['checkbox_label'],
				'#max_length'    => 0,
				'#states'        => array(
					'visible' => array(
						sprintf( '[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'switch' ) ) ) ) => array(
							'type'  => 'checked',
							'value' => false,
						),
					),
				),
			),
			'checked'            => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Make this field checked by default', 'directories-pro' ),
				'#default_value' => ! empty( $settings['checked'] ),
			),
			'switch'             => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Display toggle switch', 'directories-pro' ),
				'#default_value' => ! empty( $settings['switch'] ),
			),
			'hide_after_checked' => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Hide after checked', 'directories-pro' ),
				'#description'   => __( 'If checked, this field will be hidden on the frontend once it has been checked.', 'directories-pro' ),
				'#default_value' => ! empty( $settings['hide_after_checked'] ),
				'#states'        => array(
					'visible' => array(
						sprintf( '[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'checked' ) ) ) ) => array(
							'type'  => 'checked',
							'value' => false,
						),
					),
				),
			),
		);
	}

	public function fieldWidgetForm( IField $field, array $settings, $value = null, ?Entity\Type\IEntity $entity = null, array $parents = array(), $language = null ) {
		$hide_after_checked = ! empty( $settings['hide_after_checked'] )
			&& isset( $entity )
			&& (bool) $entity->getSingleFieldValue( $field->getFieldName() ) // need to check the value from the entity as $value may not be set when the form is submitted and there was an error
			&& ! $this->_application->getPlatform()->isAdmin();
		if ( $hide_after_checked ) {
			return array(
				'#type'  => 'hidden',
				'#value' => 1,
			);
		}

		return array(
			'#type'             => 'checkbox',
			'#on_value'         => 1,
			'#off_value'        => 0,
			'#on_label'         => $settings['checkbox_label'],
			'#default_value'    => isset( $value ) ? $value : ! empty( $settings['checked'] ),
			'#switch'           => ! empty( $settings['switch'] ),
			'#option_no_escape' => true,
		);
	}
}
