<?php
/**
 * Spacetree Admin
 *
 * @class    ST_Admin
 * @package  Spacetree\Admin
 * @version  1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * ST_Admin class.
 */
class ST_Admin {

	/**
	 * Constructor.
	 */
	public function __construct() {
		
		add_action( 'init', array( $this, 'includes' ) );
		//add_action( 'current_screen', array( $this, 'conditional_includes' ) );
		add_action( 'admin_init', array( $this, 'buffer' ), 1 );
		// Add body class for WP 5.3+ compatibility.
		add_filter( 'admin_body_class', array( $this, 'include_admin_body_class' ), 9999 );
		
		// Enqueue admin styles and scripts
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
		
		// Add footer scripts
		add_action( 'admin_footer', array( $this, 'admin_footer_scripts' ) );
	}

	/**
	 * Output buffering allows admin screens to make redirects later on.
	 */
	public function buffer() {
		ob_start();
	}

	/**
	 * Include any classes we need within admin.
	 */
	public function includes() {
		/**
		 * Class ST_Admin_Menus
		 */
		
		include_once __DIR__ . '/class-st-admin-menus.php';
	
	}

	/**
	 * Include admin files conditionally.
	 */
	public function conditional_includes() {}


	/**
	 * Include admin classes.
	 *
	 * @since 4.2.0
	 * @param string $classes Body classes string.
	 * @return string
	 */
	public function include_admin_body_class( $classes ) {
		if ( in_array( array( 'st-wp-version-gte-53', 'st-wp-version-gte-55' ), explode( ' ', $classes ), true ) ) {
			return $classes;
		}

		$raw_version   = get_bloginfo( 'version' );
		$version_parts = explode( '-', $raw_version );
		$version       = count( $version_parts ) > 1 ? $version_parts[0] : $raw_version;

		// Add WP 5.3+ compatibility class.
		if ( $raw_version && version_compare( $version, '5.3', '>=' ) ) {
			$classes .= ' st-wp-version-gte-53';
		}

		// Add WP 5.5+ compatibility class.
		if ( $raw_version && version_compare( $version, '5.5', '>=' ) ) {
			$classes .= ' st-wp-version-gte-55';
		}

		return $classes;
	}

	/**
	 * Enqueue admin styles
	 * 
	 * Moved from: spacetree/libs/proqyz/includes/admin/class-st-proqyz-admin.php
	 */
	public function enqueue_admin_styles() {
		// Check if we're on the support page
		if ( isset( $_GET['page'] ) && $_GET['page'] === 'st-support' ) {
			// Enqueue Bootstrap CSS for support page
			wp_enqueue_style( 
				'st-admin-bootstrap-css', 
				'https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.min.css', 
				array(), 
				'3.4.1' 
			);
		}
	}

	/**
	 * Enqueue admin scripts
	 * 
	 * Moved from: spacetree/libs/proqyz/includes/admin/class-st-proqyz-admin.php
	 */
	public function enqueue_admin_scripts() {
		// Check if we're on the support page
		if ( isset( $_GET['page'] ) && $_GET['page'] === 'st-support' ) {
			// Enqueue Bootstrap JS for support page
			wp_enqueue_script( 
				'st-admin-bootstrap-js', 
				'https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.min.js', 
				array( 'jquery' ), 
				'3.4.1',
				true 
			);
		}
	}

	/**
	 * Add footer scripts
	 * 
	 * Moved from: spacetree/libs/proqyz/includes/admin/class-st-proqyz-admin.php
	 */
	public function admin_footer_scripts() {
		// Only add Tawk.to script on support page
		if ( isset( $_GET['page'] ) && $_GET['page'] === 'st-support' ) {
			?>
			<!--Start of Tawk.to Script-->
			<script type="text/javascript">
				var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
				(function(){
				var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
				s1.async=true;
				s1.src='https://embed.tawk.to/66030865a0c6737bd1251ab2/1hptthjv5';
				s1.charset='UTF-8';
				s1.setAttribute('crossorigin','*');
				s0.parentNode.insertBefore(s1,s0);
				})();
			</script>
			<!--End of Tawk.to Script-->
			<?php
		}
	}
}

return new ST_Admin();
