<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://blockvis.com
 * @since      1.0.0
 *
 * @package    Civic_Sip
 * @subpackage Civic_Sip/admin
 */

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Civic_Sip
 * @subpackage Civic_Sip/admin
 * @author     Blockvis <blockvis@blockvis.com>
 */
class Civic_Sip_Admin {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string $plugin_name The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string $version The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 *
	 * @param      string $plugin_name The name of this plugin.
	 * @param      string $version     The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version     = $version;

	}

	/**
	 * @since 1.0.0
	 */
	public function register_settings_page() {

		add_menu_page(
			__( 'Civic QR Auth Settings', 'civic-sip' ),
			__( 'Civic QR Auth', 'civic-sip' ),
			'manage_options',
			'civic-qr-auth',
			[ $this, 'display_settings_page' ],
			'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIj8+Cjxzdmcgd2lkdGg9IjY0MCIgaGVpZ2h0PSI0ODAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6c3ZnPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPgogPCEtLSBDcmVhdGVkIHdpdGggU1ZHLWVkaXQgLSBodHRwOi8vc3ZnLWVkaXQuZ29vZ2xlY29kZS5jb20vIC0tPgogPGRlZnM+CiAgPHN5bWJvbCBpZD0ic3ZnXzEiIHZpZXdCb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iA8L2RlZnM+CiA8Zz4KICA8dGl0bGU+TGF5ZXIgMTwvdGl0bGU+CiAgPHVzZSBmaWxsPSIjZmZmZmZmIiB4PSIwIiB5PSIwIiB4bGluazpocmVmPSIjc3ZnXzEiIGlkPSJzdmdfMiIvPgogIDxnIGlkPSJzdmdfMyIvPgogPC9nPgo8L3N2Zz4=',
			90
		);

	}

	/**
	 * Renders admin settings page.
	 *
	 * @since 1.0.0
	 */
	public function display_settings_page() {

		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/civic-sip-admin-display.php';

	}

	/**
	 * Registers settings page sections and fields.
	 *
	 * @since 1.0.0
	 */
	public function register_settings() {

		register_setting(
			$this->plugin_name . '-settings',
			$this->plugin_name . '-settings'
		);

		add_settings_section(
			$this->plugin_name . '-settings-section',
			'',
			[ $this, 'add_settings_section' ],
			$this->plugin_name . '-settings'
		);

		add_settings_field(
			'app_id',
			__( 'App ID', 'civic-sip' ),
			[ $this, 'add_settings_field_input_text' ],
			$this->plugin_name . '-settings',
			$this->plugin_name . '-settings-section',
			[
				'value_for' => 'app_id',
			]
		);

		add_settings_field(
			'privkey',
			__( 'Private Signing Key', 'civic-sip' ),
			[ $this, 'add_settings_field_input_textarea' ],
			$this->plugin_name . '-settings',
			$this->plugin_name . '-settings-section',
			[
				'value_for' => 'privkey',
			]
		);

		add_settings_field(
			'pubkey',
			__( 'Public Signing Key ', 'civic-sip' ),
			[ $this, 'add_settings_field_input_textarea' ],
			$this->plugin_name . '-settings',
			$this->plugin_name . '-settings-section',
			[
				'value_for' => 'pubkey',
				'rows'      => 5,
			]
		);

		add_settings_field(
			'secret',
			__( 'Secret', 'civic-sip' ),
			[ $this, 'add_settings_field_input_text' ],
			$this->plugin_name . '-settings',
			$this->plugin_name . '-settings-section',
			[
				'value_for' => 'secret',
			]
		);

		add_settings_section(
			$this->plugin_name . '-settings-auth-section',
			'WP User Authentication',
			[ $this, 'add_settings_auth_section' ],
			$this->plugin_name . '-settings'
		);

		add_settings_field(
			'wp_user_auth_enabled',
			__( 'Enable WP User Authentication', 'civic-sip' ),
			[ $this, 'add_settings_field_single_checkbox' ],
			$this->plugin_name . '-settings',
			$this->plugin_name . '-settings-auth-section',
			[
				'value_for' => 'wp_user_auth_enabled',
			]
		);

		add_settings_field(
			'show_civic_button',
			__( 'Show Civic Sign In Button', 'civic-sip' ),
			[ $this, 'add_settings_field_single_checkbox' ],
			$this->plugin_name . '-settings',
			$this->plugin_name . '-settings-auth-section',
			[
				'value_for' => 'show_civic_button',
			]
		);

		add_settings_section(
			$this->plugin_name . '-settings-shortcode-section',
			'Short Code',
			[ $this, 'add_settings_shortcode_section' ],
			$this->plugin_name . '-settings'
		);

	}

	/**
	 * Renders text input field.
	 *
	 * @since 1.0.0
	 */
	public function add_settings_field_input_text( $args ) {

		$field_id = $args['value_for'];
		$options  = get_option( $this->plugin_name . '-settings' );
		$option   = isset( $options[ $field_id ] ) ? $options[ $field_id ] : '';
		?>
		<input type="text" name="<?php echo $this->plugin_name . '-settings[' . $field_id . ']'; ?>"
		       id="<?php echo $this->plugin_name . '-settings[' . $field_id . ']'; ?>"
		       value="<?php echo esc_attr( $option ); ?>" class="regular-text"/>
		<?php

	}

	/**
	 * Renders textarea input field.
	 *
	 * @since 1.0.0
	 */
	public function add_settings_field_input_textarea( $args ) {

		$field_id = $args['value_for'];
		$options  = get_option( $this->plugin_name . '-settings' );
		$option   = isset( $options[ $field_id ] ) ? $options[ $field_id ] : '';
		?>
		<textarea name="<?php echo $this->plugin_name . '-settings[' . $field_id . ']'; ?>"
		          id="<?php echo $this->plugin_name . '-settings[' . $field_id . ']'; ?>"
		          rows="<?php echo isset( $args['rows'] ) ? $args['rows'] : 3 ?>"
		          class="regular-text"><?php echo esc_attr( $option ); ?></textarea>
		<?php

	}

	/**
	 * Renders checkbox input field.
	 *
	 * @since 1.0.0
	 */
	public function add_settings_field_single_checkbox( $args ) {

		$field_id = $args['value_for'];
		$options  = get_option( $this->plugin_name . '-settings' );
		$option   = isset( $options[ $field_id ] ) ? $options[ $field_id ] : 0;

		?>

		<label for="<?php echo $this->plugin_name . '-settings[' . $field_id . ']'; ?>">
			<input type="checkbox"
			       name="<?php echo $this->plugin_name . '-settings[' . $field_id . ']'; ?>"
			       id="<?php echo $this->plugin_name . '-settings[' . $field_id . ']'; ?>"
				<?php checked( 1, $option, 1 ); ?>
				   value="1"/>
		</label>

		<?php

	}

	/**
	 * Renders general settings section.
	 *
	 * @since 1.0.0
	 */
	public function add_settings_section() {

		?>
		<p>
			<?php echo esc_html__(
				'Allow users to securely login and register for your site using the Civic Secure Identity Platform.' .
				' To get started, you will need to register with the Civic app and then use the app to log into the developer portal to obtain your App ID and API keys.',
				'civic-sip' ); ?>
			<br>
			<a href="https://www.civic.com/app"
			   target="_blank"><?php echo esc_html__( 'Civic Signup', 'civic-sip' ); ?></a> |
			<a href="https://sip-partners.civic.com/"
			   target="_blank"><?php echo esc_html__( 'Partner Dev Portal', 'civic-sip' ); ?></a>
		</p>
		<h2>Integration Settings</h2>
		<p>
			<?php printf( esc_html__(
				'To gain approval to use of the Civic QR Auth on your public site, all of the following fields are required and will be managed through the %s. Please note: If any fields are left blank, the shortcode will not display anything on the frontend of your site.',
				'civic-sip' ),
				'<a href="https://sip-partners.civic.com/" target="_blank">' .
				esc_html__( 'Partner Dev Portal', 'civic-sip' ) .
				'</a>' );
			?>
		</p>
		<?php

	}

	/**
	 * Renders Auth settings section.
	 *
	 * @since 1.0.0
	 */
	public function add_settings_auth_section() {

		?>
		<p>
			<?php echo esc_html__(
				'A User signing in with Civic will result in JSON data about that user being returned to your site from Civic. If WP User Authentication is not activated, you will need to handle the response to successful Civic authentication with your own custom code.',
				'civic-sip' ); ?>
			<a href="https://docs.civic.com/" target="_blank">
				<?php echo esc_html__( 'Documentation', 'civic-sip' ); ?>
			</a>
			<br>
			<?php echo esc_html__(
				'When activated, upon successful QR code sign in, the User will be matched based on email address to a User in your WP database and logged in to your site. If no match is found, they will need to confirm being registered as a new User.',
				'civic-sip' ); ?>
		</p>
		<?php

	}

	/**
	 * Renders Shortcode settings section.
	 *
	 * @since 1.0.0
	 */
	public function add_settings_shortcode_section() {

		?>
		<p>
			<?php printf( esc_html__(
				'Use the shortcode %s in your WP content or templates to display the “Sign In With Civic” button.',
				'civic-sip' ), '<strong>[civic-auth]</strong>' ); ?>
			<br>
			<?php printf(esc_html__(
				'If you like to style the button and want to add a custom CSS class you can use shortcode "class" parameter as following %s.',
				'civic-sip' ), '<strong>[civic-auth class="your-custom-class"]</strong>'); ?>
		</p>
		<?php

	}

	/**
	 * Deactivates plugin
	 *
	 * @since 1.1.0
	 */
	public function deactivate() {

		deactivate_plugins( 'civic-sip/civic-sip.php' );
		if ( isset( $_GET['activate'] ) ) {
			unset( $_GET['activate'] );
		}

	}

	/**
	 * Render admin notice to version requirements mismatch
	 *
	 * @since 1.1.0
	 */
	public function version_notice() {

		$this->render_notice(
			sprintf(
				__(
					'<strong>Civic SIP</strong> plugin requires at least WordPress %1$s and PHP %2$s.' .
					' You are currently running WordPress %3$s and PHP %4$s.' .
					' Please upgrade to activate the plugin.'
				),
				'4.0',
				'7.0',
				get_bloginfo( 'version' ),
				PHP_VERSION
			)
		);
	}

	/**
	 * Render admin notice to require GMP extension
	 *
	 * @since 1.1.0
	 */
	public function gmp_notice() {

		$this->render_notice(
			__(
				'<strong>Civic SIP</strong> plugin requires GMP php extension to run.' .
				' Please install and enable ext-gmp to activate the plugin.'
			)
		);

	}

	/**
	 * @param $message
	 *
	 * @return void
	 */
	private function render_notice( $message ) {

		printf( '<div class="error"><p>%s</p></div>', $message );

	}

}
