<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://belovdigital.agency
 * @since      1.0.0
 *
 * @package    ultimate-pregnancy-ovulation-calculator
 * @subpackage ULPC/admin
 */

/**
 * Global sanitization function for ULPC options.
 *
 * @since    1.0.0
 *
 * @param array $input Form Input.
 * @return array Sanitized input.
 */
function ulpc_sanitize_options( $input ) {

	// If input is not an array, return an empty array.
	if ( ! is_array( $input ) ) {
		return array();
	}

	$all_options = array(
		'title_show',
		'title_text',
		'primary_color',
		'font_size',
		'calculator_type',
		'calculator_method',
		'date_format',
		'disable_custom_font',
	);

	$switch_options = array(
		'title_show',
	);

	$sanitized_input = array();

	foreach ( $all_options as $option ) {
		if ( in_array( $option, $switch_options, true ) ) {
			// For switch options, check if the value is explicitly 'show'.
			$sanitized_input[ $option ] = ( isset( $input[ $option ] ) && 'show' === $input[ $option ] ) ? 'show' : 'hide';
		} else {
			if ( isset( $input[ $option ] ) && '' !== $input[ $option ] ) {
				$sanitized_input[ $option ] = sanitize_text_field( $input[ $option ] );
			} elseif ( isset( $input[ $option ] ) && '' === $input[ $option ] ) {
				$sanitized_input[ $option ] = 'empty';
			} else {
				$sanitized_input[ $option ] = '';
			}
		}
	}

	return $sanitized_input;
}

/**
 * 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    ultimate-pregnancy-ovulation-calculator
 * @subpackage ULPC/admin
 */
class ULPC_Admin {

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

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @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;
	}


	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 *
	 * @param string $hook Hook name.
	 */
	public function enqueue_styles( $hook ) {
		if ( 'toplevel_page_' . $this->plugin_name !== $hook ) {
			return;
		}
		wp_enqueue_style( 'wp-color-picker' );
		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/ulpc-admin.min.css', array(), $this->version, 'all' );
	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 *
	 * @param string $hook Hook name.
	 */
	public function enqueue_scripts( $hook ) {
		if ( 'toplevel_page_' . $this->plugin_name !== $hook ) {
			return;
		}
		wp_enqueue_script( 'underscore' );
		wp_enqueue_script( 'wp-color-picker' );
		wp_enqueue_script( $this->plugin_name . '-vendor', plugin_dir_url( __FILE__ ) . 'js/ulpc-vendor.min.js', array( 'jquery' ), $this->version, true );
		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/ulpc-admin.min.js', array( 'jquery' ), $this->version, true );

		wp_localize_script(
			$this->plugin_name,
			'ulpc_vars',
			array(
				'ajaxurl' => admin_url( 'admin-ajax.php' ),
				'i18n'    => array(
					'copied'        => __( 'Copied!', 'ultimate-pregnancy-ovulation-calculator' ),
					'copyShortcode' => __( 'Copy shortcode', 'ultimate-pregnancy-ovulation-calculator' ),
				),

			)
		);
	}

	/**
	 * Add plugin settings page.
	 *
	 * @since    1.0.0
	 */
	public function add_settings_page() {

		$icon_url = ULPC_PLUGIN_URL . 'admin/images/pregnancy-icon.svg';

		add_menu_page(
			__( 'WP Pregnancy & Ovulation Calculator', 'ultimate-pregnancy-ovulation-calculator' ),
			__( 'Pregnancy & Ovulation', 'ultimate-pregnancy-ovulation-calculator' ),
			'manage_options',
			$this->plugin_name,
			array( $this, 'settings_page_callback' ),
			$icon_url,
			null
		);
	}

	/**
	 * Add "Download PRO" submenu link.
	 *
	 * @since    1.2.0
	 */
	public function go_pro_menu() {
		add_submenu_page(
			$this->plugin_name,
			__( 'Download PRO', 'ultimate-pregnancy-ovulation-calculator' ),
			__( 'Download PRO', 'ultimate-pregnancy-ovulation-calculator' ),
			'manage_options',
			'#ulpc-go-pro',
			null,
			99
		);
	}

	/**
	 * Sanitize plugin settings.
	 *
	 * @since    1.0.0
	 *
	 * @param array $input Form Input.
	 * @return array Sanitized input.
	 */
	public function sanitize_options( $input ) {
		return ulpc_sanitize_options( $input );
	}

	/**
	 * Register plugin settings.
	 *
	 * @since    1.0.0
	 */
	public function register_settings() {
		// Register setting with a global function callback.
		register_setting(
			'ulpc-settings-group',       // Option group.
			'ulpc_options',              // Option name.
			'ulpc_sanitize_options'      // Global sanitization function.
		);
	}

	/**
	 * Plugin settings page markup.
	 *
	 * @since    1.0.0
	 */
	public function settings_page_callback() {
		ob_start();
		include 'partials/admin-display.php';
		echo ob_get_clean(); //phpcs:ignore.
	}

	/**
	 * Plugin settings link.
	 *
	 * @since    1.0.0
	 *
	 * @param array $links Links.
	 */
	public function plugin_settings_link( $links ) {
		$pro_link      = '<a href="https://wp-pregnancy.com" target="_blank" style="color:#e3609a;font-weight:600;">' . __( 'Get PRO', 'ultimate-pregnancy-ovulation-calculator' ) . '</a>';
		$settings_link = '<a href="admin.php?page=ultimate-pregnancy-ovulation-calculator">' . __( 'Settings', 'ultimate-pregnancy-ovulation-calculator' ) . '</a>';
		array_unshift( $links, $settings_link );
		array_unshift( $links, $pro_link );
		return $links;
	}
}
