<?php
declare( strict_types=1 );

namespace FiboFilters\Integrations\Themes;


use FiboFilters\Util\Helpers;

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

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

		add_action(
			'after_setup_theme',
			function () {
				$this->change_enable_integration_field_desc();
			}
		);
	}


	/**
	 * 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 );

				// Filter location - Sidebar.
				$added        = false;
				$sidebar_html = '';

				$sidebar_pos = 'right-sidebar';
				if ( function_exists( 'generate_get_option' ) ) {
					$sidebar_pos = generate_get_option( 'blog_layout_setting' );
				}

				foreach ( $this->wp_helper->get_widget_areas_containing_ff_elements() as $key => $widget_area ) {

					$left_sidebar_passed  = in_array( $sidebar_pos, [ 'left-sidebar' ] ) && $key === 'sidebar-2';
					$right_sidebar_passed = in_array( $sidebar_pos, [ 'right-sidebar' ] ) && $key === 'sidebar-1';

					if ( $left_sidebar_passed || $right_sidebar_passed ) {
						$sidebar_html .= '<p class="fibofilters-settings-location-sidebar-ok">';
						$sidebar_html .= '<span class="dashicons dashicons-yes-alt"></span>';
						$sidebar_html .= '<span>';
						/* translators: %s: name of the sidebar */
						$sidebar_html .= sprintf( _x( '<b>Well done!</b> Filters have been successfully added as a WordPress widget under the widget area called “<b>%s</b>”.', '[admin]', 'fibofilters' ), $widget_area['name'] );
						$sidebar_html .= '</span>';
						$sidebar_html .= '</p>';
						$added        = true;
						break;
					}
				}

				if ( ! $added ) {
					/* 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 WordPress customizer view */
					$sidebar_html .= '<li>' . sprintf( _x( 'Go to <code><a href="%s" target="_blank">Appearance -> Customize -> Layout -> Sidebars</a></code>.', '[admin]', 'fibofilters' ), Helpers::get_customizer_link( '', 'generate_layout_sidebars' ) ) . '</li>';
					$sidebar_html .= '<li>' . _x( 'In the option <b>Blog Sidebar Layout</b> select "<i>Sidebar / Content</i>" or "<i>Content / Sidebar</i>" to display left or right sidebar.', '[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>' . _x( 'Add a block called <i>FiboFilters</i> to the <b>Right Sidebar</b> or <b>Left Sidebar</b> depending on which sidebar layout you set in the previous step.', '[admin]', 'fibofilters' ) . '</li>';
					$sidebar_html .= '</ol>';
				}

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

				return $settings;
			},
			15
		);
	}

}
