<?php

/**
 * Welcome Notice class for Fire Brigade theme.
 */
class Fire_Brigade_Welcome_Notice {

	/**
	** Constructor.
	*/
	public function __construct() {
		add_action( 'admin_notices', [ $this, 'fire_brigade_render_notice' ] );
		add_action( 'admin_enqueue_scripts', [ $this, 'fire_brigade_admin_enqueue_scripts' ], 5 );
		add_action( 'admin_enqueue_scripts', [ $this, 'fire_brigade_notice_enqueue_scripts' ], 5 );
		add_action( 'wp_ajax_fire_brigade_dismissed_handler', [ $this, 'fire_brigade_dismissed_handler' ] );
		add_action( 'switch_theme', [ $this, 'fire_brigade_reset_notices' ] );
		add_action( 'after_switch_theme', [ $this, 'fire_brigade_reset_notices' ] );
	}

	/**
	** Render Notice
	*/
	public function fire_brigade_render_notice() {
		$screen = get_current_screen();

		if (
			$screen &&
			$screen->id !== 'appearance_page_fire-brigade-theme-info' &&
			$screen->id !== 'appearance_page_fire-brigade-demo'
		) {
			$transient_name = sprintf( '%s_activation_notice', get_template() );

			if ( ! get_transient( $transient_name ) ) {
				?>
				<div class="fire-brigade-notice notice notice-info is-dismissible" data-notice="<?php echo esc_attr( $transient_name ); ?>">
					<button type="button" class="notice-dismiss"></button>
					<?php $this->fire_brigade_render_notice_content(); ?>
				</div>
				<?php
			}
		}
	}

	/**
	** Render Notice Content
	*/
	public function fire_brigade_render_notice_content() {
		$action = 'install-activate';
		$redirect_url = 'admin.php?page=fire-brigade-theme-info';

		?>
		<div class="notice-left-icon-box">
			<span class="dashicons dashicons-businessperson notc-theme-icon"></span>
		</div>
		<div class="welcome-message">
			<div class="notc-contnt">
				<h4><?php esc_html_e( 'Thank you for choosing Legacy Themes!', 'fire-brigade' ); ?></h4>
				<h1><?php esc_html_e( 'Welcome to Fire Brigade WordPress Theme!', 'fire-brigade' ); ?></h1>
				<p>
					<?php esc_html_e( 'Design a modern logistics site with responsive shipment overviews, automated booking prompts, and clear delivery tracking sections.', 'fire-brigade' ); ?>
				</p>
				<div class="action-buttons">
					<a href="<?php echo esc_url( admin_url( $redirect_url ) ); ?>" class="button notice-btn button-hero" data-action="<?php echo esc_attr( $action ); ?>">
						<span class="notc-btn-txt"><?php esc_html_e( 'Get Started with Fire Brigade', 'fire-brigade' ); ?></span>
					</a>
					<a href="<?php echo esc_url( FIRE_BRIGADE_THEME_BUNDLE_URL ); ?>" target="_blank" class="bundle-btn btn">
						<span class="demo-btn-txt"><?php esc_html_e( 'Get All Themes', 'fire-brigade' ); ?></span>
					</a>
				</div>
			</div>
		</div>
		<div class="notice-right-img-box">
			<img class="notc-right-img" src="<?php echo esc_url( get_template_directory_uri() . '/activation-notice/img/notice-right.png' ); ?>" alt="<?php esc_attr_e( 'notice themes img', 'fire-brigade' ); ?>" />
		</div>
		<?php
	}

	/**
	** Reset Notice.
	*/
	public function fire_brigade_reset_notices() {
		delete_transient( sprintf( '%s_activation_notice', get_template() ) );
	}

	/**
	** Dismissed handler.
	*/
	public function fire_brigade_dismissed_handler() {
		wp_verify_nonce( null );

		if ( isset( $_POST['notice'] ) ) {
			set_transient( sanitize_text_field( wp_unslash( $_POST['notice'] ) ), true, 0 );
		}
	}

	/**
	** Enqueue notice scripts.
	*/
	public function fire_brigade_notice_enqueue_scripts( $page ) {
		wp_enqueue_script( 'jquery' );

		ob_start();
		?>
		<script>
			jQuery(function($) {
				$( document ).on( 'click', '.fire-brigade-notice .notice-dismiss', function () {
					jQuery.post( 'ajax_url', {
						action: 'fire_brigade_dismissed_handler',
						notice: $( this ).closest( '.fire-brigade-notice' ).data( 'notice' ),
					});
					$( '.fire-brigade-notice' ).hide();
				} );
			});
		</script>
		<?php
		$script = str_replace( 'ajax_url', admin_url( 'admin-ajax.php' ), ob_get_clean() );

		wp_add_inline_script( 'jquery', str_replace( ['<script>', '</script>'], '', $script ) );
	}

	/**
	** Register notice styles.
	*/
	public function fire_brigade_admin_enqueue_scripts( $page ) {
		wp_enqueue_style( 'fire-brigade-welcome-notice-css', get_template_directory_uri() . '/activation-notice/css/notice-bar.css' );
	}
}

new Fire_Brigade_Welcome_Notice();
