<?php 

// Exit if accessed directly
if( !defined('ABSPATH') ) exit;

class ESLP_Easy_Social_Login_Core {

	/**
	 * Class properties and constructor for Easy Social Login plugin.
	 *
	 * The class defines static properties for managing modules and 
	 * an instance of the Extra functionality class. The constructor 
	 * sets up actions, shortcodes, and initializes required modules.
	 */

	// Static properties
	private static $eslp_modules_dir = null;
	private static $eslp_module_instance = [];
	private static $eslp_easy_social_login_extra = null;

	/**
	 * Class constructor.
	 *
	 * The constructor initializes the plugin by setting up necessary actions, 
	 * shortcodes, admin menu entries, and including essential files.
	 */
	public function __construct() {

		// Include the Extra functionality class
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/eslp-easy-social-login-extra.php';
		self::$eslp_easy_social_login_extra = new ESLP_Easy_Social_Login_Extra();

		// Modules folder
		self::$eslp_modules_dir = plugin_dir_path( dirname( __FILE__ ) ) . 'modules/';

		// Register plugin settings
		add_action( 'admin_init', array( $this, 'eslp_easy_social_login_register_settings' ) );

		// Initialize hooks and modules
		add_action( 'init', array( $this, 'eslp_easy_social_login_init_hook' ) );
		add_action( 'init', array( $this, 'eslp_easy_social_login_init_modules' ) );

		// Register shortcode for social login
		add_shortcode( 'eslp_easy_social_login', array( $this, 'eslp_easy_social_login_shortcode' ) );

		// Add admin menu
		add_action( 'admin_menu', array( $this, 'eslp_add_admin_menu' ) );

		// Display admin notices
		add_action( 'admin_notices', array( $this, 'eslp_admin_notices' ) );

		// Work-in-progress: Render 2FA page
		add_action( 'template_redirect', array( $this, 'eslp_render_2fa_page' ) );

		// Include donation functionality
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/eslp-easy-social-login-donate.php';
	}

	/**
	 * Class methods to initialize and load assets for Easy Social Login plugin.
	 *
	 * These methods handle the registration of scripts and styles for both 
	 * the admin panel and the frontend, as well as WooCommerce integration.
	 */

	/**
	 * Main run method for the plugin.
	 *
	 * This method initiates the loading of necessary assets for the plugin.
	 */
	public function eslp_run() {
		self::eslp_load_assets();
	}

	/**
	 * Load assets required for the plugin.
	 *
	 * This method registers actions to enqueue scripts and styles
	 * for the admin panel, login page, and WooCommerce integration if enabled.
	 */
	private function eslp_load_assets() {
		// Enqueue admin scripts
		add_action( 'admin_enqueue_scripts', array( $this, 'eslp_enqueue_scripts' ) );

		// Enqueue frontend scripts
		add_action( 'login_enqueue_scripts', array( $this, 'eslp_enqueue_scripts_front' ) );

		// Enqueue scripts for WooCommerce if the feature is enabled
		if ( 'not-exists' !== get_option( 'eslp_easy_social_login_woocommerce_checkout', 'not-exists' ) 
			&& get_option( 'eslp_easy_social_login_woocommerce_checkout' ) == true ) {
			add_action( 'woocommerce_before_customer_login_form', array( $this, 'eslp_enqueue_scripts_front' ) );
			add_action( 'woocommerce_checkout_init', array( $this, 'eslp_enqueue_scripts_front' ) );
		}
	}

