<?php
/**
 * Admin Hooks Class.
 *
 * This file contains the Admin_Hooks class which manages admin-specific hooks
 * for the Postomatic plugin.
 *
 * @package Postomatic
 * @since      1.0.0
 * @subpackage Postomatic\Admin
 */

namespace Postomatic\Admin;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class for managing public-specific hooks.
 */
class Admin_Hooks {

	/**
	 * Initialize the public hooks.
	 */
	public function init() {
		// Public-facing actions.
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
		add_action( 'init', array( $this, 'register_shortcodes' ) );

		// Public-facing filters.
		add_filter( 'the_content', array( $this, 'maybe_modify_content' ) );

		// Add Postomatic icon to the admin bar when user is logged in.
		add_action( 'admin_bar_menu', array( $this, 'add_postomatic_icon_to_admin_bar' ), 999 );

		// Enqueue styles for the admin bar icon.
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_admin_bar_styles' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_bar_styles' ) );

		// Filter admin notices to only show plugin's own notices on plugin pages.
		add_action( 'admin_notices', array( $this, 'filter_admin_notices' ), -999 );
	}

	/**
	 * Register and enqueue public-facing styles.
	 */
	public function enqueue_styles() {
		wp_enqueue_style(
			'postomatic-public',
			POSTOMATIC_PLUGIN_URL . 'includes/public/css/wp-postomatic-public.css',
			array(),
			POSTOMATIC_VERSION,
			'all'
		);
	}

	/**
	 * Register and enqueue public-facing scripts.
	 */
	public function enqueue_scripts() {
		// Enqueue scripts code.
	}

	/**
	 * Register shortcodes.
	 */
	public function register_shortcodes() {
		// Register shortcodes.
	}

	/**
	 * Modify content if needed.
	 *
	 * @param  string $content The post content.
	 * @return string The filtered content.
	 */
	public function maybe_modify_content( $content ) {
		// Content filter logic.
		return $content;
	}

	/**
	 * Add Postomatic icon to the WordPress admin bar.
	 *
	 * @param  \WP_Admin_Bar $wp_admin_bar WordPress admin bar object.
	 * @return void
	 */
	public function add_postomatic_icon_to_admin_bar( $wp_admin_bar ) {
		// Only add the icon if user is logged in.
		if ( ! is_user_logged_in() ) {
			return;
		}

		// Add the main Postomatic icon/link.
		$wp_admin_bar->add_node(
			array(
				'id'    => 'postomatic',
				'title' => '<span class="ab-icon dashicons dashicons-text"></span><span class="ab-label">Postomatic</span>',
				'href'  => admin_url( 'admin.php?page=postomatic' ),
				'meta'  => array(
					'title' => __( 'Postomatic', 'postomatic' ),
				),
			)
		);

		// Add submenu items.
		// Main page (Dashboard).
		$wp_admin_bar->add_node(
			array(
				'id'     => 'postomatic-main',
				'parent' => 'postomatic',
				'title'  => __( 'Add Post', 'postomatic' ),
				'href'   => admin_url( 'admin.php?page=postomatic' ),
			)
		);

		// Membership page.
		$wp_admin_bar->add_node(
			array(
				'id'     => 'postomatic-membership',
				'parent' => 'postomatic',
				'title'  => __( 'Subscription', 'postomatic' ),
				'href'   => admin_url( 'admin.php?page=postomatic-membership' ),
			)
		);

		// Add Credit page.
		$wp_admin_bar->add_node(
			array(
				'id'     => 'postomatic-add-credit',
				'parent' => 'postomatic',
				'title'  => __( 'Buy Credit', 'postomatic' ),
				'href'   => admin_url( 'admin.php?page=postomatic-add-credit' ),
			)
		);

		// Support page.
		$wp_admin_bar->add_node(
			array(
				'id'     => 'postomatic-support',
				'parent' => 'postomatic',
				'title'  => __( 'Support', 'postomatic' ),
				'href'   => admin_url( 'admin.php?page=postomatic-support' ),
			)
		);

		// Autopilot page.
		$wp_admin_bar->add_node(
			array(
				'id'     => 'postomatic-autopilot',
				'parent' => 'postomatic',
				'title'  => __( 'AutoPilot', 'postomatic' ),
				'href'   => admin_url( 'admin.php?page=postomatic-autopilot' ),
			)
		);
	}

	/**
	 * Enqueue styles for the admin bar icon.
	 *
	 * @return void
	 */
	public function enqueue_admin_bar_styles() {
		// We're using dashicons-text now, so no custom CSS is needed.
		// Ensure dashicons are loaded.
		wp_enqueue_style( 'dashicons' );
	}

	/**
	 * Filter admin notices to only show plugin's own notices on plugin pages.
	 *
	 * This prevents notices from other plugins from appearing on Postomatic pages.
	 *
	 * @return void
	 */
	public function filter_admin_notices() {
		global $hook_suffix;

		// Check if we're on a Postomatic admin page.
		if ( strpos( $hook_suffix, 'postomatic' ) !== false ) {
			// Remove all admin notices.
			remove_all_actions( 'admin_notices' );
			remove_all_actions( 'all_admin_notices' );

			// Add back only our notices.
			add_action( 'admin_notices', array( $this, 'display_postomatic_notices' ) );
		}
	}

	/**
	 * Display only Postomatic-specific notices.
	 *
	 * @return void
	 */
	public function display_postomatic_notices() {
		// Display activation notice if it exists.
		if ( class_exists( '\Postomatic\Core\Activator' ) ) {
			\Postomatic\Core\Activator::activation_admin_notice();
		}

		// Display PHP version notice if needed.
		$this->maybe_display_php_version_notice();
	}

	/**
	 * Display PHP version notice if PHP version is too low.
	 *
	 * @return void
	 */
	private function maybe_display_php_version_notice() {
		if ( version_compare( PHP_VERSION, '5.6', '<' ) ) {
			$message = sprintf(
				// translators: %1$s: Required PHP version, %2$s: Current PHP version.
				esc_html__(
					'Postomatic requires PHP %1$s+. You have %2$s. Please upgrade PHP.',
					'postomatic'
				),
				'5.6',
				esc_html( PHP_VERSION )
			);
			printf( '<div class="notice notice-error"><p>%s</p></div>', wp_kses_post( $message ) );
		}
	}
}
