<?php
/**
 * Main Plugin Class
 *
 * @package ResponsiveMobileSelect\Core
 * @since 2.0.0
 */

namespace ResponsiveMobileSelect\Core;

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Main plugin class.
 *
 * @since 2.0.0
 */
class Plugin {

	/**
	 * Plugin version.
	 *
	 * @since 2.0.0
	 * @var string
	 */
	const VERSION = RESPONSIVE_MOBILE_SELECT_VERSION;

	/**
	 * Settings instance.
	 *
	 * @since 2.0.0
	 * @var \ResponsiveMobileSelect\Core\Settings
	 */
	private $settings;

	/**
	 * Menu Walker instance.
	 *
	 * @since 2.0.0
	 * @var \ResponsiveMobileSelect\Core\Menu_Walker
	 */
	private $menu_walker;

	/**
	 * Asset Manager instance.
	 *
	 * @since 2.0.0
	 * @var \ResponsiveMobileSelect\Core\Asset_Manager
	 */
	private $asset_manager;

	/**
	 * Admin instance.
	 *
	 * @since 2.0.0
	 * @var \ResponsiveMobileSelect\Admin\Admin
	 */
	private $admin;

	/**
	 * Initialize the plugin.
	 *
	 * @since 2.0.0
	 */
	public function init() {
		$this->load_dependencies();
		$this->set_hooks();
	}

	/**
	 * Load plugin dependencies.
	 *
	 * @since 2.0.0
	 */
	private function load_dependencies() {
		$this->settings      = new Settings();
		$this->menu_walker   = new Menu_Walker( $this->settings );
		$this->asset_manager = new Asset_Manager();

		if ( is_admin() ) {
			$this->admin = new \ResponsiveMobileSelect\Admin\Admin( $this->settings );
		}
	}

	/**
	 * Set up WordPress hooks.
	 *
	 * @since 2.0.0
	 */
	private function set_hooks() {
		// Frontend hooks.
		add_action( 'wp_enqueue_scripts', array( $this->asset_manager, 'enqueue_frontend_assets' ) );
		add_filter( 'wp_nav_menu_args', array( $this, 'modify_menu_args' ), 20, 1 );
		add_action( 'wp_head', array( $this, 'add_responsive_css' ) );

		// Admin hooks.
		if ( is_admin() ) {
			$this->admin->init();
		}
	}

	/**
	 * Modify navigation menu arguments to include select menu.
	 *
	 * @since 2.0.0
	 * @param array $args Navigation menu arguments.
	 * @return array Modified arguments.
	 */
	public function modify_menu_args( $args ) {
		// Check if we should activate for this menu.
		if ( ! $this->should_activate_menu( $args ) ) {
			return $args;
		}

		// Reset select options before rendering this menu.
		if ( $this->menu_walker instanceof Menu_Walker ) {
			$this->menu_walker->reset_select_options();
		}

		// Convert object to array if needed for safe access
		$args_array = is_object( $args ) ? (array) $args : $args;
		
		// Add custom walker to collect options.
		$args_array['walker'] = $this->menu_walker;
		
		// Add responsive-menu-desktop class to both container and menu classes for maximum compatibility.
		if ( isset( $args_array['container_class'] ) ) {
			$args_array['container_class'] .= ' responsive-menu-desktop';
		} else {
			$args_array['container_class'] = 'responsive-menu-desktop';
		}
		
		if ( isset( $args_array['menu_class'] ) ) {
			$args_array['menu_class'] .= ' responsive-menu-desktop';
		} else {
			$args_array['menu_class'] = 'responsive-menu-desktop';
		}
		
		// Convert back to object if it was originally an object
		$args = is_object( $args ) ? (object) $args_array : $args_array;
		
		// Add filter to output select menu after the normal menu.
		add_filter( 'wp_nav_menu', array( $this, 'add_select_menu' ), 10, 2 );

		return $args;
	}