	/**
	 * Enqueue admin scripts and styles.
	 *
	 * This method registers styles and scripts used in the admin panel,
	 * including support for localized AJAX calls and additional libraries.
	 */
	public function eslp_enqueue_scripts() {
		wp_enqueue_style('dashicons');
		wp_enqueue_style( 'eslp-easy-social-login-admin-css', plugin_dir_url(__DIR__) . 'assets/css/eslp-easy-social-login-admin.css', false, ESLP_VERSION );
		wp_enqueue_script( 'eslp-easy-social-login-admin-js', plugin_dir_url(__DIR__) . 'assets/js/eslp-easy-social-login-admin-min.js', array( 'jquery' ), ESLP_VERSION, true );

		wp_localize_script(
			'eslp-easy-social-login-admin-js',
			'donateMessageAjax',
			array(
				'ajaxUrl' => admin_url('admin-ajax.php'),
			)
		);

		wp_enqueue_style( 'lightbox2-css', plugin_dir_url(__DIR__) . 'assets/css/lightbox.min.css', false, '2.11.5' );
		wp_enqueue_script( 'lightbox2-js', plugin_dir_url(__DIR__) . 'assets/js/lightbox.min.js', array(), '2.11.5', true );
	}

	/**
	 * Enqueue frontend scripts and styles.
	 *
	 * This method registers styles and scripts used in the frontend,
	 * including the login page and WooCommerce pages if applicable.
	 */
	public function eslp_enqueue_scripts_front() {
		wp_enqueue_style('dashicons');
		wp_enqueue_style( 'eslp-easy-social-login-front-css', plugins_url( '../assets/css/eslp-easy-social-login-front.css', __FILE__ ), false, ESLP_VERSION );
	}

	/**
	 * Register plugin settings for Easy Social Login.
	 *
	 * This method initializes the settings for the plugin, including validation
	 * callbacks, default values, and optional fields to be added to the database.
	 */
	public function eslp_easy_social_login_register_settings() {
		// Register settings with validation callbacks and default values.
		register_setting('eslp_easy_social_login_settings_group', 'eslp_easy_social_login_settings', array($this, 'eslp_easy_social_login_validate_data'));

		register_setting('eslp_easy_social_login_settings_group', 'eslp_easy_social_login_user_role', array(
			'type' => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default' => 'subscriber',
		));

		register_setting('eslp_easy_social_login_settings_group', 'eslp_easy_social_login_notify_admin', array(
			'type' => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default' => 'no',
		));

		register_setting('eslp_easy_social_login_settings_group', 'eslp_easy_social_login_redirect_url', array(
			'type' => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default' => null,
		));

		register_setting('eslp_easy_social_login_settings_group', 'eslp_easy_social_login_display_header_message', array(
			'type' => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default' => 'yes',
		));

		register_setting('eslp_easy_social_login_settings_group', 'eslp_easy_social_login_display_button_login', array(
			'type' => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default' => 'yes',
		));

		register_setting('eslp_easy_social_login_settings_group', 'eslp_easy_social_login_display_button_register', array(
			'type' => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default' => 'yes',
		));

		register_setting('eslp_easy_social_login_settings_group', 'eslp_easy_social_login_button_position', array(
			'type' => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default' => 'after',
		));

		register_setting('eslp_easy_social_login_settings_group', 'eslp_easy_social_login_woocommerce_login', array(
			'type' => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default' => 'yes',
		));

		register_setting('eslp_easy_social_login_settings_group', 'eslp_easy_social_login_woocommerce_register', array(
			'type' => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default' => 'yes',
		));

		register_setting('eslp_easy_social_login_settings_group', 'eslp_easy_social_login_woocommerce_checkout', array(
			'type' => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default' => 'yes',
		));

		register_setting('eslp_easy_social_login_settings_group', 'eslp_hide_donate_time', array(
			'type' => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default' => null,
		));

		register_setting('eslp_easy_social_login_settings_group', 'eslp_last_hide', array(
			'type' => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default' => null,
		));

		register_setting('eslp_easy_social_login_settings_group', 'eslp_database_version', array(
			'type' => 'string',
			'sanitize_callback' => 'sanitize_text_field',
			'default' => ESLP_DATABASE_VERSION,
		));

		// Add options with default values if they do not already exist.
		if ('not-exists' === get_option('eslp_easy_social_login_user_role', 'not-exists')) {
			add_option('eslp_easy_social_login_user_role', 'subscriber');
		}

		if ('not-exists' === get_option('eslp_easy_social_login_notify_admin', 'not-exists')) {
			add_option('eslp_easy_social_login_notify_admin', 'yes');
		}

		if ('not-exists' === get_option('eslp_easy_social_login_redirect_url', 'not-exists')) {
			add_option('eslp_easy_social_login_redirect_url', null);
		}

		if ('not-exists' === get_option('eslp_easy_social_login_display_header_message', 'not-exists')) {
			add_option('eslp_easy_social_login_display_header_message', 'yes');
		}

		if ('not-exists' === get_option('eslp_easy_social_login_display_button_login', 'not-exists')) {
			add_option('eslp_easy_social_login_display_button_login', 'yes');
		}

		if ('not-exists' === get_option('eslp_easy_social_login_display_button_register', 'not-exists')) {
			add_option('eslp_easy_social_login_display_button_register', 'yes');
		}

		if ('not-exists' === get_option('eslp_easy_social_login_button_position', 'not-exists')) {
			add_option('eslp_easy_social_login_button_position', 'after');
		}

		if ('not-exists' === get_option('eslp_easy_social_login_woocommerce_login', 'not-exists')) {
			add_option('eslp_easy_social_login_woocommerce_login', 'yes');
		}

		if ('not-exists' === get_option('eslp_easy_social_login_woocommerce_register', 'not-exists')) {
			add_option('eslp_easy_social_login_woocommerce_register', 'yes');
		}

		if ('not-exists' === get_option('eslp_easy_social_login_woocommerce_checkout', 'not-exists')) {
			add_option('eslp_easy_social_login_woocommerce_checkout', 'yes');
		}

		if ('not-exists' === get_option('eslp_hide_donate_time', 'not-exists')) {
			add_option('eslp_hide_donate_time', null);
		}

		if ('not-exists' === get_option('eslp_last_hide', 'not-exists')) {
			add_option('eslp_last_hide', null);
		}
		
		if ('not-exists' === get_option('eslp_database_version', 'not-exists')) {
			add_option('eslp_database_version', ESLP_DATABASE_VERSION);
		}
	}

