<?php
declare( strict_types=1 );

namespace FiboFilters\Integrations\Plugins\Breakdance;

use FiboFilters\Config\Env;
use FiboFilters\Integrations\Plugins\AbstractPlugin;
use FiboFilters\Settings\Settings;

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

/**
 * Class Breakdance
 */
class Breakdance extends AbstractPlugin {

	public function early_init(): void {
		add_action( 'breakdance_loaded', [ $this, 'register_elements' ], 9 );
	}

	public function init(): void {
		add_action( 'wp_enqueue_scripts', [ $this, 'custom_styles' ] );
		add_filter( 'fibofilters/config/products_fetching_strategy', fn() => 'page' );
		add_filter( 'fibofilters/config/products_per_page', [ $this, 'products_per_page' ] );
		add_filter( 'fibofilters/config/products_loop_selectors', [ $this, 'products_loop_selectors' ] );
	}


	public function register_elements() {
		\Breakdance\ElementStudio\registerSaveLocation(
			Env::$plugin_folder . '/lib/Integrations/Plugins/Breakdance/elements',
			'FiboFiltersElements',
			'element',
			'FiboFilters Elements',
			false,
			true
		);
	}

	public function products_per_page( $products_per_page ) {
		if ( function_exists( '\Breakdance\WooCommerce\getProductListSetting' ) ) {
			$products_per_page = \Breakdance\WooCommerce\getProductListSetting( 'products_per_page', $products_per_page );
		}

		return $products_per_page;
	}

	public function products_loop_selectors() {
		return [ '.bde-wooshoppage ul.products' ];
	}

	public function custom_styles() {
		$context = apply_filters( 'fibofilters/request/get_context', [] );

		if ( empty( $context['show_filters'] ) ) {
			return;
		}

		// Custom CSS.
		ob_start();
		?>
		<style>
			.fibofilters-show-more {
				background: var(--bde-button-secondary-background-color);
				color: var(--bde-button-secondary-text-color);
				border-color: var(--bde-button-secondary-border-color);
				border-radius: var(--bde-button-border-radius);
				border-width: var(--bde-button-border-width);

				font-size: var(--bde-button-font-size);
				line-height: var(--bde-button-line-height);
				font-weight: var(--bde-button-font-weight);
				transition: var(--bde-woo-base-transition);
				transition-duration: var(--bde-transition-duration);

				display: inline-flex;
				border-style: solid;
				text-transform: capitalize;
				padding: var(--bde-button-padding-base);
				text-decoration: none;
				text-align: center;
				justify-content: center;
				align-items: center;
				cursor: pointer;
				width: max-content;
				gap: var(--bde-woo-base-small-gaps);
			}

			.fibofilters-show-more:hover {
				background-color: var(--bde-button-secondary-background-color-hover);
				color: var(--bde-button-secondary-text-color-hover);
				border-color: var(--bde-button-secondary-border-color-hover);
			}

			body {
				--fibofilters--colors--base: var( --bde-brand-primary-color, var( --fibofilters--colors--base-org ) );
				--fibofilters--colors--link: var( --bde-links-color, var( --fibofilters--colors--link-org ) );
			}

			.bde-wooshoppage {
				gap: var(--bde-woo-base-big-gaps) !important;
			}

			.fibofilters-horizontal-filters-container .fibofilters-horizontal-filters {
				padding-bottom: 0;
			}

			.fibofilters-mobile-filters-container .fibofilters-mobile-filters {
				padding-bottom: 0;
			}

			.fibofilters-applied-filters {
				padding-bottom: 0;
			}

			<?php
			if ( $this->get_config_value( 'filter_location' ) === 'above_products' && $this->get_config_value( 'mobile_filters_enabled' ) ) {
				?>
				@media screen and ( min-width: 451px ) {
					.fibofilters-horizontal-filters-container {
						min-height: 48px !important;
					}
				}
			<?php } ?>
		</style>
		<?php
		$css = ob_get_clean();

		wp_add_inline_style(
			'fibofilters',
			str_replace( [ '<style>', '</style>' ], '', $css )
		);
	}
}
