<?php

namespace SugarCalendarBookings\Admin;

use Exception;
use SugarCalendarBookings\Options;
use SugarCalendarBookings\Plugin;
use SugarCalendarBookings\Support\Admin\Notices;
use SugarCalendarBookings\Support\Bootable;
use SugarCalendarBookings\Support\Helpers\Helpers;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Education.
 *
 * @since 1.0.0
 */
class Education implements Bootable {

	/**
	 * Register hooks.
	 *
	 * @since 1.0.0
	 */
	public function hooks() {

		add_action( 'admin_init', [ $this, 'register_notices' ] );
		add_action( 'sugar_calendar_bookings_admin_area_enqueue_assets', [ $this, 'enqueue_assets' ] );
	}

	/**
	 * Register notices.
	 *
	 * @since 1.0.0
	 * @since 1.2.0 Added the ability to exclude pages from showing the education notice.
	 * @since 1.2.1 Set education notice registration to only show on Calendar page.
	 *
	 * @return void
	 *
	 * @throws Exception If template is not found.
	 */
	public function register_notices() {

		$area = Plugin::get( Area::class );

		// Bail if not in a plugin screen or on an education page.
		if ( ! $area->is_calendar_page() ) {
			return;
		}

		// Bail if announcements are hidden via Misc settings.
		if ( Plugin::get( Options::class )->get( 'misc.hide_announcements' ) ) {
			return;
		}

		// New events education.
		Plugin::get( Notices::class )->add(
			Helpers::template( 'education/notice-new-events.php', [], true ),
			'',
			[
				'dismiss' => Notices::DISMISS_USER,
				'slug'    => 'education-new-events',
				'class'   => [
					'sugar-calendar-bookings-education-notice',
					'sugar-calendar-bookings-education-notice--new-events',
				],
			]
		);
	}

	/**
	 * Enqueue assets.
	 *
	 * @since 1.0.0
	 */
	public function enqueue_assets() {

		$assets   = Plugin::get( Assets::class );
		$manifest = $assets->get_asset_manifest( 'admin/education' );

		if ( $manifest !== false ) {
			wp_enqueue_style(
				'sugar-calendar-bookings-admin-education',
				$assets->get_src( 'admin/education.css' ),
				[],
				$assets->is_dev() ? $manifest['version'] : SCBOOKINGS_PLUGIN_VERSION
			);

			wp_enqueue_script(
				'sugar-calendar-bookings-admin-education',
				$assets->get_src( 'admin/education.js' ),
				$manifest['dependencies'],
				$assets->is_dev() ? $manifest['version'] : SCBOOKINGS_PLUGIN_VERSION,
				true
			);

			wp_set_script_translations(
				'sugar-calendar-bookings-admin-education',
				'sugar-calendar-bookings-scheduling-appointments-lite',
				SCBOOKINGS_PLUGIN_PATH . 'assets/languages'
			);
		}
	}
}