	/**
	 * Validate and sanitize settings data for Easy Social Login.
	 *
	 * This method validates and sanitizes user input for the Easy Social Login settings.
	 * It ensures data integrity and prevents unauthorized changes using nonce verification.
	 *
	 * @param array $input The input settings data to validate.
	 * @return array The validated and sanitized settings data.
	 */
	public function eslp_easy_social_login_validate_data( $input ) {
		$validated_input = [];

		// Verify nonce to prevent CSRF.
		if ( ! isset( $_POST['eslp_easy_social_login_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['eslp_easy_social_login_nonce'] ) ), 'social_login_action' ) ) {
			add_settings_error( 'eslp_easy_social_login_settings', 'invalid_nonce', esc_html( __( 'Nonce verification failed. Settings not saved.', 'easy-social-login' ) ) );
			return [];
		}

		// Sanitize and validate each input setting.
		if ( isset( $input['eslp_easy_social_login_user_role'] ) ) {
			$validated_input['eslp_easy_social_login_user_role'] = sanitize_text_field( $input['eslp_easy_social_login_user_role'] );
		}

		if ( isset( $input['eslp_easy_social_login_notify_admin'] ) ) {
			$validated_input['eslp_easy_social_login_notify_admin'] = $input['eslp_easy_social_login_notify_admin'] === 'yes' ? 'yes' : 'no';
		}

		if ( isset( $input['eslp_easy_social_login_redirect_url'] ) ) {
			$validated_input['eslp_easy_social_login_redirect_url'] = sanitize_url( $input['eslp_easy_social_login_redirect_url'] );
		}

		if ( isset( $input['eslp_easy_social_login_display_header_message'] ) ) {
			$validated_input['eslp_easy_social_login_display_header_message'] = $input['eslp_easy_social_login_display_header_message'] === 'yes' ? 'yes' : 'no';
		}

		if ( isset( $input['eslp_easy_social_login_display_button_login'] ) ) {
			$validated_input['eslp_easy_social_login_display_button_login'] = $input['eslp_easy_social_login_display_button_login'] === 'yes' ? 'yes' : 'no';
		}

		if ( isset( $input['eslp_easy_social_login_display_button_register'] ) ) {
			$validated_input['eslp_easy_social_login_display_button_register'] = $input['eslp_easy_social_login_display_button_register'] === 'yes' ? 'yes' : 'no';
		}

		if ( isset( $input['eslp_easy_social_login_button_position'] ) ) {
			$allowed_positions = [ 'before', 'after' ];
			$validated_input['eslp_easy_social_login_button_position'] = in_array( $input['eslp_easy_social_login_button_position'], $allowed_positions, true )
				? $input['eslp_easy_social_login_button_position']
				: 'after';
		}

		if ( isset( $input['eslp_easy_social_login_woocommerce_login'] ) ) {
			$validated_input['eslp_easy_social_login_woocommerce_login'] = $input['eslp_easy_social_login_woocommerce_login'] === 'yes' ? 'yes' : 'no';
		}

		if ( isset( $input['eslp_easy_social_login_woocommerce_register'] ) ) {
			$validated_input['eslp_easy_social_login_woocommerce_register'] = $input['eslp_easy_social_login_woocommerce_register'] === 'yes' ? 'yes' : 'no';
		}

		if ( isset( $input['eslp_easy_social_login_woocommerce_checkout'] ) ) {
			$validated_input['eslp_easy_social_login_woocommerce_checkout'] = $input['eslp_easy_social_login_woocommerce_checkout'] === 'yes' ? 'yes' : 'no';
		}

		return $validated_input;
	}


	/**
	 * Deletes all Easy Social Login settings and performs module cleanup.
	 *
	 * This method removes all saved options related to Easy Social Login from the database.
	 * It also triggers the uninstall process for all loaded modules.
	 */
	public function eslp_delete_settings() {
		// Delete all Easy Social Login options from the database.
		delete_option( 'eslp_easy_social_login_user_role' );
		delete_option( 'eslp_easy_social_login_notify_admin' );
		delete_option( 'eslp_easy_social_login_redirect_url' );
		delete_option( 'eslp_easy_social_login_display_header_message' );
		delete_option( 'eslp_easy_social_login_display_button_login' );
		delete_option( 'eslp_easy_social_login_display_button_register' );
		delete_option( 'eslp_easy_social_login_button_position' );
		delete_option( 'eslp_easy_social_login_woocommerce_login' );
		delete_option( 'eslp_easy_social_login_woocommerce_register' );
		delete_option( 'eslp_easy_social_login_woocommerce_checkout' );
		delete_option( 'eslp_database_version' );

		delete_option( 'eslp_hide_donate_time' );
		delete_option( 'eslp_last_hide' );

		// Load modules and trigger their uninstall methods.
		self::eslp_load_modules();
		foreach ( self::$eslp_module_instance as $eslp_module ) {
			$eslp_module->eslp_uninstall();
		}
	}

	/**
	 * Initializes Easy Social Login hooks based on stored settings.
	 *
	 * This method dynamically adds actions for displaying header messages or social login buttons
	 * in various locations, including login, registration, and WooCommerce forms.
	 */
	public function eslp_easy_social_login_init_hook() {
		if ( 'not-exists' !== get_option( 'eslp_easy_social_login_display_header_message', 'not-exists' )
			&& get_option( 'eslp_easy_social_login_display_header_message' ) == true ) {
			
			// Add action for login form if enabled.
			if ( 'not-exists' !== get_option( 'eslp_easy_social_login_display_button_login', 'not-exists' )
				&& get_option( 'eslp_easy_social_login_display_button_login' ) == true ) {
				add_action( 'login_form', [ $this, 'eslp_header_message' ] );
			}

			// Add action for registration form if enabled.
			if ( 'not-exists' !== get_option( 'eslp_easy_social_login_display_button_register', 'not-exists' )
				&& get_option( 'eslp_easy_social_login_display_button_register' ) == true ) {
				add_action( 'register_form', [ $this, 'eslp_header_message' ] );
			}
			
			// Check WooCommerce status and add appropriate actions.
			$woo_status = self::$eslp_easy_social_login_extra->eslp_check_woocommerce_status();
			if ( $woo_status[0] == 'active' ) {
				if ( get_option( 'woocommerce_enable_myaccount_registration' ) === 'yes' ) {
					
					// Add action for WooCommerce login form if enabled.
					if ( 'not-exists' !== get_option( 'eslp_easy_social_login_woocommerce_login', 'not-exists' )
						&& get_option( 'eslp_easy_social_login_woocommerce_login' ) == true ) {
						if ( 'not-exists' !== get_option( 'eslp_easy_social_login_button_position', 'not-exists' ) ) {
							$prefix = ( get_option( 'eslp_easy_social_login_button_position' ) == 'after' ) ? '_form_start' : '_form';
							add_action( 'woocommerce_login' . $prefix, [ $this, 'eslp_header_message' ] );
						}
					}
					
					// Add action for WooCommerce registration form if enabled.
					if ( 'not-exists' !== get_option( 'eslp_easy_social_login_woocommerce_register', 'not-exists' )
						&& get_option( 'eslp_easy_social_login_woocommerce_register' ) == true ) {
						if ( 'not-exists' !== get_option( 'eslp_easy_social_login_button_position', 'not-exists' ) ) {
							$prefix = ( get_option( 'eslp_easy_social_login_button_position' ) == 'after' ) ? '_form_start' : '_form';
							add_action( 'woocommerce_register' . $prefix, [ $this, 'eslp_header_message' ] );
						}
					}
				}
				
				// Add action for WooCommerce checkout page if enabled and user is not logged in.
				if ( get_option( 'woocommerce_enable_signup_and_login_from_checkout' ) === 'yes' ) {
					if ( 'not-exists' !== get_option( 'eslp_easy_social_login_woocommerce_checkout', 'not-exists' )
						&& get_option( 'eslp_easy_social_login_woocommerce_checkout' ) == true ) {
						if ( ! is_user_logged_in() ) {
							add_action( 'woocommerce_checkout_init', [ $this, 'eslp_header_message' ] );
						}
					}
				}
			}
		}
	}

	/**
	 * Outputs the header message for Easy Social Login.
	 *
	 * This method displays a styled header message encouraging users to log in or register
	 * using their preferred social network for a faster and more convenient experience.
	 */
	public function eslp_header_message() {
		?>
		<div class="eslp-easy-social-login-header-message">
			<h3><?php echo esc_html( __( 'Connect with your social network', 'easy-social-login' ) ); ?></h3>
			<p><?php echo esc_html( __( 'For a faster and more convenient experience, log in with your preferred social network below.', 'easy-social-login' ) ); ?></p>
		</div>
		<?php
	}

	/**
	 * Initializes the Easy Social Login modules.
	 *
	 * This method ensures that all modules required for the plugin are loaded and initialized.
	 */
	public function eslp_easy_social_login_init_modules() {
		self::eslp_load_modules();
	}

	/**
	 * Loads the Easy Social Login modules from the designated modules directory.
	 *
	 * This method checks if the modules directory exists and iterates over all PHP files within it.
	 * Each valid module file is included, and if it defines a class with the same name as the file, 
	 * an instance of that class is created and stored in the module instance array.
	 */
	private function eslp_load_modules() {
		if ( ! is_dir( self::$eslp_modules_dir ) ) {
			return;
		}

		foreach ( glob( self::$eslp_modules_dir . '*.php' ) as $file ) {
			include_once $file;

			$class_name = basename( $file, '.php' );
			
			if ( class_exists( $class_name ) ) {
				self::$eslp_module_instance[] = new $class_name();
			}
		}
	}

	/**
	 * Generates the Easy Social Login shortcode output.
	 *
	 * This shortcode displays the social login options for users who are not logged in.
	 * It includes the header message, enqueues necessary scripts, and renders the social login icons.
	 *
	 * @return string The HTML output for the shortcode.
	 */
	public function eslp_easy_social_login_shortcode() {
		ob_start();
		if ( ! is_user_logged_in() ) {
			if ( 'not-exists' !== get_option( 'eslp_easy_social_login_display_header_message', 'not-exists' )
				&& get_option( 'eslp_easy_social_login_display_header_message' ) == true ) {
				self::eslp_header_message();
			}

			add_action( 'wp_enqueue_scripts', array( $this, 'eslp_enqueue_scripts_front' ) );

			foreach ( self::$eslp_module_instance as $eslp_module ) {
				$eslp_module->eslp_module_enqueue_scripts();
				$eslp_module->display_social_icon_form( 'login_form' );
			}
		}

		return ob_get_clean();
	}

	/**
	 * Adds the Easy Social Login menu to the WordPress admin dashboard.
	 *
	 * This function creates a new menu item for managing the social login settings.
	 */
	public function eslp_add_admin_menu() {
		add_menu_page(
			esc_html( __( 'Social Login Settings', 'easy-social-login' ) ),
			esc_html( __( 'Social Login', 'easy-social-login' ) ),
			'manage_options',
			'easy-social-login',
			array( $this, 'eslp_settings_page' ),
			'dashicons-networking',
			2
		);
	}

	/**
	 * Displays admin notices based on the status of settings updates.
	 *
	 * This function shows a success or error message in the WordPress admin interface
	 * depending on whether the settings were saved successfully or not.
	 */
	public function eslp_admin_notices() {
		if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == 'true' ) {
			?>
			<div class="notice notice-success is-dismissible">
				<p><?php echo esc_html( __( 'Settings saved successfully.', 'easy-social-login' ) ); ?></p>
			</div>
			<?php
		} elseif ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == 'false' ) {
			?>
			<div class="notice notice-error is-dismissible">
				<p><?php echo esc_html( __( 'An error occurred while saving the settings.', 'easy-social-login' ) ); ?></p>
				<p><?php settings_errors( 'eslp_easy_social_login_settings' ); ?></p>
			</div>
			<?php
		}
	}

