<?php
declare( strict_types=1 );

namespace FiboFilters\Integrations\Themes;

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

/**
 * Class XStore
 */
class XStore extends AbstractTheme {

	public function init( string $slug = '', string $name = '', array $args = [] ): void {
		parent::init( $slug, $name, $args );

		/**
		 * Integration with Archive Products widget from XStore for Elementor.
		 */
		$this->maybe_add_integration_with_elementor();
		add_action(
			'elementor/element/woocommerce-etheme_archive_products/section_general/before_section_end',
			[
				$this,
				'add_controls_to_xstore_archive_products',
			],
			10,
			2
		);
		add_action( 'wp', [ $this, 'xstore_archive_products_widget_support' ] );
	}

	protected function maybe_add_integration_with_elementor() {
		if ( defined( 'ELEMENTOR_PRO_VERSION' ) && class_exists( '\ElementorPro\Plugin' ) ) {
			add_filter( 'fibofilters/settings/integrations_section_enabled', '__return_true' );

			add_filter(
				'fibofilters/settings/section=integrations',
				function ( array $settings ): array {
					$settings[20] = [
						'name'        => 'xstore_archive_products_widget_support',
						'type'        => 'toggle',
						'label'       => _x( 'Support for Archive Products widget from XStore', '[admin]', 'fibofilters' ),
						'description' => _x( 'If you use the Archive Products to display products in the catalog, this option should be enabled. Otherwise, the filters will not be displayed or will not work correctly.', '[admin]', 'fibofilters' ),
						'default'     => false,
						'schema'      => [
							'type' => 'boolean',
						],
						'group'       => 'integration_elementor',
					];

					return $settings;
				}
			);
		}
	}

	public function add_controls_to_xstore_archive_products( $element ): void {
		$element->add_control(
			'fibofilters_integration_pagination_note',
			[
				'type'       => \Elementor\Controls_Manager::ALERT,
				'alert_type' => 'warning',
				'heading'    => _x( 'FiboFilters', '[admin]', 'fibofilters' ),
				'content'    => _x( 'Pagination settings are not supported. You need to set the type to "Pagination" and configure the product loading method in the FiboFilters settings.', '[admin]', 'fibofilters' ),
			]
		);
	}

	public function xstore_archive_products_widget_support() {
		$support_for_xstore_archive_products = $this->settings->get( 'integrations_xstore_archive_products_widget_support' );

		if ( ! $support_for_xstore_archive_products ) {
			return;
		}

		// Break early if the page is not the main loop and contains a "Products" widget.
		$context = apply_filters( 'fibofilters/request/get_context', [] );
		if ( ! empty( $context ) && isset( $context['internal']['is_page_with_elementor_woocommerce_products'] ) ) {
			return;
		}

		add_filter(
			'elementor/frontend/widget/before_render',
			function ( $element ) {
				if ( $this->is_main_products_xstore_archive_products( $element ) ) {
					$element->add_render_attribute(
						'_wrapper',
						[
							'class' => 'fibofilters-xstore-archive-products',
						]
					);

					do_action( 'fibofilters/front/display_horizontal_filters_placeholder' );
					do_action( 'fibofilters/front/display_mobile_filters_placeholder' );
					do_action( 'fibofilters/front/display_applied_filters_placeholder' );
				}
			}
		);

		/**
		 * ╭───────────────────╮
		 * | Products selector |
		 * ╰───────────────────╯
		 */
		add_filter(
			'fibofilters/config/products_loop_selectors',
			function ( $selectors ) {
				$selectors[] = '.fibofilters-xstore-archive-products .etheme-product-grid';

				return $selectors;
			}
		);

		/*
		 * ╭────────────────────────────────────────────────╮
		 * | Products fetching strategy |
		 * ╰────────────────────────────────────────────────╯
		 */
		add_filter(
			'fibofilters/config/products_fetching_strategy',
			function () {
				return 'page';
			},
			20
		);

		/**
		 *  ╭─────────────────────╮
		 *  | Pagination selector |
		 *  ╰─────────────────────╯
		 *
		 * @param array $selectors Selectors.
		 */
		add_filter(
			'fibofilters/config/pagination_selectors',
			function ( array $selectors ) {
				$selectors[] = '.etheme-elementor-pagination';

				return $selectors;
			}
		);

		/**
		 * ╭─────────────────────╮
		 * | Product placeholder |
		 * ╰─────────────────────╯
		 */
		add_filter(
			'fibofilters/request/html/product_placeholder',
			function () {
				ob_start();
				?>
				<div class="product etheme-product-grid-item fibofilters-product-placeholder ">
					<div class="etheme-product-grid-image">
						<img src="<?php echo esc_url( wc_placeholder_img_src() ); ?> " alt="Placeholder image"/>
					</div>
				</div>
				<?php
				return (string) ob_get_clean();
			},
			15
		);
	}

	/**
	 * Check if specific Elementor widget is the Archive Products element, and it's main products loop.
	 *
	 * @param $element
	 */
	private function is_main_products_xstore_archive_products( $element ): bool {
		return is_object( $element ) &&
				is_a( $element, 'ETC\App\Controllers\Elementor\Theme_Builder\WooCommerce\Archive\Products' ) &&
				$element->get_settings( 'query_type' ) === 'current_query';
	}
}
