<?php
/**
 * Admin page registration and rendering.
 *
 * @package CoffeeWP_Media_Cleanup
 */

namespace CWPM\Admin;

use CWPM\Core\Db;

defined( 'ABSPATH' ) || exit;

class Page {

	/**
	 * Menu slug.
	 *
	 * @var string
	 */
	const MENU_SLUG = 'coffeewp-media-cleanup';

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'admin_menu', array( $this, 'register_menu' ) );

		add_action( 'cwpm_admin_page_loaded', array( \CWPM\Core\Logger::init(), 'maybe_purge_old_logs' ) );

		add_filter( 'admin_body_class', array( $this, 'add_body_class' ) );
		add_action( 'admin_notices', array( $this, 'inject_before_notices' ), -9999 );
		add_action( 'admin_notices', array( $this, 'inject_after_notices' ), PHP_INT_MAX );
	}

	/**
	 * Return the merged nav / scan-meta state with defaults filled in.
	 *
	 * @return array{
	 *     status: string,
	 *     scan_id: string,
	 *     started_at: ?int,
	 *     completed_at: ?int,
	 *     scanned_files: int,
	 *     total_files: int,
	 *     last_file_index: int,
	 *     current_view: string,
	 *     current_step: int,
	 * }
	 */
	public static function get_nav_state() {
		$meta = Db::get_scan_meta();
		if ( ! is_array( $meta ) ) {
			$meta = array();
		}
		return wp_parse_args(
			$meta,
			array(
				'status'          => 'idle',
				'scan_id'         => '',
				'started_at'      => null,
				'completed_at'    => null,
				'scanned_files'   => 0,
				'total_files'     => 0,
				'last_file_index' => 0,
				'current_view'    => 'wizard',
				'current_step'    => 1,
			)
		);
	}

	/**
	 * Check if the current screen is a plugin page.
	 *
	 * @return bool
	 */
	public static function is_plugin_page() {
		$screen = get_current_screen();
		return $screen && false !== strpos( $screen->id, self::MENU_SLUG );
	}

	/**
	 * Add body class on plugin pages for CSS targeting.
	 *
	 * @param string $classes Existing body classes.
	 * @return string
	 */
	public function add_body_class( $classes ) {
		if ( self::is_plugin_page() ) {
			$classes .= ' cwpm-admin-page';
		}
		return $classes;
	}

	/**
	 * Wrap admin notices in a visually-hidden div on plugin pages.
	 */
	public function inject_before_notices() {
		if ( ! self::is_plugin_page() ) {
			return;
		}
		echo '<div class="cwpm-notice-hide" style="position:absolute !important; left:-9999px !important; top:auto !important; width:1px !important; height:1px !important; overflow:hidden !important;">';
		echo '<div class="wp-header-end"></div>';
	}

	/**
	 * Close the hidden notices wrapper.
	 */
	public function inject_after_notices() {
		if ( ! self::is_plugin_page() ) {
			return;
		}
		echo '</div>';
	}

	/**
	 * Register the top-level admin menu.
	 */
	public function register_menu() {
		add_menu_page(
			__( 'CoffeeWP Media Cleanup', 'coffeewp-media-cleanup' ),
			__( 'Media Cleanup', 'coffeewp-media-cleanup' ),
			'manage_options',
			self::MENU_SLUG,
			array( $this, 'render' ),
			'dashicons-images-alt2',
			26
		);
	}

	/**
	 * Render the main admin page.
	 */
	public function render() {
		do_action( 'cwpm_admin_page_loaded' );
		include CWPM_PATH . 'templates/admin-page.php';
	}
}
