<?php

defined( 'ABSPATH' ) || exit;

class AIEO_Dependency_Manager {

	
	private $minimum_supported_wc_version;

	
	private $wc_active_version = null;

	
	private $wc_version_cached = false;

	
	public function __construct( $minimum_supported_wc_version ) {
		$this->minimum_supported_wc_version = $minimum_supported_wc_version;
	}

	
	public function has_valid_dependencies() {
		
		return $this->is_woocommerce_version_supported();
	}

	
	public function is_woocommerce_active() {
		if ( class_exists( 'WooCommerce' ) ) {
			return true;
		}

		return $this->get_woocommerce_active_version() !== null;
	}

	
	public function is_woocommerce_version_supported() {
		return version_compare(
			
			
			$this->get_woocommerce_active_version() ?? '',
			$this->minimum_supported_wc_version,
			'>='
		);
	}

	
	private function get_woocommerce_active_version() {
		if ( defined( 'WC_VERSION' ) ) {
			return WC_VERSION;
		}

		
		if ( true === $this->wc_version_cached ) {
			return $this->wc_active_version;
		}

		$this->wc_version_cached = true;

		
		if ( ! function_exists( 'is_plugin_active' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		
		foreach ( get_plugins() as $plugin_slug => $plugin_data ) {
			$is_woocommerce = false;

			
			if ( 'woocommerce/woocommerce.php' === $plugin_slug ) {
				$is_woocommerce = true;
			} elseif ( 'woocommerce.php' === basename( $plugin_slug ) && 'WooCommerce' === $plugin_data['Name'] ) {
				$is_woocommerce = true;
			}

			if ( $is_woocommerce && is_plugin_active( $plugin_slug ) ) {
				$this->wc_active_version = $plugin_data['Version'];
			}
		}

		return $this->wc_active_version;
	}

	
	public function display_dependency_admin_notice() {
		if ( ! current_user_can( 'activate_plugins' ) ) {
			return;
		}

		$admin_notice_content = '';

		if ( ! $this->is_woocommerce_active() ) {
			$install_url = wp_nonce_url(
				add_query_arg(
					array(
						'action' => 'install-plugin',
						'plugin' => 'woocommerce',
					),
					admin_url( 'update.php' )
				),
				'install-plugin_woocommerce'
			);

			
			$admin_notice_content = sprintf( esc_html__( '%1$sAI eshop Optimizer is inactive.%2$s The %3$sWooCommerce plugin%4$s must be active for AI eshop Optimizer to work. Please %5$sinstall & activate WooCommerce &raquo;%6$s', 'ai-eshop-optimizer' ), '<strong>', '</strong>', '<a href="http://wordpress.org/extend/plugins/woocommerce/">', '</a>', '<a href="' . esc_url( $install_url ) . '">', '</a>' );
		} elseif ( ! $this->is_woocommerce_version_supported() ) {
			
			$admin_notice_content = sprintf( esc_html__( '%1$sAI eshop Optimizer is inactive.%2$s This version of AI eshop Optimizer WooCommerce %3$s or newer. Please %4$supdate WooCommerce to version %3$s or newer &raquo;%5$s', 'ai-eshop-optimizer' ), '<strong>', '</strong>', $this->minimum_supported_wc_version, '<a href="' . esc_url( admin_url( 'plugins.php' ) ) . '">', '</a>' );
		}

		if ( $admin_notice_content ) {
			echo '<div class="error">';
			echo '<p>' . wp_kses_post( $admin_notice_content ) . '</p>';
			echo '</div>';
		}
	}
}
