<?php
/**
 * This class handles the OTP tab.
 *
 * @author     FutrX
 * @year       2023
 * @package    futrx.com
 * @subpackage NoMoreBots\Admin\Pages\Settings
 */

namespace NoMoreBots\Admin\Pages\Settings;

use Carbon_Fields\Field;
use NoMoreBots\Utils\Plugins as PluginsUtils;
use NoMoreBots\Utils\OTP\OTP as OTPUtils;

class OTP {

	/**
	 * Returns the Object for the activation field
	 *
	 * @return Field\Field
	 */
	public static function getActivateField(): Field\Field {
		return Field::make( 'select', NOMOREBOTS_OTP_FIELD_PREFIX . '_activate', 'Activate OTP' )
					->add_options(
						array(
							'yes' => 'Yes',
							'no'  => 'No',
						)
					)
					->set_default_value( 'no' );
	}

	/**
	 * Returns the Object for the Secret Key Field.
	 *
	 * @return Field\Field
	 */
	public static function getSecretKeyField(): Field\Field {
		return Field::make(
			'text',
			NOMOREBOTS_OTP_FIELD_PREFIX . NOMOREBOTS_OTP_SECRET_KEY_META_KEY,
			__( 'OTP Secret', NOMOREBOTS_PLUGINTEXTDOMAIN )
		)
					->set_default_value( OTPUtils::generateOTPSecret( 32 ) )
					->set_attribute( 'min', '32' )
					->set_attribute( 'max', '32' )
					->set_classes( 'nmb-otp-secret-key' )
					->set_help_text(
						__(
							'The OTP secret code that will be used to generate the OTP tokens. This is a 32 characters code.',
							NOMOREBOTS_PLUGINTEXTDOMAIN
						)
					)
					->set_conditional_logic(
						array(
							'relation' => 'AND',
							array(
								'field'    => NOMOREBOTS_OTP_FIELD_PREFIX . '_activate',
								'value'    => 'yes',
								'operator' => '==',
							),
						)
					);
	}

	/**
	 * Returns the Object for the WordPress forms field.
	 *
	 * @return Field\Field
	 */
	public static function getWordpressFormsField(): Field\Field {
		return Field::make(
			'multiselect',
			NOMOREBOTS_OTP_FIELD_PREFIX . NOMOREBOTS_WP_FORMS_META_KEY,
			__( 'Select WordPress Forms', NOMOREBOTS_PLUGINTEXTDOMAIN )
		)
					->set_help_text(
						__(
							'Select the WordPress forms you want to add the "Honeypot" field to.',
							NOMOREBOTS_PLUGINTEXTDOMAIN
						)
					)
					->add_options(
						array(
							'registerform'     => 'Register',
							'loginform'        => 'Login',
							'lostpasswordform' => 'Lost Password',
						)
					);
	}
	/**
	 * Returns the Object for the WooCommerce Pages.
	 *
	 * @return mixed
	 */
	public static function getWooCommerceFormsField() {
		if ( PluginsUtils::isWooCommerceActive() ) {
			// return a multi select field for pages.
			return Field::make(
				'multiselect',
				NOMOREBOTS_OTP_FIELD_PREFIX . NOMOREBOTS_WOOCOMMERCE_FORMS_META_KEY,
				__( 'Select WooCommerce Forms', NOMOREBOTS_PLUGINTEXTDOMAIN )
			)
						->set_help_text(
							__(
								'Select the WooCommerce forms you want to add the "OTP" functionality to.',
								NOMOREBOTS_PLUGINTEXTDOMAIN
							)
						)
						->add_options(
							array(
								'woocommerce-form-checkout' => 'Checkout',
								'woocommerce-form-login' => 'Login',
								'woocommerce-form-register' => 'Register',
							)
						);
		}

		return false;
	}

	/**
	 * Returns the Object for the selected pages field.
	 *
	 * @return Field\Field
	 */
	public static function getListOfPages(): Field\Field {
		return Field::make(
			'association',
			NOMOREBOTS_OTP_FIELD_PREFIX . NOMOREBOTS_PAGES_TO_FILTER_META_KEY,
			__( 'Select Pages', NOMOREBOTS_PLUGINTEXTDOMAIN )
		)
					->set_help_text(
						__(
							'Select the pages you want to add the "OTP" field to.',
							NOMOREBOTS_PLUGINTEXTDOMAIN
						)
					)
					->set_types(
						array(
							array(
								'type'      => 'post',
								'post_type' => 'page',
							),
						)
					)
					->set_conditional_logic(
						array(
							'relation' => 'AND',
							array(
								'field'    => NOMOREBOTS_OTP_FIELD_PREFIX . '_activate',
								'value'    => 'yes',
								'operator' => '==',
							),
						)
					);
	}

	/**
	 * Save the OTP fields directly to wp_options for faster and more performant access.
	 *
	 * @return void
	 */
	public static function saveOTPFields() {
		// 1. Activate Option.
		$otpActivate = carbon_get_theme_option( NOMOREBOTS_OTP_FIELD_PREFIX . '_activate' );
		update_option( NOMOREBOTS_OTP_FIELD_PREFIX . '_activate', $otpActivate );

		// 2. Secret.
		$otpSecret = carbon_get_theme_option( NOMOREBOTS_OTP_FIELD_PREFIX . NOMOREBOTS_OTP_SECRET_KEY_META_KEY );
		update_option( NOMOREBOTS_OTP_FIELD_PREFIX . NOMOREBOTS_OTP_SECRET_KEY_META_KEY, $otpSecret );

		// 3. WordPress Forms.
		$otpWordpressForms = carbon_get_theme_option( NOMOREBOTS_OTP_FIELD_PREFIX . NOMOREBOTS_WP_FORMS_META_KEY );
		if ( ! empty( $otpWordpressForms ) && is_array( $otpWordpressForms ) ) {
			update_option( NOMOREBOTS_OTP_FIELD_PREFIX . NOMOREBOTS_WP_FORMS_META_KEY, $otpWordpressForms );
		}

		// 4. WooCommerce Forms.
		$otpWooCommerceForms = carbon_get_theme_option( NOMOREBOTS_OTP_FIELD_PREFIX . NOMOREBOTS_WOOCOMMERCE_FORMS_META_KEY );
		if ( ! empty( $otpWooCommerceForms ) && is_array( $otpWooCommerceForms ) ) {
			update_option( NOMOREBOTS_OTP_FIELD_PREFIX . NOMOREBOTS_WOOCOMMERCE_FORMS_META_KEY, $otpWooCommerceForms );
		}

		// 4. List of Pages
		$otpPages = carbon_get_theme_option( NOMOREBOTS_OTP_FIELD_PREFIX . NOMOREBOTS_PAGES_TO_FILTER_META_KEY );
		if ( ! empty( $otpPages ) && is_array( $otpPages ) ) {
			$pages_ids = array_map(
				function ( $page ) {
					return $page['id'];
				},
				$otpPages
			);
			update_option( NOMOREBOTS_OTP_FIELD_PREFIX . NOMOREBOTS_PAGES_TO_FILTER_META_KEY, $pages_ids );
		}
	}
}
