<?php
declare( strict_types=1 );

namespace FiboFilters\Config;

use FiboFilters\AdminPages\Pages\Debug as DebugPage;
use FiboFilters\AdminPages\Pages\Settings as SettingsPage;
use FiboFilters\DI\ContainerWrapper;
use FiboFilters\Indexer\BuildInfo;
use FiboFilters\Indexer\Config;
use Freemius;

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

/**
 * Class Menu
 */
class Menu {
	const MAIN_PAGE_SLUG  = 'fibofilters';
	const DEBUG_PAGE_SLUG = 'fibofilters-debug';

	/** @var BuildInfo */
	private $build_info;

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

	/** @var ContainerWrapper */
	private $container;

	/** @var Freemius */
	private $freemius;

	public function __construct(
		BuildInfo $build_info,
		Config $config,
		ContainerWrapper $container,
		Freemius $freemius
	) {
		$this->build_info = $build_info;
		$this->config     = $config;
		$this->container  = $container;
		$this->freemius   = $freemius;
	}

	public function init(): void {
		// With priority 18, this menu is just before FiboSearch (priority 20).
		add_action( 'admin_menu', [ $this, 'setup' ], 18 );
	}

	public function setup(): void {
		$menu_suffix = '';
		if ( $this->freemius->is__premium_only() ) {
			// TODO Zaimplementować Builder::isIndexerWorkingTooLong().
			if ( $this->build_info->get_info( 'status', $this->config->get_index_role() ) === 'error' ) {
				$menu_suffix = '<span class="fibofilters-menu-warning-icon">!</span>';
				add_action(
					'admin_print_styles',
					function () { ?>
					<style>
						.fibofilters-menu-warning-icon {
							display: inline-block;
							vertical-align: top;
							box-sizing: border-box;
							margin: 1px 0 -1px 3px;
							padding: 0 5px;
							min-width: 18px;
							height: 18px;
							border-radius: 9px;
							background-color: #d63638;
							color: #fff;
							font-size: 11px;
							line-height: 1.6;
							text-align: center;
							z-index: 26;
						}
					</style>
						<?php
					}
				);
			}
		}

		$shop_manager_has_access = apply_filters( 'fibofilters/shop_manager_has_access', false );

		add_submenu_page(
			'woocommerce',
			_x( 'FiboFilters', '[admin]', 'fibofilters' ),
			_x( 'FiboFilters', '[admin]', 'fibofilters' ) . $menu_suffix,
			$shop_manager_has_access ? 'manage_woocommerce' : 'manage_options',
			self::MAIN_PAGE_SLUG,
			[ $this, 'settings_page' ]
		);

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		if ( self::is_on_debug_admin_page() ) {
			add_submenu_page(
				'woocommerce',
				_x( 'FiboFilters Debug', '[admin]', 'fibofilters' ),
				sprintf( '<span class="fs-submenu-item fs-sub">%s</span>', _x( 'Debug', '[admin]', 'fibofilters' ) ),
				$shop_manager_has_access ? 'manage_woocommerce' : 'manage_options',
				self::DEBUG_PAGE_SLUG,
				[ $this, 'debug_page' ]
			);
		}
	}

	public function settings_page(): void {
		$this->container->get( SettingsPage::class )->render();
	}

	public function debug_page(): void {
		$this->container->get( DebugPage::class )->render();
	}

	/**
	 * Check if settings admin page
	 */
	public static function is_on_settings_admin_page(): bool {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		if ( is_admin() && ! empty( $_GET['page'] ) && sanitize_key( $_GET['page'] ) === self::MAIN_PAGE_SLUG ) {
			return true;
		}

		return false;
	}

	/**
	 * Check if debug admin page
	 */
	public static function is_on_debug_admin_page(): bool {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		if ( is_admin() && ! empty( $_GET['page'] ) && sanitize_key( $_GET['page'] ) === self::DEBUG_PAGE_SLUG ) {
			return true;
		}

		return false;
	}
}
