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

class Monocle_Admin {
	private static $instance = null;

	private static $dir = MONOCLE_DIR . 'admin/';
	private static $url = MONOCLE_URL . 'admin/';

	public static function get_instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	private function __construct() {
		$this->include_files();

		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );

		add_action( 'admin_notices', array( $this, 'welcome_notice' ) );

		add_action( 'admin_menu', array( $this, 'register_theme_menu' ) );
	}

	private function include_files() {
		require_once self::$dir . 'includes/class-admin-ajax.php';
		\Monocle_Admin_Ajax::get_instance();
	}

	public function enqueue_admin_assets() {
		if ( ! function_exists( 'get_current_screen' ) ) {
			return;
		}

		$saaslauncher_notice_current_screen = get_current_screen();

		$allowed_pages = array(
			'dashboard',
			'themes',
		);

		if ( ( isset( $_GET['page'] ) && ! empty( $_GET['page'] ) && 'about-monocle' === $_GET['page'] ) || in_array( $saaslauncher_notice_current_screen->id, $allowed_pages, true ) ) {
			wp_enqueue_style( 'monocle-admin-style', self::$url . 'css/admin-style.css', array(), MONOCLE_VERSION, 'all' );

			wp_enqueue_script( 'monocle-admin-scripts', self::$url . 'js/admin-scripts.js', array( 'jquery' ), MONOCLE_VERSION, true );
			wp_localize_script(
				'monocle-admin-scripts',
				'ajaxObj',
				array(
					'ajaxURL'      => admin_url( 'admin-ajax.php' ),
					'welcomeNonce' => wp_create_nonce( 'monocle_welcome_nonce' ),
					'redirectURL'  => admin_url( 'themes.php?page=about-monocle' ),
				)
			);
		}
	}

	public function welcome_notice() {
		$current_screen  = get_current_screen();
		$allowed_screens = array( 'dashboard', 'themes' );
		if ( ! in_array( $current_screen->id, $allowed_screens, true ) || is_network_admin() || ! current_user_can( 'manage_options' ) || get_option( 'monocle_dismissed_custom_notice' ) ) {
			return;
		}
		?>
		<div class="monocle-admin-notice notice notice-info is-dismissible content-install-plugin theme-info-notice" id="monocle-welcome-notice">
			<div class="content-holder">
				<div class="notices">
					<figure class="brand-logo">
						<img width="44" height="44" src="<?php echo esc_url( self::$url . 'images/cozy-addons-icon.png' ); ?>" alt="Cozy Blocks logo" />
					</figure>

					<div>
						<p class="highlighted-text"><?php esc_html_e( 'Welcome to Monocle! 🎉', 'monocle' ); ?></p>
						<h2 class="notice-heading"><?php esc_html_e( 'Supercharge Your Website with Cozy Blocks', 'monocle' ); ?></h2>
	
						<p><?php esc_html_e( 'Build faster and launch sooner with Cozy Blocks—powerful blocks, ready-made sections, and starter templates help you create stunning websites without starting from scratch.', 'monocle' ); ?></p>
	
						<div class="notice-buttons">
							<?php
							if ( is_plugin_active( 'cozy-addons/cozy-addons.php' ) ) {
								?>
								<button class="notice-button">
									<a href="<?php echo esc_url( admin_url( 'site-editor.php' ) ); ?>"><?php esc_html_e( 'Customize', 'monocle' ); ?></a>
								</button>
								<button class="notice-button notice-button-secondary">
									<a href="<?php echo esc_url( admin_url( 'themes.php?page=about-monocle' ) ); ?>"><?php esc_html_e( 'Getting Started', 'monocle' ); ?></a>
								</button>
								<?php
							} else {
								?>
								<button class="cozy-addons-install notice-button has-spinner">
									<a href="#"><?php esc_html_e( 'Install Cozy Blocks →', 'monocle' ); ?></a>
									<span class="spinner monocle-display-none" id="monocle-admin-spinner"></span>
								</button>
								<?php
							}
							?>
						</div>
					</div>
				</div>
				<figure class="notice-image">
					<img src="<?php echo esc_url( self::$url . 'images/theme_screen_img.png' ); ?>" />
				</figure>
			</div>
		</div>
		<?php
	}

	public function register_theme_menu() {
		add_theme_page(
			esc_html__( 'About Monocle', 'monocle' ),
			esc_html__( 'About Monocle', 'monocle' ),
			'edit_theme_options',
			'about-monocle',
			array( $this, 'render_admin_dashboard' )
		);
	}

	public function render_admin_dashboard() {
		require_once self::$dir . 'index.php';
	}
}
