<?php
/**
 * Main plugin class.
 *
 * @package EverestForms\UserRegistration
 *
 * @since   1.7.9
 */

namespace  EverestForms\Pro\Addons\UserRegistration;

use EverestForms\Pro\Addons\UserRegistration\Builder\EVFUR_Form_Settings;
use EverestForms\Pro\Addons\UserRegistration\Frontend\Everest_Forms_Login_Shortcode;
use EverestForms\Pro\Addons\UserRegistration\Frontend\EVF_User_Activation;
use EverestForms\Pro\Addons\UserRegistration\Process\EVF_User_Registration_Process;


/**
 * Main plugin class.
 *
 * @since 1.7.9
 */
class UserRegistration {


	/**
	 * Plugin Constructor.
	 *
	 * @since 1.7.9
	 */
	public function __construct() {
		$this->init();
		$this->hook_inits();
	}

	/**
	 * Init function.
	 *
	 * @since 1.7.9
	 */
	public function init() {
		if ( is_admin() ) {
			new EVFUR_Form_Settings();
		}

		new EVF_User_Activation();
		new EVF_User_Registration_Process();
		new Everest_Forms_Login_Shortcode();
		$this->load_abstract_classes();
	}

	/**
	 * Hook inits.
	 *
	 * @since 1.7.9
	 */
	public function hook_inits() {
		if ( is_admin() ) {
			add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
			add_filter( 'everest_forms_general_settings', array( $this, 'general_settings' ), 10, 1 );
			add_filter( 'everest_forms_get_settings_pages', array( $this, 'load_settings_pages' ) );
		}
		add_action( 'everest_forms_frontend_output', array( $this, 'append_social_login_button_for_user_registration' ), 99 );
	}

	/**
	 * Load settings page.
	 *
	 * @param  array $settings Settings page.
	 * @return array of settings page.
	 */
	public function load_settings_pages( $settings ) {
		include_once __DIR__ . '/Settings/EVF_Settings_Social_Login.php';
		$settings[] = new \EverestForms\Pro\Addons\UserRegistration\Settings\EVF_Settings_Social_Login();

		return $settings;
	}

	/**
	 * Load abstract classes.
	 *
	 * @since 1.7.9
	 */
	public function load_abstract_classes() {
		include_once __DIR__ . '/Abstract/EVF_Social_Networks.php';
	}

	/**
	 * Enqueue scripts.
	 */
	public function admin_scripts() {
		$screen    = get_current_screen();
		$screen_id = $screen ? $screen->id : '';
		$suffix    = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';

		// Register admin scripts.
		wp_register_script( 'everest-forms-user-registration-builder', plugins_url( "src/Addons/UserRegistration/assets/js/admin/admin{$suffix}.js", EFP_PLUGIN_FILE ), array( 'jquery', 'jquery-ui-sortable' ), EFP_VERSION, true );
		wp_localize_script(
			'everest-forms-user-registration-builder',
			'evf_user_registration_params',
			array(
				'i18n_disabled_message' => esc_html__( 'User registration is currently disabled. Please enable it to register users with Everest Forms.', 'everest-forms-user-registration' ),
			)
		);

		// Admin scripts for EVF builder page.
		if ( 'everest-forms_page_evf-builder' === $screen_id || 'everest-forms_page_evf-settings' === $screen_id ) {
			wp_enqueue_script( 'everest-forms-user-registration-builder' );
		}
	}

	/**
	 * Add checkbox box for enabling/disabling forgot password.
	 *
	 * @param mixed $settings Settings.
	 *
	 * @return $settings Settings.
	 */
	public function general_settings( $settings ) {
		array_splice(
			$settings,
			5,
			0,
			array(
				array(
					'title'    => esc_html__( 'Enable Forgot Password.', 'everest-forms' ),
					'desc'     => esc_html__( 'Enable forgot password.', 'everest-forms' ),
					'id'       => 'everest_forms_lost_password',
					'default'  => 'no',
					'type'     => 'toggle',
					'desc_tip' => true,
				),
			)
		);
		return $settings;
	}

	/**
	 * Append social login button.
	 *
	 * @since 1.7.9
	 *
	 * @param  array $form_data Form data.
	 */
	public function append_social_login_button_for_user_registration( $form_data ) {
		$value = isset( $form_data['settings']['enable_user_registration'] ) ? $form_data['settings']['enable_user_registration'] : 0;

		if ( $value ) {
			$social_networks  = Helper::everest_forms_social_networks();
			$enabled_networks = array();

			foreach ( $social_networks as $network_key => $network_data ) {
				if ( 'yes' === get_option( 'everest_forms_social_setting_enable_social_registration' ) && 'yes' === get_option( $network_data['enable_id'] ) ) {
					$enabled_networks[ $network_key ] = $network_data;
				}
			}

			$redirect_to = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';

			$url = ( ! empty( $_SERVER['HTTPS'] ) ) ? 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] : 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

			$formatted_url = substr( $url, 0, strpos( $url, '?' ) );

			$encoded_url = '';

			echo '<div class="everest-forms-social-connect-networks">';
			if ( count( $enabled_networks ) > 0 ) {
				?>
					<ul class="evfur-network-lists evfur_theme_4">
							<?php
							foreach ( $enabled_networks as $network_key => $network_data ) {

								$social_login_network_select = isset( $form_data['settings']['social_login_network_select'] ) ? $form_data['settings']['social_login_network_select'] : array();
								if ( empty( $social_login_network_select ) ) {
									continue;
								}
								if ( ! in_array( $network_key, $social_login_network_select ) ) {
									continue;
								}

								?>
								<li class="evfur-login-media evfur-login-media--<?php echo $network_key; ?>">
									<a href="<?php echo $formatted_url; ?>?everest_forms_social_login=<?php echo $network_key; ?>&evfur_action=login
												<?php
												if ( $encoded_url ) {
													echo '&state=' . base64_encode( "redirect_to=$encoded_url" );
												}
												?>
									" title='
									<?php
									_e( 'Login with', 'everest-forms-pro' );
									echo ' ' . $network_key;
									?>
									'>
									<span class="evfur-icon-block icon-<?php echo $network_key; ?> evfur-login-with-<?php echo $network_key; ?>"></span>
									<span class="evfur-login-text"><?php echo esc_html( get_option( $network_data['login_text'] ) ); ?></span>
									</a>
								</li>
								<?php
							}
							?>
					</ul>

				<?php
				echo '</div>';
			}
		}
	}
}
