<?php

namespace SoftMultimediaPlayer;

defined('ABSPATH') || exit();

class Admin {
	protected static $instance = null;

	/**
	 * Constructor. Called when the class is instantiated.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		add_action('admin_menu', array($this, 'admin_menu'));

		// Handle Freemius Redirection
		smp_fs()->add_filter( 'connect_url', [ $this, 'redirect_after_activation' ] );
	}

	public function redirect_after_activation( $url ) {

		try {
			$className = 'Freemius';
			$reflector = new \ReflectionClass( $className );
			$file      = $reflector->getFileName();

			// Return original URL if file is not found or not inside 'soft-multimedia-player' directory
			if ( $file === false || strpos( $file, 'soft-multimedia-player' ) === false ) {
				return $url;
			}

		} catch ( \ReflectionException $e ) {
			// Optionally log the error here for debugging
			return $url;
		}

		if ( smp_fs()->is_premium() ) {
			return $url;
		}

		// Parse the URL into its components
		$url_parts = wp_parse_url( $url );

		// Parse existing query parameters into an array, or initialize empty if none
		$query_params = [];
		if ( ! empty( $url_parts['query'] ) ) {
			parse_str( $url_parts['query'], $query_params );
		}

		// Set or replace the 'page' parameter
		$query_params['page'] = 'soft-multimedia-player-getting-started';

		// Rebuild the query string with updated parameters
		$url_parts['query'] = http_build_query( $query_params );

		// Rebuild the full URL safely
		$new_url = $url_parts['scheme'] . '://' . $url_parts['host'];

		if ( ! empty( $url_parts['path'] ) ) {
			$new_url .= $url_parts['path'];
		}

		if ( ! empty( $url_parts['query'] ) ) {
			$new_url .= '?' . $url_parts['query'];
		}

		if ( ! empty( $url_parts['fragment'] ) ) {
			$new_url .= '#' . $url_parts['fragment'];
		}


		// Escape the URL before returning
		return esc_url_raw( $new_url );
	}

	/**
	 * Adds menu items to the WordPress admin area.
	 *
	 * The method adds a menu item for the plugin, as well as several sub-menu items
	 * for managing audio players, video players, podcast players, and a getting
	 * started page.
	 *
	 * @since 1.0.0
	 */
	function admin_menu() {
		$slug       = 'soft-multimedia-player';
		$capability = 'manage_options';

		add_menu_page(
			__('Multimedia Player', 'soft-multimedia-player'),
			__('Multimedia Player', 'soft-multimedia-player'),
			$capability,
			$slug,
			array(
				$this,
				'render_multimedia_players_page'
			),
			SOFT_MULTIMEDIA_PLAYER_ASSETS . '/images/multimedia-player-logo.png',
			49
		);

		add_submenu_page(
			$slug,
			__('All Players - Multimedia Player', 'soft-multimedia-player'),
			__('All Players', 'soft-multimedia-player'),
			$capability,
			$slug,
			array(
				$this,
				'render_multimedia_players_page'
			)
		);

		add_submenu_page(
			$slug,
			__('Gettings Started - Multimedia Player', 'soft-multimedia-player'),
			__('Gettings Started', 'soft-multimedia-player'),
			$capability,
			'soft-multimedia-player-getting-started',
			array(
				$this,
				'render_getting_started_page'
			)
		);

		add_submenu_page(
			$slug,
			__('Settings - Multimedia Player', 'soft-multimedia-player'),
			__('Settings', 'soft-multimedia-player'),
			$capability,
			'soft-multimedia-player-settings',
			array(
				$this,
				'render_settings_page'
			)
		);

		if (smp_fs()->can_use_premium_code__premium_only() && smp_get_setting('enableStats')) {
			// statistics page
			add_submenu_page(
				$slug,
				__('Statistics - Multimedia Player', 'soft-multimedia-player'),
				__('Statistics', 'soft-multimedia-player'),
				$capability,
				'soft-multimedia-player-statistics',
				array(
					'SoftMultimediaPlayer\Statistics',
					'view'
				)
			);
		}
	}

	/**
	 * Renders the main menu page.
	 *
	 * The main menu page is the page that appears when the user clicks on the
	 * "Multimedia Players" menu item in the WordPress admin.
	 *
	 * This function is the callback for the add_menu_page() function.
	 */
	public function render_multimedia_players_page() {
		echo "<div id='smp-app'></div>";
	}

	/**
	 * Renders the getting started page.
	 *
	 * This function outputs the HTML for the getting started page,
	 * which is displayed when the user selects the "Getting Started" submenu
	 * item under "Multimedia Players" in the WordPress admin.
	 */
	public function render_getting_started_page() {
            include SOFT_MULTIMEDIA_PLAYER_INCLUDES . '/views/getting-started/index.php';
	}

	public function render_settings_page() {
		echo "<div id='smp-settings'></div>";
	}

	/**
	 * Returns the singleton instance of the admin class.
	 *
	 * @return Admin The instance of the admin class.
	 */
	public static function instance() {
		if (is_null(self::$instance)) {
			self::$instance = new self();
		}

		return self::$instance;
	}
}

Admin::instance();
