<?php

namespace WPKIN_SHOP_MANAGER\Admin;

if ( ! defined( 'ABSPATH' ) ) {
	wp_die( esc_html__( 'You can\'t access this page', 'shop-manager' ) );
}

use WPKIN_SHOP_MANAGER\Helper;

/**
 * Admin Menu Class
 *
 * @since 1.0.0
 */
class Notice {

	/**
	 * Notice class constructor
	 *
	 * @since 1.0.0
	 */
	public function __construct() {

		$this->wpkin_notice_init();
	}

	/**
	 * Method notice_init.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 */
	public function wpkin_notice_init() {

		// Check for required PHP version.
		if ( version_compare( PHP_VERSION, WPKIN_SM_MINIMUM_PHP_VERSION, '<' ) ) {
			add_action( 'admin_notices', [ $this, 'wpkin_minimum_php_version' ] );
			return;
		}

		// Check if WooCommerce installed and activated.
		if ( ! class_exists( 'WooCommerce' ) ) {
			add_action( 'admin_notices', [ $this, 'wpkin_admin_notice_missing_main_plugin' ] );
			return;
		}

		// Check for required Woocommerce version.
		if ( version_compare( WC_VERSION, WPKIN_SM_MINIMUM_WC_VERSION, '<' ) ) {
			add_action( 'admin_notices', [ $this, 'wpkin_minimum_wc_version' ] );
			return;
		}
	}

	/**
	 * Method wpkin_minimum_php_version.
	 *
	 * Warning when the site doesn't have a minimum required PHP version.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 */
	public function wpkin_minimum_php_version() {

		$message = sprintf(
			/* translators: 1: Plugin name 2: PHP 3: Required PHP version */
			esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'shop-manager' ),
			'<strong>' . esc_html__( 'Shop Manager', 'shop-manager' ) . '</strong>',
			'<strong>' . esc_html__( 'PHP', 'shop-manager' ) . '</strong>',
			WPKIN_SM_MINIMUM_PHP_VERSION
		);

		printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', wp_kses( $message, Helper::wpkin_plugins_allowedtags() ) );
	}

	/**
	 * Method wpkin_admin_notice_missing_main_plugin.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 */
	public function wpkin_admin_notice_missing_main_plugin() {

		$screen = get_current_screen();
		if ( isset( $screen->parent_file ) && 'plugins.php' === $screen->parent_file && 'update' === $screen->id ) {
			return;
		}

		$plugin            = 'woocommerce';
		$file_path         = 'woocommerce/woocommerce.php';
		$installed_plugins = get_plugins();

		if ( isset( $installed_plugins[ $file_path ] ) ) { // check if plugin is installed.

			if ( ! current_user_can( 'activate_plugins' ) ) {
				return;
			}
			$activation_url = wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=' . $file_path ), 'activate-plugin_' . $file_path );

			$message  = wp_sprintf( '<p><strong>%s</strong>%s</p>', esc_html__( 'Shop Manager', 'shop-manager' ), __( ' not working because you need to activate the WooCommerce plugin.', 'shop-manager' ) );
			$message .= wp_sprintf( '<p><a href="%s" class="button-primary">%s</a></p>', $activation_url, esc_html__( 'Activate WooCommerce Now', 'shop-manager' ) );
		} else {
			if ( ! current_user_can( 'install_plugins' ) ) {
				return;
			}
			$install_url = wp_nonce_url(
                add_query_arg(
                    [
						'action' => 'install-plugin',
						'plugin' => $plugin,
                    ],
					admin_url( 'update.php' )
                ),
                'install-plugin_' . $plugin
            );
			$message     = wp_sprintf( '<p><strong>%s</strong>%s</p>', esc_html__( 'Shop Manager', 'shop-manager' ), __( ' not working because you need to install the WooCommerce plugin.', 'shop-manager' ) );
			$message    .= wp_sprintf( '<p><a href="%s" class="button-primary">%s</a></p>', $install_url, esc_html__( 'Install WooCommerce Now', 'shop-manager' ) );
		}

		printf( '<div class="error"><p>%s</p></div>', wp_kses( $message, Helper::wpkin_plugins_allowedtags() ) );
	}

	/**
	 * Method wpkin_minimum_wc_version.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 */
	public function wpkin_minimum_wc_version() {

		printf(
            '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', '%1$s' . esc_html__( ' requires ', 'shop-manager' ) . ' %2$s' . esc_html__( ' version ', 'shop-manager' ) . '%3$s' . esc_html__( 'or greater.', 'shop-manager' ),
            '<strong>' . esc_html__( 'Shop Manager', 'shop-manager' ) . '</strong>',
            '<strong>' . esc_html__( 'Woocommerce', 'shop-manager' ) . '</strong>',
            floatval( WPKIN_SM_MINIMUM_WC_VERSION )
        );
	}
}
