<?php
/**
 *
 * @author     FutrX
 * @year       2023
 * @package    NoMoreBots
 * @subpackage NoMoreBots\Utils
 */

namespace NoMoreBots\Utils;

class Modules {

	/**
	 * Return a boolean if the module is active or not.
	 *
	 * @param string $moduleName
	 *
	 * @return bool
	 */
	public static function isActive( string $moduleName ): bool {
		return self::isAvailable( $moduleName ) && self::isEnabled( $moduleName );
	}


	/**
	 * Checks if the module is available or not, by checking its class.
	 *
	 * The only way to disable a module here, is to remove it from the global NOMOREBOTS_ACTIVE_MODULES constant.
	 *
	 * @param string $moduleName
	 *
	 * @return bool
	 * @see NOMOREBOTS_ACTIVE_MODULES
	 *
	 */
	public static function isAvailable( string $moduleName ): bool {
		switch ( $moduleName ) {
			case 'remote':
				return class_exists( 'NoMoreBots\Modules\Remote\Register' );
			case 'otp':
				return class_exists( 'NoMoreBots\Modules\OTP\Register' );
			case 'hp':
				return class_exists( 'NoMoreBots\Modules\HP\Register' );
			default:
				return false;
		}
	}

	/**
	 * This checks if the module is enabled from the control panel, which is managed by the admin.
	 *
	 * @param string $moduleName
	 *
	 * @return bool
	 */
	public static function isEnabled( string $moduleName ): bool {
		switch ( $moduleName ) {
			case 'remote':
				$activateValue = get_option( NOMOREBOTS_REMOTE_FIELD_PREFIX . '_activate' );
				return $activateValue && $activateValue === 'yes';
			case 'otp':
				$activateValue = get_option( NOMOREBOTS_OTP_FIELD_PREFIX . '_activate' );
				return $activateValue && $activateValue === 'yes';
			case 'hp':
				return true; // This one is enabled by default
			default:
				return false;
		}
	}
}
