<?php
/**
 * Product size filter widget class.
 *
 * @package ecommerce-product-catalog/includes/widgets
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Size filter widget.
 */
class IC_Product_Size_Filter extends WP_Widget {

	/**
	 * Sets up the widget.
	 *
	 * @return void
	 */
	public function __construct() {
		if ( is_plural_form_active() ) {
			$names = get_catalog_names();
			/* translators: %s: singular catalog item label. */
			$label = sprintf( __( '%s Size Filter', 'ecommerce-product-catalog' ), ic_ucfirst( $names['singular'] ) );
			/* translators: %s: plural catalog item label. */
			$sublabel = sprintf( __( 'Filter %s by size.', 'ecommerce-product-catalog' ), ic_lcfirst( $names['plural'] ) );
		} else {
			$label    = __( 'Catalog Size Filter', 'ecommerce-product-catalog' );
			$sublabel = __( 'Filter items by size.', 'ecommerce-product-catalog' );
		}
		$classname = 'product_size_filter';
		if ( is_product_filter_active( '_size_length' ) ) {
			$classname .= ' active';
		}
		$widget_ops = array(
			'classname'             => $classname,
			'description'           => $sublabel,
			'show_instance_in_rest' => true,
		);
		parent::__construct( 'ic_product_size_filter', $label, $widget_ops );
	}

	/**
	 * Renders the widget.
	 *
	 * @param array $args     Widget wrapper args.
	 * @param array $instance Widget instance.
	 *
	 * @return void
	 */
	public function widget( $args, $instance ) {
		if ( 'simple' !== get_integration_type() ) {
			if ( ic_if_show_filter_widget( $instance ) && function_exists( 'ic_size_field_names' ) ) {
				ob_start();
				$this->size_filters();
				$size_filters = ob_get_clean();
				if ( empty( $size_filters ) ) {
					return;
				}
				$this->styles();

				$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Widget wrapper HTML is provided by WordPress/theme.
				echo $args['before_widget'];
				if ( $title ) {
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Widget wrapper HTML is provided by WordPress/theme.
					echo $args['before_title'] . $title . $args['after_title'];
				}
				do_action( 'ic_size_widget_before_sliders', $instance );
				echo '<form class="product-size-filter-container ic-slider-container toReload ic_ajax ' . esc_attr( design_schemes( 'box', 0 ) ) . '" data-ic_responsive_label="' . esc_attr__( 'Size', 'ecommerce-product-catalog' ) . '" data-ic_ajax="product-size-filter-container" action="' . esc_url( get_filter_widget_action( $instance ) ) . '">';
				$hidden_fields = array_keys( ic_size_field_names() );
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.EscapeOutput.OutputNotEscaped -- Hidden field HTML is generated by the widget helper while preserving the current filter query.
				echo ic_get_to_hidden_field( $_GET, $hidden_fields );
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Slider HTML is generated by the widget helper.
				echo $size_filters;
				echo '</form>';
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Widget wrapper HTML is provided by WordPress/theme.
				echo $args['after_widget'];
			}
		}
	}

	/**
	 * Outputs the size filters.
	 *
	 * @return void
	 */
	public function size_filters() {
		do_action( 'ic_size_filters' );
	}

	/**
	 * Outputs the widget admin form.
	 *
	 * @param array $instance Widget instance.
	 *
	 * @return void
	 */
	public function form( $instance ) {
		if ( 'simple' !== get_integration_type() ) {
			$instance = wp_parse_args(
				(array) $instance,
				array(
					'title'             => '',
					'shortcode_support' => 0,
				)
			);
			$title    = $instance['title'];
			?>
			<p>
				<label
						for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'ecommerce-product-catalog' ); ?>
					<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
							name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
							value="<?php echo esc_attr( $title ); ?>"/></label></p>
			<p><input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'shortcode_support' ) ); ?>"
						name="<?php echo esc_attr( $this->get_field_name( 'shortcode_support' ) ); ?>" type="checkbox"
						value="1" <?php checked( 1, $instance['shortcode_support'] ); ?> /> <label
					for="<?php echo esc_attr( $this->get_field_id( 'shortcode_support' ) ); ?>"><?php esc_html_e( 'Enable also for shortcodes', 'ecommerce-product-catalog' ); ?></label>
			</p>
			<?php
			do_action( 'ic_size_filter_widget_form', $instance, $this );
		} else {
			// Size filter is disabled in simple mode due to the missing main listing page.
			IC_Catalog_Notices::simple_mode_notice();
		}
	}

	/**
	 * Sanitizes widget options on save.
	 *
	 * @param array $new_instance New widget values.
	 * @param array $old_instance Previous widget values.
	 *
	 * @return array
	 */
	public function update( $new_instance, $old_instance ) {
		$instance                      = $old_instance;
		$new_instance                  = wp_parse_args(
			(array) $new_instance,
			array(
				'title'             => '',
				'shortcode_support' => 0,
			)
		);
		$instance['title']             = wp_strip_all_tags( $new_instance['title'] );
		$instance['shortcode_support'] = intval( $new_instance['shortcode_support'] );

		return apply_filters( 'ic_size_widget_save', $instance, $new_instance );
	}

	/**
	 * Enqueues slider assets.
	 *
	 * @return void
	 */
	public function styles() {
		wp_enqueue_style( 'ic_range_slider' );
		wp_enqueue_script( 'ic_range_slider' );
	}
}

