<?php

/**
 * Welcome Notice class for Transport Carrier theme.
 */
class Transport_Carrier_Welcome_Notice {

	/**
	** Constructor.
	*/
	public function __construct() {
		add_action( 'admin_notices', [ $this, 'transport_carrier_render_notice' ] );
		add_action( 'admin_enqueue_scripts', [ $this, 'transport_carrier_admin_enqueue_scripts' ], 5 );
		add_action( 'admin_enqueue_scripts', [ $this, 'transport_carrier_notice_enqueue_scripts' ], 5 );
		add_action( 'wp_ajax_transport_carrier_dismissed_handler', [ $this, 'transport_carrier_dismissed_handler' ] );
		add_action( 'switch_theme', [ $this, 'transport_carrier_reset_notices' ] );
		add_action( 'after_switch_theme', [ $this, 'transport_carrier_reset_notices' ] );
	}

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

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

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

	/**
	** Render Notice Content
	*/
	public function transport_carrier_render_notice_content() {
		$action = 'install-activate';
		$redirect_url = 'admin.php?page=transport-carrier-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!', 'transport-carrier' ); ?></h4>
				<h1><?php esc_html_e( 'Welcome to Transport Carrier WordPress Theme!', 'transport-carrier' ); ?></h1>
				<p>
					<?php esc_html_e( 'Design a modern logistics site with responsive shipment overviews, automated booking prompts, and clear delivery tracking sections.', 'transport-carrier' ); ?>
				</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 Transport Carrier', 'transport-carrier' ); ?></span>
					</a>
					<a href="<?php echo esc_url( TRANSPORT_CARRIER_THEME_BUNDLE_URL ); ?>" target="_blank" class="bundle-btn btn">
						<span class="demo-btn-txt"><?php esc_html_e( 'Get All Themes', 'transport-carrier' ); ?></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', 'transport-carrier' ); ?>" />
		</div>
		<?php
	}

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

	/**
	** Dismissed handler.
	*/
	public function transport_carrier_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 transport_carrier_notice_enqueue_scripts( $page ) {
		wp_enqueue_script( 'jquery' );

		ob_start();
		?>
		<script>
			jQuery(function($) {
				$( document ).on( 'click', '.transport-carrier-notice .notice-dismiss', function () {
					jQuery.post( 'ajax_url', {
						action: 'transport_carrier_dismissed_handler',
						notice: $( this ).closest( '.transport-carrier-notice' ).data( 'notice' ),
					});
					$( '.transport-carrier-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 transport_carrier_admin_enqueue_scripts( $page ) {
		wp_enqueue_style( 'transport-carrier-welcome-notice-css', get_template_directory_uri() . '/activation-notice/css/notice-bar.css' );
	}
}

new Transport_Carrier_Welcome_Notice();
