<?php
/**
 * Verification Logic Abstract Class
 *
 * This abstract class defines common functionality for email and phone verification logic.
 * It should be extended by email and phone verification logic classes.
 *
 * @package miniorange-sms-order-notification-otp-verification/objects
 */

namespace WCSMSOTP\Objects;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use WCSMSOTP\Helper\MoUtility;
/**
 * Verification Logic Abstract Class
 *
 * This abstract class defines common functionality for email and phone verification logic.
 * It should be extended by email and phone verification logic classes.
 */
if ( ! class_exists( 'VerificationLogic' ) ) {
	/**
	 * Verification Logic Abstract Class
	 *
	 * This abstract class defines common functionality for email and phone verification logic.
	 * It should be extended by email and phone verification logic classes.
	 */
	abstract class VerificationLogic {

		/**
		 * Handle verification logic request.
		 *
		 * This function is called to handle verification request. Processes
		 * the request and starts the OTP Verification process. Starts with
		 * checking if the phone number or email is in the requested format.
		 *
		 * @param string $user_login    Username of the user.
		 * @param string $user_email    Email of the user.
		 * @param string $phone_number  Phone number of the user.
		 * @param string $otp_type      Email or SMS verification.
		 * @param bool   $from_both     Has user enabled from both.
		 * @return void
		 */
		abstract public function mowc_handle_logic( $user_login, $user_email, $phone_number, $otp_type, $from_both );

		/**
		 * Handle successful OTP sending.
		 *
		 * This function is called to handle what needs to be done when OTP sending is successful.
		 * Checks if the current form is an AJAX form and decides what message has to be
		 * shown to the user.
		 *
		 * @param string $user_login    Username of the user.
		 * @param string $user_email    Email of the user.
		 * @param string $phone_number  Phone number of the user.
		 * @param string $otp_type      Email or SMS verification.
		 * @param bool   $from_both     Has user enabled from both.
		 * @param array  $content       JSON decoded response from server.
		 * @return void
		 */
		abstract public function mowc_handle_otp_sent( $user_login, $user_email, $phone_number, $otp_type, $from_both, $content );

		/**
		 * Handle failed OTP sending.
		 *
		 * This function is called to handle what needs to be done when OTP sending fails.
		 * Checks if the current form is an AJAX form and decides what message has to be
		 * shown to the user.
		 *
		 * @param string $user_login    Username of the user.
		 * @param string $user_email    Email of the user.
		 * @param string $phone_number  Phone number of the user.
		 * @param string $otp_type      Email or SMS verification.
		 * @param bool   $from_both     Has user enabled from both.
		 * @param array  $content       JSON decoded response from server.
		 * @return void
		 */
		abstract public function mowc_handle_otp_sent_failed( $user_login, $user_email, $phone_number, $otp_type, $from_both, $content );

		/**
		 * Get the success message to be shown to the user when OTP was sent
		 * successfully. If admin has set his own unique message then
		 * show that to the user instead of the default one.
		 *
		 * @return string Success message.
		 */
		abstract public function mowc_get_otp_sent_message();

		/**
		 * Get the error message to be shown to the user when there was an
		 * error sending OTP. If admin has set his own unique message then
		 * show that to the user instead of the default one.
		 *
		 * @return string Error message.
		 */
		abstract public function mowc_get_otp_sent_failed_message();

		/**
		 * Get message for invalid format.
		 *
		 * Function decides what message needs to be sent to the user when the
		 * phone number or email does not match the required format. It checks if the admin
		 * has set any message in the plugin settings and returns that instead of the
		 * default one.
		 *
		 * @return string Invalid format message.
		 */
		abstract public function mowc_get_otp_invalid_format_message();

		/**
		 * Get message for blocked phone number or email.
		 *
		 * Function decides what message needs to be shown to the user when he enters a
		 * blocked phone number or email. It checks if the admin has set any message in the
		 * plugin settings and returns that instead of the default one.
		 *
		 * @return string Blocked message.
		 */
		abstract public function mowc_get_is_blocked_message();

		/**
		 * Handle matched format verification.
		 *
		 * This function starts the OTP Verification process if phone number or email matches the
		 * correct format and is not blocked by the admin.
		 *
		 * @param string $user_login    Username of the user.
		 * @param string $user_email    Email of the user.
		 * @param string $phone_number  Phone number of the user.
		 * @param string $otp_type      Email or SMS verification.
		 * @param bool   $from_both     Has user enabled from both.
		 * @return void
		 */
		abstract public function mowc_handle_matched( $user_login, $user_email, $phone_number, $otp_type, $from_both );

		/**
		 * Handle not matched format verification.
		 *
		 * This function handles what message needs to be shown to the user if phone number or email
		 * doesn't match the correct format. Check if admin has set any message, and check
		 * if the form is an ajax form to show the message in the correct format.
		 *
		 * @param string $phone_number  The phone number or email being processed.
		 * @param string $otp_type      Email or SMS verification.
		 * @param bool   $from_both     Has user enabled from both.
		 * @return void
		 */
		abstract public function mowc_handle_not_matched( $phone_number, $otp_type, $from_both );

		/**
		 * Start OTP verification process.
		 *
		 * This function starts the OTP Verification process and contacts server to send OTP to the
		 * user's phone number or email.
		 *
		 * @param string $user_login    Username of the user.
		 * @param string $user_email    Email of the user.
		 * @param string $phone_number  Phone number of the user.
		 * @param string $otp_type      Email or SMS verification.
		 * @param bool   $from_both     Has user enabled from both.
		 * @return void
		 */
		abstract public function mowc_start_otp_verification( $user_login, $user_email, $phone_number, $otp_type, $from_both );

		/**
		 * Check if phone number or email has been blocked.
		 *
		 * This function checks if the phone number or email has been blocked by the admin.
		 *
		 * @param string $user_email     Email of the user.
		 * @param string $phone_number   Phone number of the user.
		 * @return bool True if blocked, false otherwise.
		 */
		abstract public function mowc_is_blocked( $user_email, $phone_number );

		/**
		 * Detect if the current form is an AJAX form.
		 *
		 * Static function to detect if the current form being submitted for which
		 * OTP Verification has started is an AJAX form.
		 *
		 * @return bool True if AJAX form, false otherwise.
		 */
		public static function is_ajax_form() {
			$request_var = MoUtility::get_current_page_parameter_value( 'mo_external_popup_option', '' );
			if ( isset( $request_var ) &&
				in_array( sanitize_text_field( wp_unslash( $request_var ) ), array( 'miniorange-ajax-otp-generate', 'miniorange-ajax-otp-validate', 'mo_ajax_form_validate' ), true ) ) {
				return true;
			}
			$filter_result = apply_filters( 'mowc_is_ajax_form', false );
			return (bool) $filter_result;
		}
	}
}
