<?php
declare( strict_types=1 );

namespace FiboFilters\Integrations\Themes;

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

/**
 * Class Electro
 */
class Electro extends AbstractTheme {
	public function init( string $slug = '', string $name = '', array $args = [] ): void {
		parent::init( $slug, $name, $args );

		$this->change_enable_integration_field_desc();
	}

	/**
	 * Get a positive message about a location of the widget.
	 *
	 * @param string $widget_name
	 * @param string $widget_area
	 *
	 * @return string
	 */
	private function widget_ok_message( string $widget_name, string $widget_area ): string {

		$html  = '<p class="fibofilters-settings-location-sidebar-ok">';
		$html .= '<span class="dashicons dashicons-yes-alt"></span>';
		$html .= '<span>';
		/* translators: %s: name of the widget or widget area */
		$html .= sprintf( _x( 'Widget %1$s is added to the widget area %2$s.', '[admin]', 'fibofilters' ), '<b>' . $widget_name . '</b>', '<b>' . $widget_area . '</b>' );
		$html .= '</span>';
		$html .= '</p>';

		return $html;
	}

	/**
	 * Get a negative message about a location of the widget.
	 *
	 * @param string $widget_name
	 * @param string $widget_area
	 *
	 * @return string
	 */
	private function widget_error_message( string $widget_name, string $widget_area ): string {

		$html  = '<p class="fibofilters-settings-location-sidebar-error">';
		$html .= '<span class="dashicons dashicons-dismiss"></span>';
		$html .= '<span>';
		/* translators: %s: name of the widget or widget area */
		$html .= sprintf( _x( 'Widget %1$s should be added to the widget area %2$s.', '[admin]', 'fibofilters' ), '<b>' . $widget_name . '</b>', '<b>' . $widget_area . '</b>' );
		$html .= '</span>';
		$html .= '</p>';

		return $html;
	}

	/**
	 * Adds extra description for the "Enable integration" option.
	 */
	protected function change_enable_integration_field_desc(): void {

		add_filter(
			'fibofilters/settings/section=general',
			function ( array $settings ): array {
				// Filter location.
				/* translators: %s: name of the theme */
				$settings[200]['description'] = sprintf( _x( 'Filters can be displayed in these modes in the <b>%s</b> theme:', '[admin]', 'fibofilters' ), $this->name );

				$electro_doc  = 'https://docs.madrasthemes.com/electro/topics/blocks-sidebars/sidebars/';
				$sidebar_html = '';

				$data = [
					'cond1' => [
						'widget' => [
							'id'    => 'fibofilters-filters-widget',
							'label' => 'FiboFilters - product filters',
						],
						'area'   => [
							'id'    => 'product-filters-widgets',
							'label' => 'Product Filters',
						],
					],
					'cond2' => [
						'widget' => [
							'id'    => 'electro_products_filter',
							'label' => 'Electro Product Filter',
						],
						'area'   => [
							'id'    => 'shop-sidebar-widgets',
							'label' => 'Shop Sidebar',
						],
					],
				];

				$condition_1 = $this->wp_helper->is_widget_in_widget_area( $data['cond1']['widget']['id'], $data['cond1']['area']['id'] );
				$condition_2 = $this->wp_helper->is_widget_in_widget_area( $data['cond2']['widget']['id'], $data['cond2']['area']['id'] );

				if ( $condition_1 && $condition_2 ) {
					$sidebar_html .= sprintf( _x( '<b>Well done!</b> All widgets required by %s theme are added to the proper widget areas.', '[admin]', 'fibofilters' ), $this->name );
					$sidebar_html .= $this->widget_ok_message( $data['cond1']['widget']['label'], $data['cond1']['area']['label'] );
					$sidebar_html .= $this->widget_ok_message( $data['cond2']['widget']['label'], $data['cond2']['area']['label'] );
				}

				if ( $condition_1 && ! $condition_2 ) {
					$sidebar_html .= sprintf( _x( '<b>Almost done!</b> Only part of widgets required by %s theme are added to the proper widget areas.', '[admin]', 'fibofilters' ), $this->name );
					$sidebar_html .= $this->widget_ok_message( $data['cond1']['widget']['label'], $data['cond1']['area']['label'] );
					$sidebar_html .= $this->widget_error_message( $data['cond2']['widget']['label'], $data['cond2']['area']['label'] );

				}

				if ( ! $condition_1 && $condition_2 ) {
					$sidebar_html .= sprintf( _x( '<b>Almost done!</b> Only part of widgets required by %s theme are added to the proper widget areas.', '[admin]', 'fibofilters' ), $this->name );
					$sidebar_html .= $this->widget_error_message( $data['cond1']['widget']['label'], $data['cond1']['area']['label'] );
					$sidebar_html .= $this->widget_ok_message( $data['cond2']['widget']['label'], $data['cond2']['area']['label'] );
				}

				if ( ! $condition_1 && ! $condition_2 ) {
					/* translators: %s: name of the theme */
					$sidebar_html .= '<p>' . sprintf( _x( 'To display FiboFilters in the %s theme Sidebar, follow these steps:', '[admin]', 'fibofilters' ), $this->name ) . '</p>';
					$sidebar_html .= '<ol>';
					/* translators: %s: URL address to the settings page */
					$sidebar_html .= '<li>' . sprintf( _x( 'Go to <code><a href="%s" target="_blank">Electro -> Shop</a></code> and find the "Shop Page Layout" option.', '[admin]', 'fibofilters' ), esc_url( admin_url( 'admin.php?page=theme_options&tab=2' ) ) ) . '</li>';
					$sidebar_html .= '<li>' . _x( 'Make sure you have selected <i>Left Sidebar</i> or <i>Right Sidebar</i>.', '[admin]', 'fibofilters' ) . '</li>';
					/* translators: %s: URL address to the WordPress widgets view */
					$sidebar_html .= '<li>' . sprintf( _x( 'Go to <code><a href="%s" target="_blank">Appearance -> Widgets</a></code>.', '[admin]', 'fibofilters' ), $this->get_widgets_link() ) . '</li>';
					$sidebar_html .= '<li>' . sprintf( _x( 'Add a widget called "<b>FiboFilters - product filters</b>" to the %s area.', '[admin]', 'fibofilters' ), '<b>Product Filters</b>' ) . '</li>';
					$sidebar_html .= '<li>' . sprintf( _x( 'Then add an "<b>Electro Product Filter</b>" to the %s area.', '[admin]', 'fibofilters' ), '<b>Shop Sidebar</b>' ) . '</li>';
					/* translators: 1: URL addres of the theme documentation 2: name of the theme */
					$sidebar_html .= '<li>' . sprintf( _x( 'Learn more in the <a href="%1$s" target="_blank">%2$s documentation.</a>', '[admin]', 'fibofilters' ), $electro_doc, $this->name ) . '</li>';
					$sidebar_html .= '</ol>';
				}

				$settings[260]['html'] = $sidebar_html;

				return $settings;
			},
			15
		);
	}
}
