<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://www.francescopepe.com
 * @since      1.0.0
 *
 * @package    Popper
 * @subpackage Popper/admin
 */

namespace Popper;

/**
 * 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    Popper
 * @subpackage Popper/admin
 * @author     Francesco Pepe <info@francescopepe.com>
 */
class 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;

	/**
	 * The main file of this plugin.
	 *
	 * @since    2.6.0
	 * @access   private
	 * @var      string    $version    The main file of this plugin.
	 */
	private $entry_point;

	/**
	 * 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.
	 * @param string $entry_point The version of this plugin.
	 */
	public function __construct( $plugin_name, $version, $entry_point ) {
		$this->plugin_name = $plugin_name;
		$this->version = $version;
		$this->entry_point = $entry_point;
	}

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {
		wp_enqueue_style( 'popper-pro-dashboard' );
		wp_enqueue_style( 'popper-dashboard' );
	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {
		wp_enqueue_script( 'popper-dashboard' );
		wp_enqueue_script( 'popper-pro-admin' );
	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function register_scripts() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Popper_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Popper_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		$dir                  = plugin_dir_path( $this->entry_point );
		$dashboard_asset_path = "$dir/build/admin.asset.php";

		$dashboard_asset = require $dashboard_asset_path;

		wp_register_script(
			'popper-dashboard',
			plugin_dir_url( $this->entry_point ) . '/build/admin.js',
			$dashboard_asset['dependencies'],
			$dashboard_asset['version'],
			true
		);

		wp_register_style(
			'popper-dashboard',
			plugin_dir_url( $this->entry_point ) . '/build/style-admin.css',
			array( 'wp-components', 'wp-reset-editor-styles' ),
			$dashboard_asset['version'],
		);
	}

	/**
	 * Register the positions for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function remove_classics( $theme_json ) {

		$data = array(
			'version'  => 2,
			'settings' => array(
				'shadow' => array(
					'defaultPresets' => false,
				),
			),
		);

		return $theme_json->update_with( $data );
	}

	/**
	 * Register the positions for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function popper_positions() {
		$screen = get_current_screen();

		if ( 'popper' === $screen->id || 'toplevel_page_popper' === $screen->id ) {
			$positions = \Popper\Conditions::get_conditions();
			$users     = \Popper\Conditions::get_user_conditions();

			wp_localize_script(
				'wp-block-directory',
				'popper',
				array(
					'positions' => $positions,
					'users'     => $users,
					'pro'       => (bool) is_plugin_active( 'popper-pro/popper-pro.php' ),
				)
			);

			wp_localize_script(
				'popper-dashboard',
				'popper',
				array(
					'positions' => $positions,
					'users'     => $users,
				)
			);
		}
	}

	/**
	 * Add menu.
	 *
	 * @since    1.0.0
	 */
	public function menu() {
		$slug       = 'popper';
		$capability = 'manage_options';

		$dashboard_hook = add_menu_page(
			__( 'Popper popups', 'popper' ),
			'Popups',
			'edit_posts',
			$slug,
			array( $this, 'settings_page' ),
			'dashicons-external',
			58
		);

		$analytics_hook = add_submenu_page(
			$slug,
			__( 'Popper analytics', 'popper' ),
			'Analytics',
			$capability,
			'popper-analytics',
			array( $this, 'settings_page' ),
			6
		);

		$settings_hook = add_submenu_page(
			$slug,
			__( 'Popper settings', 'popper' ),
			'Settings',
			$capability,
			'popper-settings',
			array( $this, 'settings_page' ),
			58
		);

		add_action( 'load-' . $dashboard_hook, array( $this, 'load_popper_scripts' ) );
		add_action( 'load-' . $settings_hook, array( $this, 'load_popper_scripts' ) );
		add_action( 'load-' . $analytics_hook, array( $this, 'load_popper_scripts' ) );
	}

	/**
	 * Display settings page
	 *
	 * @return void
	 */
	public function settings_page() {
		?>
		<div id="popper"></div>
		<?php
	}

	/**
	 * Output Formello styles and scripts.
	 *
	 * @since 2.6.0
	 */
	public function load_popper_scripts() {
		$this->enqueue_styles();
		$this->enqueue_scripts();

		do_action( 'popper_settings_scripts' );
	}

	/**
	 * Redirect CPT page to Popper page
	 *
	 * @return void
	 */
	public function redirect_post_type() {
		$screen = get_current_screen();
		if ( 'edit-popper' === $screen->id ) {
			wp_safe_redirect( 'admin.php?page=popper' );
			exit;
		}
	}

	/**
	 * Add popper CPT to link search query
	 *
	 * @param array $query
	 * @return array
	 */
	public function wp_link_query_args( $query ) {
		if ( is_user_logged_in() ) {
			$query['post_type'][] = 'popper';
		}
		return $query;
	}

	/**
	 * Set link to '#' in link search query
	 *
	 * @param array $query
	 * @return array
	 */
	public function post_type_link( $post_link, $post ) {
		if ( 'popper' === $post->post_type ) {
			return '#';
		}

		return $post_link;
	}

	/**
	 * Activate pruning log
	 *
	 * @param boolean $should_we_prune
	 * @return boolean
	 */
	public function activate_pruning( $should_we_prune ) {
		return true;
	}
}
