<?php

namespace SugarCalendarBookings\Admin;

use SugarCalendarBookings\Support\Bootable;
use SugarCalendarBookings\Support\Helpers\Helpers;

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

/**
 * Admin menu.
 *
 * @since 1.0.0
 */
class AdminMenu implements Bootable {

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

		// Populate the menu.
		add_action( 'admin_menu', [ $this, 'add_menu_items' ] );

		// Add global admin menu styles for Lite version.
		if ( ! sugar_calendar_bookings()->is_pro() ) {
			add_action( 'admin_head', [ $this, 'output_menu_styles' ] );
		}
	}

	/**
	 * Output inline CSS for admin menu styling.
	 *
	 * This is output globally on all admin pages since the menu
	 * is visible everywhere but our main styles only load on SCB pages.
	 *
	 * @since 1.3.0
	 */
	public function output_menu_styles() {

		?>
		<style>
			#adminmenu #toplevel_page_sugar-calendar-bookings-calendar ul.wp-submenu-wrap {
				padding-bottom: 0;
				margin-bottom: 6px;
			}
			#adminmenu #toplevel_page_sugar-calendar-bookings-calendar ul li a[href*="Upgrade" i] {
				background-color: #008a20;
				color: #fff;
				font-weight: 500;
			}
		</style>
		<?php
	}

	/**
	 * Add admin area menu items.
	 *
	 * @since 1.0.0
	 * @since 1.2.0 Added the Employees menu item.
	 * @since 1.4.0 Dynamic capability and slug for employee access.
	 *
	 * @return void
	 */
	public function add_menu_items() {

		$is_admin       = current_user_can( 'manage_options' );
		$capability     = $is_admin ? 'manage_options' : 'scb_employee_access';
		$top_level_slug = $is_admin ? Area::SLUG . '-calendar' : Area::SLUG . '-settings';

		$admin = sugar_calendar_bookings()->get( Area::class );

		add_menu_page(
			esc_html__( 'Bookings', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			esc_html__( 'Bookings', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			$capability,
			$top_level_slug,
			[ $admin, 'display' ],
			'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTciIGhlaWdodD0iMTYiIHZpZXdCb3g9IjAgMCAxNyA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C43Mzc5QzEwLjczNzkgMi43MDY3MiAxMC43NzQ5IDIuNzA2NzIgMTAuNzc0OSAyLjc0MzY3QzEwLjc3NDkgMy4xNTAxNCAxMS4xMDc0IDMuNDgyNyAxMS41MTM5IDMuNDgyN0gxMS43MTcxQzEyLjEyMzYgMy40ODI3IDEyLjQ1NjIgMy4xNTAxNCAxMi40NTYyIDIuNzQzNjdWMi43MjUxOUMxMi40NTYyIDIuNzI1MTkgMTIuNDU2MiAyLjcyNTE5IDEyLjQ3NDYgMi43MjUxOUMxMy4zMjQ1IDIuNzI1MTkgMTMuOTg5NiAzLjQwODggMTMuOTg5NiA0LjI0MDIxVjUuMjU2MzdDMTMuOTg5NiA1LjI1NjM3IDEzLjkzNDIgNS4zODU3IDEzLjg3ODggNS4zODU3SDYuMTM3NDVDNS42NzU1NSA1LjM4NTcgNS4yODc1NiA1Ljc1NTIyIDUuMjg3NTYgNi4yMzU1OUM1LjI4NzU2IDYuNjk3NDggNS42NzU1NSA3LjA4NTQ3IDYuMTM3NDUgNy4wODU0N0gxMy4wNDc0QzEzLjMwNiA3LjA4NTQ3IDEzLjU0NjIgNy4xNzc4NSAxMy43MzEgNy4zNjI2MUMxMy45MTU3IDcuNTQ3MzcgMTQuMDA4MSA3Ljc4NzU1IDE0LjAwODEgOC4wMjc3M1YxMS44ODkySDEzLjk4OTZaIiBmaWxsPSJ3aGl0ZSIvPgo8L3N2Zz4K',
			50
		);

		add_submenu_page(
			$top_level_slug,
			esc_html__( 'Calendar', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			esc_html__( 'Calendar', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			'manage_options',
			Area::SLUG . '-calendar',
			[ $admin, 'display' ],
			10
		);

		add_submenu_page(
			$top_level_slug,
			esc_html__( 'Appointments', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			esc_html__( 'Appointments', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			'manage_options',
			Area::SLUG . '-appointments',
			[ $admin, 'display' ],
			20
		);

		add_submenu_page(
			$top_level_slug,
			esc_html__( 'Services', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			esc_html__( 'Services', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			'manage_options',
			Area::SLUG . '-services',
			[ $admin, 'display' ],
			30
		);

		add_submenu_page(
			$top_level_slug,
			esc_html__( 'Locations', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			esc_html__( 'Locations', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			'manage_options',
			Area::SLUG . '-locations',
			[ $admin, 'display' ],
			40
		);

		add_submenu_page(
			$top_level_slug,
			esc_html__( 'Customers', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			esc_html__( 'Customers', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			'manage_options',
			Area::SLUG . '-customers',
			[ $admin, 'display' ],
			50
		);

		add_submenu_page(
			$top_level_slug,
			esc_html__( 'Employees', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			esc_html__( 'Employees', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			'manage_options',
			Area::SLUG . '-employees',
			[ $admin, 'display' ],
			55
		);

		add_submenu_page(
			$top_level_slug,
			esc_html__( 'Payments', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			esc_html__( 'Payments', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			'manage_options',
			Area::SLUG . '-payments',
			[ $admin, 'display' ],
			60
		);

		add_submenu_page(
			$top_level_slug,
			esc_html__( 'Settings', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			esc_html__( 'Settings', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
			$capability,
			Area::SLUG . '-settings',
			[ $admin, 'display' ],
			70
		);

		// Add "Upgrade to Pro" link for Lite version only.
		if ( ! sugar_calendar_bookings()->is_pro() ) {
			add_submenu_page(
				$top_level_slug,
				esc_html__( 'Upgrade to Pro', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				esc_html__( 'Upgrade to Pro', 'sugar-calendar-bookings-scheduling-appointments-lite' ),
				'manage_options',
				Helpers::get_utm_url(
					'https://sugarcalendar.com/pricing/bookings/',
					[
						'source'  => 'WordPress',
						'medium'  => 'admin-menu',
						'content' => 'Upgrade to Pro',
					]
				),
				'',
				100
			);
		}
	}
}
