<?php
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * The main Sifency Addons Notice class.
 *
 * @final
 * @since 1.0.4
 */
final class Sifency_Addons_Notice {

	/**
	 * Instance of the class.
	 *
	 * @var Sifency_Addons_Notice|null
	 */
	private static $instance = null;

	/**
	 * Singleton instance.
	 *
	 * @return Sifency_Addons_Notice
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}
    
	/**
	 * Checks plugin requirements.
	 *
	 * @return bool
	 */
	public function check_requirements() {

		if ( version_compare( PHP_VERSION, SIFENCY_ADDONS_MIN_PHP_VERSION, '<' ) ) {
			add_action( 'admin_notices', [ $this, 'admin_notice_minimum_php_version' ] );
			return false;
		}

		if ( version_compare( get_bloginfo( 'version' ), SIFENCY_ADDONS_MIN_WP_VERSION, '<' ) ) {
			add_action( 'admin_notices', [ $this, 'admin_notice_minimum_wp_version' ] );
			return false;
		}

		if ( ! did_action( 'elementor/loaded' ) ) {
			add_action( 'admin_notices', [ $this, 'admin_notice_missing_main_plugin' ] );
			return false;
		}

		if ( defined( 'ELEMENTOR_VERSION' ) && version_compare( ELEMENTOR_VERSION, SIFENCY_MIN_ELEMENTOR_VERSION, '<' ) ) {
			add_action( 'admin_notices', [ $this, 'admin_notice_minimum_elementor_version' ] );
			return false;
		}

		return true;
	}

	/**
	 * Admin notice: Missing Elementor plugin.
	 */
	public function admin_notice_missing_main_plugin() {
		if ( file_exists( WP_PLUGIN_DIR . '/elementor/elementor.php' ) ) {
			$elementor_link = wp_nonce_url(
				self_admin_url( 'plugins.php?action=activate&plugin=elementor/elementor.php' ),
				'activate-plugin_elementor/elementor.php'
			);
			$elementor_action = esc_html__( 'Activate Elementor', 'sifency-addons' );
		} else {
			$elementor_link = wp_nonce_url(
				self_admin_url( 'update.php?action=install-plugin&plugin=elementor' ),
				'install-plugin_elementor'
			);
			$elementor_action = esc_html__( 'Install Elementor', 'sifency-addons' );
		}

		$this->render_sifency_admin_notice(
			sprintf(
				'<div class="sifency-addons-notice-content sifency-flex-wrapper">%1$s %2$s</div>',
				sprintf(
					/* translators: 1: Plugin name, 2: Required plugin name. */
					esc_html__( '"%1$s" requires "%2$s" to be installed and activated.', 'sifency-addons' ),
					'<strong>' . esc_html__( 'Sifency Addons', 'sifency-addons' ) . '</strong>',
					'<strong>' . esc_html__( 'Elementor', 'sifency-addons' ) . '</strong>'
				),
				sprintf(
					' <a href="%s" class="sifency-addons-button button-primary sifency-admin-btn">%s</a>',
					esc_url( $elementor_link ),
					esc_html( $elementor_action )
				)
			)
		);

	}


	/**
	 * Admin notice: Minimum Elementor version.
	 */
	public function admin_notice_minimum_elementor_version() {
		$this->render_sifency_admin_notice(
			sprintf(
				/* translators: 1: Plugin name, 2: Required plugin name. */
				esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'sifency-addons' ),
				'<strong>' . esc_html__( 'Sifency Addons', 'sifency-addons' ) . '</strong>',
				'<strong>' . esc_html__( 'Elementor', 'sifency-addons' ) . '</strong>',
				SIFENCY_MIN_ELEMENTOR_VERSION
			)
		);
	}

	/**
	 * Admin notice: Minimum PHP version.
	 */
	public function admin_notice_minimum_php_version() {
		$this->render_sifency_admin_notice(
			sprintf(
				/* translators: 1: Plugin name, 2: Required PHP name. */
				esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'sifency-addons' ),
				'<strong>' . esc_html__( 'Sifency Addons', 'sifency-addons' ) . '</strong>',
				'<strong>' . esc_html__( 'PHP', 'sifency-addons' ) . '</strong>',
				SIFENCY_ADDONS_MIN_PHP_VERSION
			)
		);
	}

	/**
	 * Admin notice: Minimum WordPress version.
	 */
	public function admin_notice_minimum_wp_version() {
		$this->render_sifency_admin_notice(
			sprintf(
				/* translators: 1: Plugin name, 2: Required WordPress name. */
				esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'sifency-addons' ),
				'<strong>' . esc_html__( 'Sifency Addons', 'sifency-addons' ) . '</strong>',
				'<strong>' . esc_html__( 'WordPress', 'sifency-addons' ) . '</strong>',
				SIFENCY_ADDONS_MIN_WP_VERSION
			)
		);
	}

	/**
	 * Renders an admin notice message.
	 *
	 * @param string $message Message to display.
	 */
	private function render_sifency_admin_notice( $message ) {
		printf(
			'<div class="notice notice-error"><p>%s</p></div>',
			wp_kses_post( $message )
		);
	}
}
