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

namespace NoMoreBots\Admin\Pages\Settings;

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


class HoneyPot {

	/**
	 * Returns the Object for the Blocked Duration Field.
	 *
	 * @return Field\Field
	 */
	public static function getBlockedDurationField(): Field\Field {
		return Field::make(
			'text',
			NOMOREBOTS_HONEYPOT_FIELD_PREFIX . NOMOREBOTS_BLOCKED_DURATION_META_KEY,
			__( 'Blocked Duration', NOMOREBOTS_PLUGINTEXTDOMAIN )
		)
					->set_default_value( '30' )
					->set_attribute( 'type', 'number' )
					->set_attribute( 'min', '1' )
					->set_attribute( 'max', '999' )
					->set_attribute( 'step', '1' )
					->set_help_text(
						__(
							'The duration in minutes, for which the user will be blocked after a failed login attempt. Default: 30 minutes. 999 means forever!',
							NOMOREBOTS_PLUGINTEXTDOMAIN
						)
					);
	}

	/**
	 * 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_HONEYPOT_FIELD_PREFIX . NOMOREBOTS_WOOCOMMERCE_FORMS_META_KEY,
				__( 'Select WooCommerce Forms', NOMOREBOTS_PLUGINTEXTDOMAIN )
			)
						->set_help_text(
							__(
								'Select the WooCommerce forms you want to add the "Honeypot" field 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 WordPress forms.
	 */
	public static function getWordpressFormsField() {
		return Field::make(
			'multiselect',
			NOMOREBOTS_HONEYPOT_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 Objects for the Manual Form Generator.
	 *
	 * @return Field\Field
	 */
	public static function getListOfPages(): Field\Field {
		return Field::make(
			'association',
			NOMOREBOTS_HONEYPOT_FIELD_PREFIX . NOMOREBOTS_PAGES_TO_FILTER_META_KEY,
			__( 'Select Pages', NOMOREBOTS_PLUGINTEXTDOMAIN )
		)
					->set_help_text(
						__(
							'Select the pages you want to add the "Honeypot" field to.',
							NOMOREBOTS_PLUGINTEXTDOMAIN
						)
					)
					->set_types(
						array(
							array(
								'type'      => 'post',
								'post_type' => 'page',
							),
						)
					);
	}

	/**
	 * Save the Honepot fields directly to wp_options for faster and more performant access.
	 *
	 * @return void
	 */
	public static function saveHonePotFields() {
		// 1. Blocked Duration
		$hpBlockedDuration = carbon_get_theme_option( NOMOREBOTS_HONEYPOT_FIELD_PREFIX . NOMOREBOTS_BLOCKED_DURATION_META_KEY );
		update_option( NOMOREBOTS_HONEYPOT_FIELD_PREFIX . NOMOREBOTS_BLOCKED_DURATION_META_KEY, $hpBlockedDuration );

		// 2. WordPress Forms
		$wpForms = carbon_get_theme_option( NOMOREBOTS_HONEYPOT_FIELD_PREFIX . NOMOREBOTS_WP_FORMS_META_KEY );
		if ( ! empty( $wpForms ) && is_array( $wpForms ) ) {
			update_option( NOMOREBOTS_HONEYPOT_FIELD_PREFIX . NOMOREBOTS_WP_FORMS_META_KEY, $wpForms );
		}

		// 3. Woo Forms
		$wooForms = carbon_get_theme_option( NOMOREBOTS_HONEYPOT_FIELD_PREFIX . NOMOREBOTS_WOOCOMMERCE_FORMS_META_KEY );
		if ( ! empty( $wooForms ) && is_array( $wooForms ) ) {
			update_option( NOMOREBOTS_HONEYPOT_FIELD_PREFIX . NOMOREBOTS_WOOCOMMERCE_FORMS_META_KEY, $wooForms );
		}

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

		}
	}
}