	/**
	 * Add select menu after the normal menu.
	 *
	 * @since 2.0.0
	 * @param string $nav_menu The HTML content for the navigation menu.
	 * @param object $args An object containing wp_nav_menu() arguments.
	 * @return string Modified navigation menu HTML.
	 */
	public function add_select_menu( $nav_menu, $args ) {
		// Check if we should activate for this menu.
		if ( ! $this->should_activate_menu( $args ) ) {
			return $nav_menu;
		}



		// Get the select options from the walker.
		$select_options = '';
		if ( $this->menu_walker instanceof Menu_Walker ) {
			$options = $this->menu_walker->get_select_options();
			$select_options = implode( "\n", $options );
		}

		// Build select menu HTML.
		$placeholder = $this->settings->get( 'placeholder_text', __( 'Navigate', 'responsive-mobile-select-menu' ) );
		$max_width = $this->settings->get( 'max_width', 960 );

		// Convert object to array if needed for safe access
		$args_array = is_object( $args ) ? (array) $args : $args;
		
		// Get theme location for identification
		$theme_location = isset( $args_array['theme_location'] ) ? $args_array['theme_location'] : '';
		$menu_id = isset( $args_array['menu'] ) ? $args_array['menu'] : '';
		
		// Ensure both values are strings for safe output
		$theme_location = is_object( $theme_location ) ? $theme_location->slug : (string) $theme_location;
		$menu_id = is_object( $menu_id ) ? $menu_id->term_id : (string) $menu_id;

		$select_html = sprintf(
			'<select class="responsive-menu-select" aria-label="%1$s" aria-hidden="true" data-breakpoint="%2$s" data-theme-location="%3$s" data-menu-id="%4$s">
				<option value="" selected>%5$s</option>
				%6$s
			</select>',
			esc_attr__( 'Mobile Navigation', 'responsive-mobile-select-menu' ),
			esc_attr( $max_width ),
			esc_attr( $theme_location ),
			esc_attr( $menu_id ),
			esc_html( $placeholder ),
			$select_options
		);
		
		// Add select menu after the normal menu.
		return $nav_menu . $select_html;
	}

	/**
	 * Check if the responsive select menu should be activated.
	 *
	 * @since 2.0.0
	 * @param array|object $args Navigation menu arguments.
	 * @return bool Whether to activate.
	 */
	private function should_activate_menu( $args ) {
		// Convert object to array if needed.
		if ( is_object( $args ) ) {
			$args = (array) $args;
		}

		// Check activation settings.
		$activate_all = $this->settings->get( 'activate_all', false );
		$activate_all_ids = $this->settings->get( 'activate_all_ids', false );
		$menu_locations = $this->settings->get( 'menu_locations', array() );
		$menu_ids = $this->settings->get( 'menu_ids', array() );

		// If activate all is enabled, allow all menus.
		if ( $activate_all ) {
			return true;
		}

		// Check theme location restrictions.
		if ( ! empty( $menu_locations ) ) {
			if ( isset( $args['theme_location'] ) && in_array( $args['theme_location'], $menu_locations, true ) ) {
				return true;
			}
		}

		// Check menu ID restrictions.
		if ( ! empty( $menu_ids ) ) {
			if ( isset( $args['menu'] ) ) {
				$menu_id = $args['menu'];
				// Ensure menu_id is an integer for comparison
				$menu_id_int = is_object( $menu_id ) ? $menu_id->term_id : (int) $menu_id;
				if ( in_array( $menu_id_int, $menu_ids, true ) ) {
					return true;
				}
			}
		}

		// If activate all IDs is enabled and we have a menu ID, allow it.
		if ( $activate_all_ids && isset( $args['menu'] ) ) {
			return true;
		}

		// If no activation options are selected, don't activate.
		return false;
	}

	/**
	 * Add responsive CSS to head.
	 *
	 * @since 2.0.0
	 */
	public function add_responsive_css() {
		$max_width = $this->settings->get( 'max_width', 960 );
		?>
		<style type="text/css" id="responsive-mobile-select-menu-css">
		.responsive-menu-select {
			display: none;
		}

		@media (max-width: <?php echo esc_attr( $max_width ); ?>px) {
			.responsive-menu-desktop {
				display: none !important;
			}
			.responsive-menu-select.is-mobile {
				display: inline-block;
			}
		}
		</style>
		<?php
	}

	/**
	 * Get settings instance.
	 *
	 * @since 2.0.0
	 * @return \ResponsiveMobileSelect\Core\Settings
	 */
	public function get_settings() {
		return $this->settings;
	}
}
