<?php
/**
 * Admin Notices Helper.
 *
 * @package automation-web-platform
 */

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

/**
 * Class WAWP_Admin_Notices
 */
class WAWP_Admin_Notices {

	/**
	 * Check if WhatsApp is offline.
	 *
	 * @param array|null $wawp_instances Instances list.
	 * @return bool
	 */
	public static function is_wa_offline( $wawp_instances = null ): bool {
		if ( ! WAWP_Connector()->is_whatsapp_active() ) {
			return false;
		}

		if ( ! empty( $wawp_instances ) ) {
			return false;
		}

		$online = WAWP()->instance_manager->get_online_instances_cached();
		return empty( $online );
	}

	/**
	 * Require an online instance.
	 *
	 * @param array  $wawp_instances Instances list.
	 * @param string $title          Notice title.
	 * @return bool
	 */
	public static function require_online_instance( $wawp_instances, string $title = 'No online instance (Whatsapp Number)' ): bool {
		if ( isset( $_GET['wawp_dismiss_wa_offline'], $_GET['_wpnonce'] )
			&& '1' === sanitize_text_field( wp_unslash( $_GET['wawp_dismiss_wa_offline'] ) )
			&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'wawp_dismiss_wa_offline' )
		) {
			update_user_meta( get_current_user_id(), 'wawp_dismiss_wa_offline_notice', true );
		}

		if ( ! self::is_wa_offline( $wawp_instances ) ) {
			// Clear dismissal if instance is reconnected? No, keep it simple. It early returns anyway.
			return true;
		}

		if ( get_user_meta( get_current_user_id(), 'wawp_dismiss_wa_offline_notice', true ) ) {
			return true;
		}

		self::render_no_instance_notice( $title );
		return false;
	}

	/**
	 * Render "no instance" notice.
	 *
	 * @param string $title Notice title.
	 */
	private static function render_no_instance_notice( string $title ): void {
		$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
		$current_url = add_query_arg( array(), $request_uri );
		$dismiss_url = wp_nonce_url( add_query_arg( 'wawp_dismiss_wa_offline', '1', $current_url ), 'wawp_dismiss_wa_offline' );

		echo '<div class="wrap">
            <h1><i class="ri-lock-line"></i> ' . esc_html( $title ) . '</h1>
            <div class="nosender-box wawp-s-f01aac">
                <p><strong>' . esc_html__( 'You need to check your Whatsapp number', 'automation-web-platform' ) . '</strong></p>
                <p>' . esc_html__( 'No instance is online! Please connect an instance before using these features.', 'automation-web-platform' ) . '</p>
                <p class="wawp-flex wawp-gap-16">
                    <a href="' . esc_url( admin_url( 'admin.php?page=wawp&wawp_section=whatsapp-web-sender#tab-wa' ) ) . '" class="wawp-btn primary">
                        <i class="ri-settings-3-line"></i> ' . esc_html__( 'Add New Instance', 'automation-web-platform' ) . '
                    </a>
                    <a href="' . esc_url( $dismiss_url ) . '" class="wawp-btn outline">
                        <i class="ri-check-line"></i> ' . esc_html__( 'I understand that', 'automation-web-platform' ) . '
                    </a>
                </p>
            </div>
        </div>';
	}

	/**
	 * Require at least one active sender.
	 *
	 * @param string $title Notice title.
	 * @return bool
	 */
	public static function require_active_sender( string $title = 'Feature Unavailable' ): bool {
		if ( isset( $_GET['wawp_dismiss_sender_notice'], $_GET['_wpnonce'] )
			&& '1' === sanitize_text_field( wp_unslash( $_GET['wawp_dismiss_sender_notice'] ) )
			&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'wawp_dismiss_sender_notice' )
		) {
			update_user_meta( get_current_user_id(), 'wawp_dismiss_sender_notice', true );
		}

		$connector             = WAWP_Connector();
		$is_any_sender_enabled = $connector->is_whatsapp_active() || $connector->is_meta_active() || $connector->is_email_active() || $connector->is_firebase_active();

		if ( $is_any_sender_enabled || get_user_meta( get_current_user_id(), 'wawp_dismiss_sender_notice', true ) ) {
			return true;
		}

		self::render_no_sender_notice( $title );
		return false;
	}

	/**
	 * Render "no sender" notice.
	 *
	 * @param string $title Notice title.
	 */
	private static function render_no_sender_notice( string $title ): void {
		$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
		$current_url = add_query_arg( array(), $request_uri );
		$dismiss_url = wp_nonce_url( add_query_arg( 'wawp_dismiss_sender_notice', '1', $current_url ), 'wawp_dismiss_sender_notice' );

		echo '<div class="wrap">
            <h1><i class="ri-toggle-line"></i> ' . esc_html( $title ) . '</h1>
            <div class="nosender-box wawp-s-f01aac">
                <p><strong>' . esc_html__( 'All Senders Disabled', 'automation-web-platform' ) . '</strong></p>
                <p>' . esc_html__( 'To use this feature, please enable at least one sender (Email, WhatsApp OR SMS/).', 'automation-web-platform' ) . '</p>
                <p class="wawp-flex wawp-gap-16">
                    <a href="' . esc_url( admin_url( 'admin.php?page=wawp&wawp_section=senders' ) ) . '" class="wawp-btn primary">
                        <i class="ri-settings-3-line"></i> ' . esc_html__( 'Go to Sender Settings', 'automation-web-platform' ) . '
                    </a>
                    <a href="' . esc_url( $dismiss_url ) . '" class="wawp-btn outline">
                        <i class="ri-check-line"></i> ' . esc_html__( 'I understand that', 'automation-web-platform' ) . '
                    </a>
                </p>
            </div>
        </div>';
	}
}
