<?php
/**
 * Admin page setup
 *
 * @package crewhrm
 */

namespace CrewHRM\Setup;

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


use CrewHRM\Helpers\Utilities;
use CrewHRM\Main;
use CrewHRM\Models\Application;
use CrewHRM\Models\Department;
use CrewHRM\Models\Mailer;
use CrewHRM\Models\Settings;
use CrewHRM\Models\User;

/**
 * The setup class
 */
class Admin {
	const SLUG_SETTINGS        = 'crewhrm-settings';
	const MOUNTPOINT_SETTINGS  = 'crewhrm_settings';
	const PAGE_SLUG_CALENDAR   = 'crewhrm-event-calendar';
	const PAGE_SLUG_ALL_JOBS   = 'crewhrm-all-jobs';
	const PAGE_SLUG_EMPLOYEE   = 'crewhrm-employees';

	/**
	 * Setup admin menus
	 *
	 * @return void
	 */
	public function __construct() {
		add_action( 'admin_menu', array( $this, 'registerMenu' ), 10 );
		add_action( 'admin_menu', array( $this, 'registerCalendar' ), 10 );
		add_filter( 'plugin_action_links_' . Main::$configs->basename, array( $this, 'pluginActionLinks' ) );
		add_action( 'admin_notices', array( $this, 'showCareersPageError' ) );
	}

	/**
	 * Register calendar promotion
	 *
	 * @return void
	 */
	public function registerCalendar() {

		// Do not register if pro plugin is active
		if ( Main::$configs->has_pro ) {
			return;
		}

		// Setting page
		add_submenu_page(
			Main::$configs->app_name,
			esc_html__( 'Event Calendar', 'hr-management' ),
			esc_html__( 'Event Calendar', 'hr-management' ),
			User::getAdminMenuRole( get_current_user_id() ),
			self::PAGE_SLUG_CALENDAR, // The slug must be same as the pro one, so the url will remain same.
			array( $this, 'calendarPage' )
		);
	}

	/**
	 * Contents for the addons page
	 */
	public function calendarPage() {
		echo '<div id="crewhrm_event_calendar_page_promotion"></div>';
	}