	/**
	 * Renders the settings page for the Easy Social Login plugin.
	 *
	 * This function displays a tabbed interface for managing the social login plugin's settings.
	 * Depending on the active tab, it loads the appropriate settings section.
	 */
	public function eslp_settings_page() {
		?>
		<div class="wrap eslp-easy-social-login">
			<h1><?php echo esc_html( __( 'Social Login Settings', 'easy-social-login' ) ); ?></h1>

			<h2 class="nav-tab-wrapper">
				<a href="?page=easy-social-login&eslp_module_tab=eslp_config" class="nav-tab
					<?php
						$tab = isset( $_GET['eslp_module_tab'] ) ? sanitize_text_field( wp_unslash( $_GET['eslp_module_tab'] ) ) : '';
						if ( $tab === 'eslp_config' || empty( $tab ) ){
							echo esc_attr( 'nav-tab-active' );
						}
					?> ">
						<?php echo esc_html( __( 'Settings', 'easy-social-login' ) ); ?>
				</a>
				<?php self::eslp_load_social_tabs(); ?>
			</h2>

			<form method="post" action="options.php" class="admin-container">
				<?php
				settings_fields( 'eslp_easy_social_login_settings_group' );
				do_settings_sections( 'easy-social-login' );

				$tab = isset( $_GET['eslp_module_tab'] ) ? sanitize_text_field( wp_unslash( $_GET['eslp_module_tab'] ) ) : '';
				$active_tab = isset( $tab ) ? $tab : 'eslp_config';

				switch ( $active_tab ) {
					case 'facebook':
					case 'google':
						self::eslp_load_settings( $active_tab );
						break;
					default:
						self::eslp_general_settings();
						break;
				}
				?>
				<?php submit_button(); ?>
			</form>
		</div>
		<?php
	}

