<?php
/**
 * HappyFenceNG Total Enterprise Security Plugin
 * HAPPYWPRESS is a RazorVue Trademark.
 *
 * @package     HappyFenceNG
 * @developer   Simonluca Scillitani
 * @copyright   2025 RazorVue di Scillitani Simonluca
 * @link        https://razorvue.it
 * @author      RazorVue di Scillitani Simonluca
 * @brand       HappyWPress
 * @uri         https://happywpress.com
 * @set all
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**PHPCS -> code compliance: we put useful comments where needed.. */
class HappyFenceNGVRDASH extends HappyFenceNGAVRoute {

	/**PHPCS -> code compliance: we put useful comments where needed.. */
	public function doRoute() {
		try {

			$hf_key_defined       = defined( 'HAPPYFENCENG_ENCRYPTION_KEY' );
			$hf_salt_defined      = defined( 'HAPPYFENCENG_ENCRYPTION_SALT' );
			$debug_active         = defined( 'WP_DEBUG' ) && WP_DEBUG;
			$debug_active_log     = defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG;
			$debug_active_display = defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY;

			$nonce_save_settings  = wp_create_nonce( 'happyfenceng_DASH_save_settings' );
			$burn                 = HappyFenceNGFailFast::setup();
			$wp_params_msg_line_1 = '';
			$wp_params_msg_line_2 = '';
			$wp_params_msg_line_3 = '';

			if ( HappyFenceNGPROC::not_equals( HAPPYFENCENG_DB->prefix, 'wp_' ) ) {
				$wp_params_msg_line_1 = esc_html__( 'Your custom database prefix is:', 'happyfenceng-total-enterprise-security' ) . esc_html( HAPPYFENCENG_DB->prefix );
				$wp_params_msg_line_2 = null;
				$wp_params_msg_line_3 = null;
			} else {
				$wp_params_msg_line_1 = esc_html__( 'Your WordPress prefix is the standard wp_', 'happyfenceng-total-enterprise-security' );
				$wp_params_msg_line_2 = esc_html__( 'You should consider migrating the prefix to something more complex to avoid attackers easily guessing the database.', 'happyfenceng-total-enterprise-security' );
				$wp_params_msg_line_3 = esc_html__( 'Act now to improve your site security.', 'happyfenceng-total-enterprise-security' );

			}

			$current_encryption = ( $hf_key_defined && $hf_salt_defined ) ?
				esc_html__( 'HappyFenceNG Db Encryption use HAPPYFENCENG_ENCRYPTION_KEY/SALT.', 'happyfenceng-total-enterprise-security' ) :
				esc_html__( 'HappyFenceNG Db Encryption use WP encryption key/salt, see Data Security page.', 'happyfenceng-total-enterprise-security' );

			$params = array(
				'texts'  => array(
					'title_params'                 => esc_html__( 'Setup Params', 'happyfenceng-total-enterprise-security' ),
					'wp_params_msg_line_1'         => $wp_params_msg_line_1,
					'wp_params_msg_line_2'         => $wp_params_msg_line_2 ?? '',
					'wp_params_msg_line_3'         => $wp_params_msg_line_3 ?? '',
					'current_encryption'           => $current_encryption,
					'debug_active_warning'         => esc_html__( 'Debug enabled, if this is a Production environment you should disable it immediately.', 'happyfenceng-total-enterprise-security' ),
					'debug_active_log_warning'     => esc_html__( 'Debug LOG enabled, if this is a Production environment you should disable it immediately.', 'happyfenceng-total-enterprise-security' ),
					'debug_active_display_warning' => esc_html__( 'Debug DISPLAY enabled, if this is a Production environment you should disable it immediately.', 'happyfenceng-total-enterprise-security' ),

					'title_dash'                   => esc_html__( 'Services', 'happyfenceng-total-enterprise-security' ),
					'dashboard_desc_line_1'        => esc_html__( 'This view shows which modules are currently operational.', 'happyfenceng-total-enterprise-security' ),
					'dashboard_desc_line_2'        => esc_html__( 'You should check it to ensure that everything is working fine.', 'happyfenceng-total-enterprise-security' ),
					'dashboard_desc_line_3'        => esc_html__( 'It is also possible to access the dashboard via WP-CLI, for more information check the WP-CLI page.', 'happyfenceng-total-enterprise-security' ),
					'dashboard_desc_line_4'        => esc_html__( 'Module availability depends on your subscription level.', 'happyfenceng-total-enterprise-security' ),

					'module_title'                 => esc_html__( 'Module', 'happyfenceng-total-enterprise-security' ),
					'status_title'                 => esc_html__( 'Status', 'happyfenceng-total-enterprise-security' ),
					'show_only_running'            => esc_html__( 'Show only running', 'happyfenceng-total-enterprise-security' ),
					'feature_requires'             => esc_html__( 'FREE FEATURE', 'happyfenceng-total-enterprise-security' ),
					'no_functions'                 => esc_html__( 'WARNING!! No functions registered, please check log file!', 'happyfenceng-total-enterprise-security' ),
					'info'                         => esc_html__( 'Info', 'happyfenceng-total-enterprise-security' ),
					'warning'                      => esc_html__( 'Warning', 'happyfenceng-total-enterprise-security' ),
					'offline'                      => esc_html__( 'Offline', 'happyfenceng-total-enterprise-security' ),
					'running'                      => esc_html__( 'Running', 'happyfenceng-total-enterprise-security' ),

				),
				'links'  => array(
					'happyfenceng_logo_url' => esc_url( HAPPYFENCENG_PLUGIN_URL . 'assets/images/happyfenceng-small.png' ),

				),
				'values' => array(
					'show_warning'         => ( HappyFenceNGPROC::not_equals( $wp_params_msg_line_2, null ) || HappyFenceNGPROC::not_equals( $wp_params_msg_line_3, null ) ),
					'modules'              => HappyFenceNGCStore::getFunctions(),
					'show_only_running'    => false,
					'checkbox_on'          => HappyFenceNGPlaceholder::CHECKBOX_ON,
					'checkbox_off'         => HappyFenceNGPlaceholder::CHECKBOX_OFF,
					'nonce'                => $nonce_save_settings,
					'burnNonce'            => $burn,
					'feature_enabled'      => true,
					'debug_active'         => $debug_active,
					'debug_active_log'     => $debug_active_log,
					'debug_active_display' => $debug_active_display,

				),
			);

			ob_start();
			HappyFenceNGVControl::renderView( $params, 'dash' );
			// phpcs:ignore WordPress.Security.EscapeOutput
			echo ob_get_clean();

		} catch ( Throwable $e ) {
			HappyFenceNGLOGCORE::logcore(
				'HappyFenceNGVRDASH - cannot render UI ' . $e->getMessage(),
				HappyFenceNGSyslogFacility::UI,
				HappyFenceNGSyslogSeverity::ERROR
			);
			ob_end_clean();
			HappyFenceNGCleaner::cleaner_oops();
		}
	}
}
