<?php
declare( strict_types=1 );

namespace FiboFilters\Integrations\Themes;

use FiboFilters\Config\AssetsLoader;
use FiboFilters\Filtering\Front;
use FiboFilters\Integrations\Plugins\Elementor\Utils as ElementorUtils;
use FiboFilters\Settings\Settings;
use FiboFilters\WP\Helper as WPHelper;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class The7
 */
class The7 extends AbstractTheme {

	/** @var ElementorUtils */
	private $elementor_utils;

	public function __construct( ElementorUtils $elementor_utils, WPHelper $wp_helper, Front $front, Settings $settings, AssetsLoader $assets_loader ) {
		parent::__construct( $wp_helper, $front, $settings, $assets_loader );

		$this->elementor_utils = $elementor_utils;
	}

	protected function integrate(): void {
		add_filter( 'fibofilters/request/context_and_query_vars', [ $this, 'support_the7_wc_products_widget' ], 15, 2 );

		add_action(
			'elementor/element/the7-wc-products/section_query/before_section_end',
			[
				$this,
				'add_fibofilters_integration_switch',
			],
			10,
			2
		);
	}

	public function support_the7_wc_products_widget( array $args, \WP_Query $query ): array {
		if ( ! $this->elementor_utils->is_elementor_pro_active() ) {
			return $args;
		}

		list ( $context, $query_vars ) = $args;

		if ( ! empty( $query->queried_object->ID ) ) {
			/** @var \The7\Mods\Compatibility\Elementor\Widgets\Woocommerce\Products $element */
			$element = $this->elementor_utils->get_element_for_integrate_with_it_on_single( 'the7-wc-products', $query->queried_object->ID, true );

			// Run integration for single post/page with Woo Product Grid widget.
			if ( ! empty( $element && class_exists( '\The7\Mods\Compatibility\Elementor\Shortcode_Adapters\Query_Adapters\Products_Query' ) ) ) {
				$context['show_filters'] = true;

				$settings       = $element->get_active_settings();
				$products_query = new \The7\Mods\Compatibility\Elementor\Shortcode_Adapters\Query_Adapters\Products_Query( $settings, 'query_' );
				$query_vars     = $products_query->parse_query_args();

				$context['internal']['forced_query_args'] = [];

				if ( ! empty( $settings['loading_mode'] ) ) {
					if ( $settings['loading_mode'] === 'disabled' ) {
						$context['internal']['forced_query_args']['posts_per_page'] = $settings['dis_posts_total'] ?? 12;
						$context['internal']['forced_query_args']['nopaging']       = false;
					}
				}

				add_filter( 'fibofilters/config/single_products_fetch_url', fn() => get_permalink( $query->queried_object->ID ) );
			}
		} else {
			/** @var \The7\Mods\Compatibility\Elementor\Widgets\Woocommerce\Products $element */
			$element = $this->elementor_utils->get_element_for_integrate_with_it_on_archive( 'the7-wc-products', true );
		}
		if ( empty( $element ) ) {
			return $args;
		}

		$this->integrate_with_fibofilters( $element );

		return [ $context, $query_vars ];
	}

	public function integrate_with_fibofilters( $element ): void {
		$settings = $element->get_active_settings();

		/*
		 * ╭───────────────────────────╮
		 * | Config: products per page |
		 * ╰───────────────────────────╯
		 */
		if ( ! empty( $settings['archive_posts_per_page'] ) ) {
			add_filter(
				'fibofilters/config/products_per_page',
				fn() => intval( $settings['archive_posts_per_page'] ),
				15
			);
		}

		/**
		 * ╭─────────────────────╮
		 * | Pagination selector |
		 * ╰─────────────────────╯
		 */
		add_filter(
			'fibofilters/config/pagination_selectors',
			function ( $selectors ) {
				$selectors[] = '.paginator';

				return $selectors;
			}
		);

		/**
		 * ╭──────────────────────────────────────────────╮
		 * | Hook our placeholders before Products widget |
		 * ╰──────────────────────────────────────────────╯
		 */
		add_filter(
			'elementor/frontend/widget/before_render',
			function ( $element ) {
				if ( $element->get_name() === 'the7-wc-products' ) {
					do_action( 'fibofilters/front/display_load_previous_button_placeholder' );
					do_action( 'fibofilters/front/display_horizontal_filters_placeholder' );
					do_action( 'fibofilters/front/display_mobile_filters_placeholder' );
					do_action( 'fibofilters/front/display_applied_filters_placeholder' );
				}
			}
		);

		/**
		 * ╭─────────────────────────────────────────────╮
		 * | Hook our placeholders after Products widget |
		 * ╰─────────────────────────────────────────────╯
		 */
		add_filter(
			'elementor/frontend/widget/after_render',
			function ( $element ) {
				if ( $element->get_name() === 'the7-wc-products' ) {
					do_action( 'fibofilters/front/display_load_more_button_placeholder' );
					do_action( 'fibofilters/front/display_infinite_scroll_trigger' );
				}
			}
		);
	}

	/**
	 * Add "Integrate with FiboFilters" switch in "Products" widget
	 *
	 * @param \Elementor\Widget_Base $element Elementor widget.
	 */
	public function add_fibofilters_integration_switch( $element ): void {
		$element->add_control(
			'fibofilters_integration',
			[
				'label'   => _x( 'Integrate with FiboFilters', '[admin]', 'fibofilters' ),
				'type'    => \Elementor\Controls_Manager::SWITCHER,
				'default' => '',
			]
		);
	}
}