	/**
	 * Register admin menu pages
	 *
	 * @return void
	 */
	public function registerMenu() {
		$logo = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjU2IiBoZWlnaHQ9IjI1NiIgdmlld0JveD0iMCAwIDI1NiAyNTYiIGZpbGw9Im5vbmUiI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head',
				function() use ( $white_label ) {
					?>
					<style>
						.toplevel_page_crewhrm .wp-menu-image.svg {
							background-image: url(<?php echo esc_url( $white_label['app_logo'] ); ?>) !important;
						}
					</style>
					<?php
				}
			);
		}

		$admin_role = User::getAdminMenuRole( get_current_user_id() );

		// Main page
		add_menu_page(
			$white_label['app_label'],
			$white_label['app_label'],
			$admin_role,
			Main::$configs->app_name,
			array( $this, 'mainPage' ),
			$logo
		);
		add_submenu_page(
			Main::$configs->app_name,
			esc_html__( 'Dashboard', 'hr-management' ),
			esc_html__( 'Dashboard', 'hr-management' ),
			$admin_role,
			Main::$configs->app_name,
			array( $this, 'mainPage' )
		);

		// All Jobs
		add_submenu_page(
			Main::$configs->app_name,
			esc_html__( 'All Job Posts', 'hr-management' ),
			esc_html__( 'All Job Posts', 'hr-management' ),
			$admin_role,
			self::PAGE_SLUG_ALL_JOBS,
			array( $this, 'allJobsPage' )
		);

		// Employee profiles
		add_submenu_page(
			Main::$configs->app_name,
			esc_html__( 'Employees', 'hr-management' ),
			esc_html__( 'Employees', 'hr-management' ),
			$admin_role,
			self::PAGE_SLUG_EMPLOYEE,
			array( $this, 'employeePage' )
		);

		// Setting page
		add_submenu_page(
			Main::$configs->app_name,
			esc_html__( 'Settings', 'hr-management' ),
			esc_html__( 'Settings', 'hr-management' ),
			$admin_role,
			self::SLUG_SETTINGS,
			array( $this, 'settingPage' )
		);
	}

	/**
	 * Main page content
	 *
	 * @return void
	 */
	public function mainPage() {
		$application_overview = Application::getApplicationStats();
		echo '<div 
				id="crewhrm_dashboard" 
				data-application-stats="' . esc_attr( wp_json_encode( $application_overview ) ) . '"></div>';
	}

	/**
	 * Provide HTML content for 'all jobs' page
	 *
	 * @return void
	 */
	public function allJobsPage() {
		echo '<div id="crewhrm_dashboard_all_jobs"></div>';
	}

	/**
	 * Provide HTML content for 'employees' page
	 *
	 * @return void
	 */
	public function employeePage() {
		$departments = Department::getDepartments();
		echo '<div 
				id="crewhrm_employees_dashboard" 
				data-departments="' . esc_attr( wp_json_encode( $departments ) ) . '"></div>';
	}

	/**
	 * Setting page content
	 *
	 * @return void
	 */
	public function settingPage() {

		$mail_templates = Mailer::getMailTemplates();

		// Add promotional email lists from prebuilt JSON
		if ( ! Main::$configs->has_pro ) {
			$json_path = Main::$configs->dir . 'dist/libraries/pro/mail-templates.json';
			$pro_mails = json_decode( file_get_contents( $json_path, true ) );

			foreach ( $pro_mails as $mail ) {
				$mail->locked                = true;
				$mail->tooltip               = esc_html__( 'Need to upgrade to Pro', 'hr-management' );
				$mail_templates[ $mail->id ] = (array) $mail;
			}
		}

		// Prepare the overall mail templates array
		$mail_templates = array_filter(
			$mail_templates,
			function( $template ) {
				return ! ( $template['exclude_from_settings'] ?? false );
			}
		);
		$mail_templates = array_values( $mail_templates );

		// Build final resource
		$resources = array(
			'pages'         => Utilities::getPageList( -1 ),
			'email_events'  => $mail_templates,
			'business_type' => Settings::getBusinessTypesDropdown(),
		);

		echo '<div 
			id="' . esc_attr( self::MOUNTPOINT_SETTINGS ) . '" 
			data-settings="' . esc_attr( wp_json_encode( (object) Settings::getSettings() ) ) . '"
			data-resources="' . esc_attr( wp_json_encode( $resources ) ) . '"></div>';
	}

	/**
	 * Add plugin action links in plugins page
	 *
	 * @param array $actions Plugin action links
	 * @return array
	 */
	public function pluginActionLinks( array $actions ) {

		$_actions = array(
			'crewhrm_dashboard' => '<a href="admin.php?page=' . Main::$configs->app_name . '">' . esc_html__( 'Dashboard', 'hr-management' ) . '</a>',
			'crewhrm_pro_link'  => '<a href="https://getcrewhrm.com/pricing/" target="_blank">
										<span style="color: #ff7742; font-weight: bold;">' .
											esc_html__( 'Get Pro', 'hr-management' ) .
										'</span>
									</a>',
		);

		$_actions = apply_filters( 'crewhrm_plugin_action_menus', $_actions );

		return array_merge( $actions, $_actions );
	}

	/**
	 * Show error notice if careers page is not setup
	 *
	 * @return void
	 */
	public function showCareersPageError() {
		$page_id = (int) Settings::getSetting( 'careers_page_id' );
		if ( ! empty( $page_id ) ) {
			return;
		}

		$link = admin_url( 'admin.php?page=' . self::SLUG_SETTINGS . '&highlight=careers_page_id#/settings/recruitment/careers/' );
		?>
		<div class="notice notice-warning">
			<p>
				<?php
					// translators: Careers page setup warning
					echo sprintf( esc_html__( 'Please %1$sset up%2$s a page to display the job posts.', 'hr-management' ), '<a href="' . esc_url( $link ) . '">', '</a>' );
				?>
			</p>
		</div>
		<?php
	}
}
