<?php

namespace EDPW\Core\Helpers;
defined( "ABSPATH" ) or die();

class Plugin {
	/**
	 * Dependency check.
	 *
	 * @param bool $allow_exit Allow exit.
	 *
	 * @return bool
	 */
	public static function checkDependencies( bool $allow_exit = false ): bool {
		if ( ! self::isPHPCompatible() ) {
			// translators: First %s will replace plugin name, Second %s replace minimum PHP version
			$message = sprintf( esc_html__( '%1$s requires minimum PHP version %2$s', 'email-editor-plus' ), EDPW_PLUGIN_NAME, EDPW_MINIMUM_PHP_VERSION );
			$allow_exit ? die( esc_html( $message ) ) : WC::adminNotice( esc_html( $message ), 'error' );

			return false;
		}

		if ( ! self::isWordPressCompatible() ) {
			// translators: First %s will replace plugin name, Second %s replace a minimum WordPress version
			$message = sprintf( esc_html__( '%1$s requires minimum WordPress version %2$s', 'email-editor-plus' ), EDPW_PLUGIN_NAME, EDPW_MINIMUM_WP_VERSION );
			$allow_exit ? exit( esc_html( $message ) ) : WC::adminNotice( esc_html( $message ), 'error' );

			return false;
		}

		if ( self::isActive( 'woocommerce/woocommerce.php' ) ) {
			if ( ! self::isWooCompatible() ) {
				// translators: 1. %s will replace plugin name, 2. %s replace WooCommerce version
				$message = sprintf( esc_html__( '%1$s requires minimum Woocommerce version %2$s', 'email-editor-plus' ), EDPW_PLUGIN_NAME, EDPW_MINIMUM_WC_VERSION );
				$allow_exit ? exit( esc_html( $message ) ) : WC::adminNotice( esc_html( $message ), 'error' );

				return false;
			}
		}

		return true;
	}

	/**
	 * Check the plugin are active or not.
	 *
	 * @param string $plugin_path Plugin path.
	 *
	 * @return bool
	 */
	public static function isActive( string $plugin_path ): bool {
		$active_plugins = get_option( 'active_plugins', array() );
		if ( is_multisite() ) {
			$active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) );
		}

		return in_array( $plugin_path, $active_plugins ) || array_key_exists( $plugin_path, $active_plugins );
	}

	/**
	 * Check the internal addon active or not.
	 *
	 * @param string $plugin_slug Plugin slug
	 *
	 * @return bool
	 */
	public static function isInternalAddonActive( string $plugin_slug ): bool {
		$active_addons = (array) get_option( 'edpw_active_addons', [] );
		if ( in_array( $plugin_slug, $active_addons ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Check WordPress required version.
	 * @return bool
	 */
	protected static function isWordPressCompatible(): bool {
		return (int) version_compare( get_bloginfo( 'version' ), EDPW_MINIMUM_WP_VERSION, '>=' ) > 0;
	}

	/**
	 * Check php version is compatible.
	 * @return bool
	 */
	protected static function isPHPCompatible(): bool {
		return (int) version_compare( PHP_VERSION, EDPW_MINIMUM_PHP_VERSION, '>=' ) > 0;
	}

	/**
	 * Check woocommerce is compatible.
	 * @return bool
	 */
	protected static function isWooCompatible(): bool {
		$woo_version = self::getWooVersion();

		return (int) version_compare( $woo_version, EDPW_MINIMUM_WC_VERSION, '>=' ) > 0;
	}

	/**
	 * Get Woocommerce version.
	 * @return string
	 */
	protected static function getWooVersion(): string {
		if ( defined( 'WC_VERSION' ) ) {
			return WC_VERSION;
		}
		if ( ! function_exists( 'get_plugins' ) ) {
			require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
		}
		if ( ! file_exists( WP_PLUGIN_DIR . '/woocommerce/woocommerce.php' ) ) {
			return '1.0.0';
		}
		$plugin_folder = get_plugins( '/woocommerce' );

		return $plugin_folder['woocommerce.php']['Version'] ?? '1.0.0';
	}

}