	/**
	 * Displays the general settings section for the Easy Social Login plugin.
	 *
	 * This function includes options for general settings, display preferences, WooCommerce integration,
	 * and the plugin's shortcode.
	 */
	private function eslp_general_settings() {
		?>
		<h2><?php echo esc_html( __( 'General Settings', 'easy-social-login' ) ); ?></h2>
		<table class="form-table">
			<tr>
				<th scope="row"><?php echo esc_html( __( 'New User Profile', 'easy-social-login' ) ); ?></th>
				<td>
					<select name="eslp_easy_social_login_user_role">
						<?php wp_dropdown_roles( get_option( 'eslp_easy_social_login_user_role' ) ); ?>
					</select>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php echo esc_html( __( 'New account notification', 'easy-social-login' ) ); ?></th>
				<td>
					<input type="checkbox" name="eslp_easy_social_login_notify_admin" value="yes" <?php echo esc_attr( checked( get_option( 'eslp_easy_social_login_notify_admin' ), 'yes' ) ); ?> /> <?php echo esc_html( __( '(Only Admin)', 'easy-social-login' ) ); ?><br/>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php echo esc_html( __( 'URL Redirect after login', 'easy-social-login' ) ); ?></th>
				<td>
					<input type="text" name="eslp_easy_social_login_redirect_url" placeholder="<?php echo esc_attr( __( 'If empty, redirects the user to the current page.', 'easy-social-login' ) ); ?>" value="<?php echo esc_attr( get_option( 'eslp_easy_social_login_redirect_url' ) ); ?>" />
				</td>
			</tr>
		</table>

		<h2><?php echo esc_html( __( 'Show settings', 'easy-social-login' ) ); ?></h2>
		<table class="form-table">
			<tr>
				<th scope="row"><?php echo esc_html( __( 'Display header message', 'easy-social-login' ) ); ?></th>
				<td>
					<input type="checkbox" name="eslp_easy_social_login_display_header_message" value="yes" <?php echo esc_attr( checked( get_option( 'eslp_easy_social_login_display_header_message' ), 'yes' ) ); ?> /> <?php echo esc_html( __( 'Header Message', 'easy-social-login' ) ); ?><br/>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php echo esc_html( __( 'Show button on page', 'easy-social-login' ) ); ?></th>
				<td>
					<input type="checkbox" name="eslp_easy_social_login_display_button_login" value="yes" <?php echo esc_attr( checked( get_option( 'eslp_easy_social_login_display_button_login' ), 'yes' ) ); ?> /> <?php echo esc_html( __( 'Login page', 'easy-social-login' ) ); ?><br/>
					<input type="checkbox" name="eslp_easy_social_login_display_button_register" value="yes" <?php echo esc_attr(  checked( get_option( 'eslp_easy_social_login_display_button_register' ), 'yes' ) ); ?> /> <?php echo esc_html( __( 'Register page', 'easy-social-login' ) ); ?>
				</td>
			</tr>
		</table>

		<?php
			// Check WooCommerce status and add appropriate actions.
			$woo_status = self::$eslp_easy_social_login_extra->eslp_check_woocommerce_status();
			if ( $woo_status[0] == 'active' ) {
		?>
				<h2>WooCommerce</h2>
		
				<table class="form-table">
					<tr>
						<th scope="row"><?php echo esc_html( __( 'Button position', 'easy-social-login' ) ); ?></th>
						<td>
							<select name="eslp_easy_social_login_button_position">
								<option value="after" <?php echo esc_html( selected( get_option( 'eslp_easy_social_login_button_position' ), 'after' ) ); ?>><?php echo esc_html( __( 'After form', 'easy-social-login' ) ); ?></option>
								<option value="before" <?php echo esc_html( selected( get_option( 'eslp_easy_social_login_button_position' ), 'before' ) ); ?>><?php echo esc_html( __( 'Before form', 'easy-social-login' ) ); ?></option>
							</select>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php echo esc_html( __( 'Show social button on WooCommerce', 'easy-social-login' ) ); ?></th>
						<td>
							<input type="checkbox" name="eslp_easy_social_login_woocommerce_login" value="yes" <?php echo esc_attr( checked( get_option( 'eslp_easy_social_login_woocommerce_login' ), 'yes' ) ); ?> /> <?php echo esc_html( __( 'Login page', 'easy-social-login' ) ); ?><br/>
							<input type="checkbox" name="eslp_easy_social_login_woocommerce_register" value="yes" <?php echo esc_attr( checked( get_option( 'eslp_easy_social_login_woocommerce_register' ), 'yes' ) ); ?> /> <?php echo esc_html( __( 'Register page', 'easy-social-login' ) ); ?><br/>
							<input type="checkbox" name="eslp_easy_social_login_woocommerce_checkout" value="yes" <?php echo esc_attr( checked( get_option( 'eslp_easy_social_login_woocommerce_checkout' ), 'yes' ) ); ?> /> <?php echo esc_html( __( 'Checkout page', 'easy-social-login' ) ); ?>
						</td>
					</tr>
				</table>
		<?php
			}
		?>

		<h2>Shortcode</h2>
		<table class="form-table">
			<tr>
				<th scope="row"><?php echo esc_html( __( 'Copy and paste to show buttons anywhere', 'easy-social-login' ) ); ?></th>
				<td>
					<input type="text" id="shortcode" name="shortcode" value="[eslp_easy_social_login]" readonly>
					<a href="javascript:void(0);" class="eslp_copy_content" data-copy-target="shortcode"><?php echo esc_html( __( 'Copy Shortcode', 'easy-social-login' ) ); ?></a>
				</td>
			</tr>
		</table>
		<?php
	}

