<?php
declare( strict_types=1 );

namespace FiboFilters\Integrations\Plugins\Elementor;

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

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

/**
 * FiboFiltersWidget Class
 */
class FiboFiltersWidget extends Widget_Base {

	public function get_name(): string {
		return 'fibofilters';
	}

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

	public function get_icon(): string {
		return 'fiboicon-fibofilters';
	}

	public function get_categories(): array {
		return [ 'woocommerce-elements-archive' ];
	}

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

	public function get_custom_help_url(): string {
		// TODO Sprecyzować link do strony z opisem dla Elementora.
		return 'https://fibofilters.com/documentation/';
	}

	/**
	 * @return void
	 */
	protected function register_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->start_controls_section(
			'content_section',
			[
				'label' => esc_html_x( 'Appearance', '[admin]', 'fibofilters' ),
				'tab'   => \Elementor\Controls_Manager::TAB_LAYOUT,
			]
		);

		$this->add_control(
			'layout',
			[
				'type'        => \Elementor\Controls_Manager::SELECT,
				'label'       => esc_html_x( 'Layout', '[admin]', 'fibofilters' ),
				'options'     => [
					'vertical'   => esc_html_x( 'Vertical', '[admin]', 'fibofilters' ),
					'horizontal' => esc_html_x( 'Horizontal', '[admin]', 'fibofilters' ),
					'mobile'     => esc_html_x( 'Mobile', '[admin]', 'fibofilters' ),
				],
				'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->add_control(
				'layout_notice',
				[
					'type'       => \Elementor\Controls_Manager::ALERT,
					'alert_type' => 'warning',
					'heading'    => esc_html_x( 'WARNING', '[admin]', 'fibofilters' ),
					'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 // TODO Dokładny adres dla Elementora.
					),
					'condition'  => [
						'layout' => 'vertical',
					],
				]
			);
		}

		$this->add_control(
			'min_screen',
			[
				'label'       => esc_html_x( 'Minimum screen width', '[admin]', 'fibofilters' ),
				'type'        => \Elementor\Controls_Manager::NUMBER,
				'default'     => '',
				'min'         => 0,
				'step'        => 1,
				'placeholder' => 'px',
			]
		);

		$this->add_control(
			'max_screen',
			[
				'label'       => esc_html_x( 'Maximum screen width', '[admin]', 'fibofilters' ),
				'type'        => \Elementor\Controls_Manager::NUMBER,
				'default'     => '',
				'min'         => 0,
				'step'        => 1,
				'placeholder' => 'px',
			]
		);

		$this->end_controls_section();
	}

	/**
	 * @return void
	 */
	protected function render() {
		if ( class_exists( '\Elementor\Plugin' ) && \Elementor\Plugin::$instance->editor->is_edit_mode() ) { ?>
			<style>
				.fibofilters-elementor-placeholder {
					outline: 3px dashed #c3c4c7;
					padding: 10px;
					margin: 5px;
				}
			</style>
			<div class="fibofilters-elementor-placeholder">
				<?php echo esc_html_x( 'Your product filters will appear here.', '[admin]', 'fibofilters' ); ?>
			</div>
			<?php
			return;
		}

		$layout     = $this->get_settings_for_display( 'layout' );
		$min_screen = $this->get_settings_for_display( 'min_screen' );
		$max_screen = $this->get_settings_for_display( '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 do_shortcode( sprintf( '[fibofilters%s]', $shortcode_settings ) );
	}
}
