<?php
declare( strict_types=1 );

namespace FiboFilters\Config;

use FiboFilters\Filters\FiltersDebugger;
use FiboFilters\Filters\StoreFilters;
use FiboFilters\Indexer\BuildInfo;
use FiboFilters\Indexer\Config;

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

/**
 * AdminBar class.
 */
class AdminBar {
	/** @var BuildInfo */
	private $build_info;

	/** @var Config */
	private $config;

	/** @var FiltersDebugger */
	private $filters_debugger;

	/** @var StoreFilters */
	private $store_filters;

	public function __construct(
		BuildInfo $build_info,
		Config $config,
		StoreFilters $store_filters,
		FiltersDebugger $filters_debugger
	) {
		$this->build_info       = $build_info;
		$this->config           = $config;
		$this->store_filters    = $store_filters;
		$this->filters_debugger = $filters_debugger;
	}

	/**
	 * @param \WP_Admin_Bar $wp_admin_bar Admin bar.
	 *
	 * @return void
	 */
	public function init( $wp_admin_bar ) {
		$minimum_capability = apply_filters( 'fibofilters/shop_manager_has_access', false ) ? 'manage_woocommerce' : 'manage_options';
		if ( ! current_user_can( $minimum_capability ) ) {
			return;
		}

		$status = $this->build_info->get_info( 'status' );

		$settings_url = admin_url( 'admin.php?page=fibofilters#indexer' );

		$notices = [];
		$class   = '';

		if ( empty( $this->store_filters->get_all_filters() ) ) {
			$notices[] = '<p>' . _x( "Filters don't work because they haven't been configured yet.", '[admin]', 'fibofilters' ) . '</p>';
			$class     = 'warning';
		} elseif ( $status !== 'completed' ) {
			$notices[] = '<p>' . _x( "Your filters do not work because the index hasn't been built yet.", '[admin]', 'fibofilters' ) . '</p>';
			$class     = 'warning';
		}

		if (
			( $this->config->is_parallel_building_enabled() && $this->build_info->get_info( 'status', 'tmp' ) === 'error' ) ||
			( ! $this->config->is_parallel_building_enabled() && $this->build_info->get_info( 'status', 'main' ) === 'error' )
		) {
			$notices[] = '<p>' . _x( 'The index could not be built!', '[admin]', 'fibofilters' ) . '<span class="fibofilters-warning-icon">!</span></p>';
			$class     = 'error';
		}

		$context = is_admin() ? [] : apply_filters( 'fibofilters/request/get_context', [] );

		if ( empty( $notices ) && ( empty( $context['show_filters'] ) || ! $this->filters_debugger->is_enabled() ) ) {
			return;
		}

		if ( isset( $context['show_filters'] ) && $context['show_filters'] && $this->filters_debugger->is_enabled() ) {
			$notices[] = '<ul class="ab-submenu"><li><a href="' . esc_url( $settings_url ) . '">' . _x( 'Go to plugin settings →', '[admin]', 'fibofilters' ) . '</a></li><li><a class="fibofilters-show-filters-debugger" href="#">' . _x( 'Show Filters Debugger', '[admin]', 'fibofilters' ) . '<span class="fibofilters-warning-icon">!</span></a></li></ul>';
		} else {
			$notices[] = '<ul class="ab-submenu"><li><a href="' . esc_url( $settings_url ) . '">' . _x( 'Go to plugin settings →', '[admin]', 'fibofilters' ) . '</a></li></ul>';
		}

		$wp_admin_bar->add_node(
			[
				'id'    => 'fibofilters',
				'title' => Env::$plugin_full_name,
				'href'  => $settings_url,
				'meta'  => [
					'class' => 'fibofilters-status-' . esc_attr( $class ) . ' menupop',
					'html'  => '<div class="ab-sub-wrapper">' . join( '', $notices ) . '</div>',
				],
			]
		);

		$this->add_styles();
	}

	private function add_styles(): void {
		$hook = is_admin() ? 'admin_footer' : 'wp_footer';

		add_action(
			$hook,
			function () {
				?>
				<style>
					#wp-admin-bar-fibofilters.fibofilters-status-warning,
					#wp-admin-bar-fibofilters.fibofilters-front-warnings
					{
						background-color: #f0b849;
					}

					#wp-admin-bar-fibofilters.fibofilters-status-error,
					#wp-admin-bar-fibofilters.fibofilters-front-errors
					{
						background-color: #d63638;
					}

					#wp-admin-bar-fibofilters .ab-sub-wrapper {
						min-width: 250px !important;
					}

					#wp-admin-bar-fibofilters .ab-sub-wrapper p {
						padding: 10px 10px 0 10px !important;
						line-height: 1.5em;
					}

					#wp-admin-bar-fibofilters .fibofilters-warning-icon {
						display: none;
						vertical-align: top;
						box-sizing: border-box;
						margin: 8px 0 0 3px;
						padding: 0 5px;
						min-width: 16px;
						height: 16px;
						border-radius: 8px;
						background-color: #d63638;
						color: #fff;
						font-size: 12px;
						line-height: 1.4;
						text-align: center;
					}

					#wp-admin-bar-fibofilters p .fibofilters-warning-icon {
						display: inline-block;
						margin: 0 0 0 3px;
						padding-right: 6px;
					}

					#wp-admin-bar-fibofilters.fibofilters-front-errors a .fibofilters-warning-icon {
						display: inline-block;
					}
				</style>
				<?php
			},
			PHP_INT_MAX - 10
		);
	}
}