	/**
	 * Load and display social module tabs in the settings page.
	 *
	 * This function generates navigation tabs for all loaded social modules 
	 * and displays them on the settings page. Each tab links to its respective 
	 * module's settings.
	 *
	 * @return void
	 */
	private function eslp_load_social_tabs() {
		foreach (self::$eslp_module_instance as $eslp_module) {
			$module_slug = $eslp_module->eslp_module_get_slug();
			$module_name = $eslp_module->eslp_module_get_name();
			
			$tab = isset($_GET['eslp_module_tab']) ? sanitize_text_field(wp_unslash($_GET['eslp_module_tab'])) : '';
			$active_class = ($tab === $module_slug) ? 'nav-tab-active' : '';

			?>
			<a href="?page=easy-social-login&eslp_module_tab=<?php echo esc_html( $module_slug ); ?>" class="nav-tab <?php echo esc_attr( $active_class ); ?>">
					<span class="dashicons dashicons-<?php echo esc_attr( $module_slug ); ?>"></span>
					<?php echo esc_html( $module_name ); ?>
				 </a>
			<?php
		}        
	}

	/**
	 * Load settings for the currently active social module.
	 *
	 * This function identifies the active module based on the active tab slug 
	 * and renders the settings form for that specific module if the corresponding 
	 * method exists.
	 *
	 * @param string $active_tab The slug of the currently active tab.
	 * @return void
	 */
	private function eslp_load_settings($active_tab) {
		foreach (self::$eslp_module_instance as $eslp_module) {
			$module_slug = method_exists($eslp_module, 'eslp_module_get_slug') ? $eslp_module->eslp_module_get_slug() : null;

			if ($module_slug === $active_tab) {
				if (method_exists($eslp_module, 'eslp_module_render_settings')) {
					$eslp_module->eslp_module_render_settings();
				}                
				break;
			}
		}
	}

	
	// Work-in-progress: Render 2FA page
	/**
	 * Render the Two-Factor Authentication page.
	 *
	 * This function checks for the presence of the 'two_factor_auth' query variable. 
	 * If detected, it sets the status header to 200, includes the two-factor 
	 * authentication template, and terminates further execution.
	 *
	 * @return void
	 */
	public function eslp_render_2fa_page() {
		if (get_query_var('two_factor_auth', false)) {
			status_header(200);
			include plugin_dir_path(dirname(__FILE__)) . 'templates/two-factor-auth.php';
			exit;
		}
	}
	// Work-in-progress: Render 2FA page

}