<?php
/**
 * Display Messages Class
 *
 * This class is used to display admin notices and messages in WordPress.
 * It provides different message types (NOTICE, ERROR, SUCCESS, CUSTOM_MESSAGE)
 * with appropriate styling and HTML structure.
 *
 * @package miniorange-sms-order-notification-otp-verification/helper
 * @since   1.0.0
 */

namespace WCSMSOTP\Helper;

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

/**
 * Display Messages Class
 *
 * This class is used to display admin notices and messages in WordPress.
 * It provides different message types (NOTICE, ERROR, SUCCESS, CUSTOM_MESSAGE)
 * with appropriate styling and HTML structure.
 */
if ( ! class_exists( 'MoDisplayMessages' ) ) {
	/**
	 * Display Messages Class
	 *
	 * This class is used to display admin notices and messages in WordPress.
	 * It provides different message types (NOTICE, ERROR, SUCCESS, CUSTOM_MESSAGE)
	 * with appropriate styling and HTML structure.
	 */
	class MoDisplayMessages {

		/**
		 * Message to display.
		 *
		 * @var string
		 */
		private $message;

		/**
		 * Type of message (NOTICE, ERROR, SUCCESS, CUSTOM_MESSAGE, etc.).
		 *
		 * @var string
		 */
		private $type;

		/**
		 * Constructor.
		 *
		 * Initializes the message and type, and hooks into admin_notices.
		 *
		 * @param string $message Message to show.
		 * @param string $type    Type of message to show (NOTICE, ERROR, SUCCESS, CUSTOM_MESSAGE, etc.).
		 */
		public function __construct( $message, $type ) {
			if ( ! is_string( $message ) ) {
				$message = '';
			}
			if ( ! is_string( $type ) ) {
				$type = 'CUSTOM_MESSAGE';
			}
			$this->message = sanitize_textarea_field( $message );
			$this->type    = sanitize_text_field( $type );

			add_action( 'admin_notices', array( $this, 'render' ) );
		}

		/**
		 * Render message.
		 *
		 * Outputs the message based on its type with appropriate HTML structure and styling.
		 *
		 * @return void
		 */
		public function render() {
			if ( empty( $this->message ) ) {
				return;
			}
			$message = $this->message;
			if ( function_exists( 'mowc_' ) ) {
				$message = mowc_( $message );
			}
			$message = is_string( $message ) ? $message : '';

			switch ( $this->type ) {
				case 'CUSTOM_MESSAGE':
					echo esc_html( $message );
					break;
				case 'NOTICE':
					printf(
						'<section style="%s" class="%s"><p>%s</p></section>',
						esc_attr( 'margin-top:1%;' ),
						esc_attr( 'is-dismissible mowc-alert error mo-admin-notif' ),
						wp_kses_post( $message )
					);
					break;
				case 'ERROR':
					printf(
						'<section style="%s" class="%s"><p>%s</p></section>',
						esc_attr( 'margin-top:1%;' ),
						esc_attr( 'mowc-alert mowc-error is-dismissible mo-admin-notif' ),
						wp_kses_post( $message )
					);
					break;
				case 'SUCCESS':
					printf(
						'<section style="%s" class="%s"><p>%s</p></section>',
						esc_attr( 'margin-top:1%;' ),
						esc_attr( 'notice mowc-success is-dismissible mo-admin-notif' ),
						wp_kses_post( $message )
					);
					break;
			}
		}

		/**
		 * Show message div for add-ons.
		 *
		 * Outputs message divs specifically for add-on messages with appropriate styling.
		 *
		 * @return void
		 */
		public function show_message_div_addons() {
			if ( empty( $this->message ) ) {
				return;
			}
			$message = $this->message;
			if ( function_exists( 'mowc_' ) ) {
				$message = mowc_( $message );
			}
			$message = is_string( $message ) ? $message : '';

			switch ( $this->type ) {
				case 'MO_ADDON_MESSAGE_CUSTOM_MESSAGE_SUCCESS':
					printf(
						'<div style="%s" class="%s"><p>%s</p></div>',
						esc_attr( 'margin-top:1%;' ),
						esc_attr( 'notice mowc-success is-dismissible mo-admin-notif' ),
						wp_kses_post( $message )
					);
					break;
				case 'MO_ADDON_MESSAGE_CUSTOM_MESSAGE_ERROR':
					printf(
						'<div style="%s" class="%s"><p>%s</p></div>',
						esc_attr( 'margin-top:1%;' ),
						esc_attr( 'mowc-alert mowc-error is-dismissible mo-admin-notif' ),
						wp_kses_post( $message )
					);
					break;
			}
		}
	}
}
