<?php
/**
 * Top-level DrezSync admin menu.
 *
 * DrezSync is a Gravity Forms add-on, so its settings normally live under
 * Forms and it has no top-level page of its own. Freemius, however, attaches
 * its account and upgrade screens to the plugin's own menu. Without a real
 * page to attach to, Freemius sends the post-activation redirect to a page
 * that does not exist ("Sorry, you are not allowed to access this page").
 *
 * This registers a genuine top-level "DrezSync" page so Freemius has a solid
 * home for its account and upgrade items. The page shows the Google Calendar
 * connection when Gravity Forms is active. It is present in both the free and
 * Pro builds, since Freemius needs it in both.
 *
 * @package DrezSync
 */

defined( 'ABSPATH' ) || exit;

/**
 * Registers and renders the top-level DrezSync admin page.
 */
class DrezSync_Admin_Menu {

	/**
	 * Hook the menu. Default priority so it registers before Freemius, which
	 * attaches its own items on admin_menu at the lowest priority (runs last).
	 */
	public static function init() {
		add_action( 'admin_menu', array( __CLASS__, 'register' ) );
	}

	/**
	 * Register the top-level menu page.
	 */
	public static function register() {
		add_menu_page(
			'DrezSync',
			'DrezSync',
			'manage_options',
			'drezsync',
			array( __CLASS__, 'render' ),
			'dashicons-calendar-alt',
			58
		);
	}

	/**
	 * Render the page: the Google connection when Gravity Forms is active,
	 * otherwise a note that Gravity Forms is required.
	 */
	public static function render() {
		echo '<div class="wrap">';
		echo '<h1>DrezSync</h1>';

		if ( class_exists( 'GFForms' ) && class_exists( 'DrezSync' ) && isset( DrezSync::instance()->connect ) ) {
			echo '<h2>' . esc_html__( 'Google Calendar', 'drezsync' ) . '</h2>';
			DrezSync::instance()->connect->render();
		} else {
			echo '<p>' . esc_html__( 'DrezSync needs Gravity Forms installed and active to run.', 'drezsync' ) . '</p>';
		}

		echo '</div>';
	}
}

DrezSync_Admin_Menu::init();
