<?php
declare( strict_types=1 );

namespace FiboFilters\Integrations\Themes\Bricks;

use FiboFilters\Config\Env;
use FiboFilters\DI\ContainerWrapper;
use FiboFilters\Settings\Settings;

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

/**
 * FiboFiltersElement class
 */
class FiboFiltersElement extends \Bricks\Element {
	/** @var string */
	public $category = 'woocommerce';
	/** @var string */
	public $name = 'fibofilters-filters';
	/** @var string */
	public $icon = 'fiboicon-fibofilters';

	public function get_label() {
		return esc_html_x( 'FiboFilters', '[admin]', 'fibofilters' );
	}

	public function get_keywords() {
		return [ 'fibo', 'filters', 'fibofilters' ];
	}

	public function set_control_groups() {
		// Remove default control groups.
		unset( $this->control_groups['_attributes'] );
		unset( $this->control_groups['_typography'] );
		unset( $this->control_groups['_background'] );
		unset( $this->control_groups['_gradient'] );
		unset( $this->control_groups['_shapes'] );
		unset( $this->control_groups['_transform'] );
		unset( $this->control_groups['_border'] );
	}

	public function set_controls() {
		/** @var Settings $settings */
		$settings        = ContainerWrapper::get_instance()->get( Settings::class );
		$filter_location = apply_filters( 'fibofilters/config/filter_location', $settings->get( 'general_filter_location', 'above_products' ) );

		$plugin_settings_page = esc_url( admin_url( 'admin.php?page=fibofilters' ) );

		$this->controls['layout'] = [
			'tab'         => 'content',
			'label'       => esc_html_x( 'Layout', '[admin]', 'fibofilters' ),
			'type'        => 'select',
			'options'     => [
				'vertical'   => esc_html_x( 'Vertical', '[admin]', 'fibofilters' ),
				'horizontal' => esc_html_x( 'Horizontal', '[admin]', 'fibofilters' ),
				'mobile'     => esc_html_x( 'Mobile', '[admin]', 'fibofilters' ),
			],
			'inline'      => true,
			'clearable'   => false,
			'pasteStyles' => false,
			'default'     => 'vertical',
			'description' => wp_kses_post( sprintf( _x( 'If you want to select "Vertical" layout, make sure you have selected the "<b>Sidebar</b>" option in the <b><a href="%s" target="_blank">Filters location</a></b> in the FiboFilters settings.', '[admin]', 'fibofilters' ), $plugin_settings_page ) ) . ' <br /><br /> ' .
							wp_kses_post( sprintf( _x( 'Otherwise, if you want to select "<b>Horizontal</b>" layout, make sure you have selected the "<b>Above the products</b>" option in the <b><a href="%s" target="_blank">Filters location</a></b> in the FiboFilters settings.', '[admin]', 'fibofilters' ), $plugin_settings_page ) ),
		];

		if ( $filter_location === 'above_products' ) {
			$this->controls['layout_notice'] = [
				'tab'      => 'content',
				'content'  => sprintf(
				/* translators: 1: Settings URL 2: Documentation URL */
					_x(
						'You have selected filter <a href="%1$s" target="_blank">location option to "Above the products"</a>. This option causes all vertical filters to be hidden. If you want to override this, set the following visibility conditions for this element. Learn more in the <a href="%2$s" target="_blank">documentation</a>.',
						'[admin]',
						'fibofilters'
					),
					$plugin_settings_page,
					Env::$doc_url . 'themes-integrations/bricks/',
				),
				'type'     => 'info',
				'required' => [ 'layout', '=', 'vertical' ],
			];
		}

		$this->controls['min_screen'] = [
			'tab'   => 'content',
			'label' => esc_html_x( 'Minimum screen width', '[admin]', 'fibofilters' ),
			'type'  => 'number',
			'step'  => 1,
		];

		$this->controls['max_screen'] = [
			'tab'   => 'content',
			'label' => esc_html_x( 'Maximum screen width', '[admin]', 'fibofilters' ),
			'type'  => 'number',
			'step'  => 1,
		];
	}

	public function render() {
		if ( ! $this->is_frontend ) {
			$this->render_element_placeholder(
				[
					'title' => esc_html_x( 'Your product filters will appear here.', '[admin]', 'fibofilters' ),
				]
			);
			return;
		}

		$layout     = $this->settings['layout'] ?? '';
		$min_screen = $this->settings['min_screen'] ?? '';
		$max_screen = $this->settings['max_screen'] ?? '';

		$shortcode_settings = '';
		if ( ! empty( $layout ) ) {
			$shortcode_settings .= sprintf( ' layout="%s"', $layout );
		}
		if ( ! empty( $min_screen ) ) {
			$shortcode_settings .= sprintf( ' min_screen="%d"', $min_screen );
		}
		if ( ! empty( $max_screen ) ) {
			$shortcode_settings .= sprintf( ' max_screen="%d"', $max_screen );
		}

		echo "<div {$this->render_attributes( '_root' )}>";
		echo do_shortcode( sprintf( '[fibofilters%s]', $shortcode_settings ) );
		echo '</div>';
	}
}
