<?php
declare( strict_types=1 );

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

use FiboFilters\Config\Env;

/*
 * ╭────────╮
 * | Colors |
 * ╰────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {

		$base_color = '#0787ea';
		$option     = get_option( 'electro_options' );

		if ( empty( $option['use_predefined_color'] ) && ! empty( $option['custom_primary_color'] ) ) {
			$base_color = $option['custom_primary_color'];
		}

		if ( ! empty( $option['use_predefined_color'] ) && ! empty( $option['main_color'] ) ) {
			switch ( $option['main_color'] ) {
				case 'black':
					$base_color = '#000000';
					break;
				case 'blue':
					$base_color = '#0787ea';
					break;
				case 'flat-blue':
					$base_color = '#00abc5';
					break;
				case 'gold':
					$base_color = '#dab26d';
					break;
				case 'green':
					$base_color = '#a3d133';
					break;
				case 'grey':
					$base_color = '#808080';
					break;
				case 'orange':
					$base_color = '#f89a20';
					break;
				case 'pink':
					$base_color = '#ce1d76';
					break;
				case 'red':
					$base_color = '#ea1b25';
					break;
				case 'yellow':
					$base_color = '#fed700';
					break;
			}
		}

		wp_add_inline_style(
			'fibofilters',
			"body {
				--fibofilters--colors--base: $base_color;
			}"
		);

	}
);

/*
 * ╭─────────────────────╮
 * | Off-canvas position |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/config/offcanvas_position',
	function (): string {
		return 'left';
	}
);

/*
 * ╭───────────────────╮
 * | Products per page |
 * ╰───────────────────╯
 */
add_filter(
	'fibofilters/config/products_per_page',
	function (): int {
		$electro_default = 15;
		if ( function_exists( 'redux_apply_shop_loop_per_page' ) ) {
			return (int) redux_apply_shop_loop_per_page( $electro_default );
		}

		return $electro_default;
	}
);

/**
 * ╭─────────────────────╮
 * | Product placeholder |
 * ╰─────────────────────╯
 */
add_filter(
	'fibofilters/request/html/product_placeholder',
	function () {
		ob_start();
		?>
		<li class="product fibofilters-product-placeholder">
			<div class="product-outer product-item__outer">
				<div class="product-inner product-item__inner">
					<div class="product-loop-header product-item__header">
						<div class="product-thumbnail product-item__thumbnail">
							<img src="<?php echo esc_url( wc_placeholder_img_src() ); ?> " alt="Placeholder image"/>
						</div>
					</div>
				</div>
			</div>
		</li>
		<?php
		return (string) ob_get_clean();
	}
);

/*
 * ╭────────────╮
 * | Custom CSS |
 * ╰────────────╯
 */
add_action(
	'wp_enqueue_scripts',
	function () {
		$filter_location    = $this->get_config_value( 'filter_location' );
		$show_more_strategy = $this->get_config_value( 'products_loading_strategy' );
		// Custom CSS.
		ob_start();
		?>
		<style>
			<?php if($show_more_strategy !== 'pagination'): ?>
			.electro-advanced-pagination {
				display: none !important;
			}

			.form-electro-wc-ppp {
				display: none !important;
			}

			<?php endif; ?>

			<?php if($filter_location === 'above_products'): ?>

			@media (min-width: 1200px) {
				.content-area .shop-control-bar {
					border-radius: 9px 9px 0 0;
				}
			}

			.fibofilters-horizontal-filters-container {
				min-height: 35px;
			}

			<?php endif; ?>

			.fibofilters-horizontal-filters-container .fibofilters-horizontal-filters,
			.fibofilters-mobile-filters-container .fibofilters-mobile-filters {
				padding-bottom: 0;
				background: #efefef;
				padding-left: 1.3em;
			}

			@media (min-width: 1200px) {
				.fibofilters-horizontal-filters-container .fibofilters-horizontal-filters {
					border-radius: 0 0 9px 9px;
				}
			}

			@media (max-width: 450px) {
				.fibofilters-mobile-filters-container {
					min-height: 35px;
				}

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

			.theme-electro .fibofilters-applied-filters {
				padding: 1.5em 1.429em 1em 1.429em;
			}

			.sidebar .widget_electro_products_filter {
				padding-top: 5px;
			}

			.content-area .fibofilters-applied-filters-container {
				min-height: 50px;
			}


		</style>
		<?php
		$css = ob_get_clean();

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

/*
 * ╭──────────────────────────╮
 * | Hide the "Filter" button |
 * ╰──────────────────────────╯
 * Hide the "Filter" button that open off-canvas sidebar
 * if the filter location is set to "above_products".
 */
add_filter( 'electro_has_handheld_sidebar', function ( $has_handheld_sidebar ) {
	if ( $this->get_config_value( 'filter_location' ) === 'above_products' ) {
		return false;
	}

	return $has_handheld_sidebar;
} );

/*
 * ╭────────────────────╮
 * | Electro Off-canvas |
 * ╰────────────────────╯
 * Electro has own "Filter" button with off-canvas.
 * Filters in sidebar will be displayed in Electro Off-canvas on desktop and mobile devices.
 * That's why we have to disable switching to horizontal or mobile filters.
 * Instead of it we always uses Electro Off-canvas.
 */
if ( get_option( Env::$plugin_prefix . 'general_filter_location' ) === 'sidebar' ) {

	add_filter( 'fibofilters/settings/section=general',
		function ( array $settings ): array {


			$settings[290]['override'] = [
				'description' => $this->get_settings_description( 'overridden' ) . ' ' . _x( 'You choose a sidebar as a filter location. In this case the "horizontal layout on tablets" option has to be <b>disabled</b>.', '[admin]', 'fibofilters' ),
			];

			$settings[350]['override'] = [
				'description' => $this->get_settings_description( 'overridden' ) . ' ' . _x( 'You choose a sidebar as a filter location. In this case the mobile-optimized layout has to be <b>disabled</b>.', '[admin]', 'fibofilters' ),
			];

			return $settings;
		},
		20
	);

	add_filter( 'fibofilters/config/mobile_filters_enabled', '__return_false' );
	add_filter( 'fibofilters/config/tablet_filters_enabled', '__return_false' );
}

