<?php
declare( strict_types=1 );

namespace FiboFilters\WP;

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

/**
 * WordPress notice
 */
class Notice {

	const TYPE_ERROR   = 'error';
	const TYPE_WARNING = 'warning';
	const TYPE_SUCCESS = 'success';
	const TYPE_INFO    = 'info';

	/** @var string */
	private $type;
	/** @var string */
	private $message;
	/** @var string */
	private $classes;
	/** @var bool */
	private $render_in_paragraph;

	/**
	 * Notice class
	 *
	 * @param string $type Type of the notice.
	 * @param string $message Content of the notice.
	 * @param string $classes Notice CSS classes.
	 * @param bool   $render_in_paragraph Wrap the notification with a paragraph.
	 */
	public function __construct( string $type, string $message, string $classes = '', bool $render_in_paragraph = true ) {
		$this->type                = $type;
		$this->message             = $message;
		$this->classes             = $classes;
		$this->render_in_paragraph = $render_in_paragraph;
	}

	public function get_message(): string {
		return $this->message;
	}

	/**
	 * Display error notice
	 *
	 * @param string $message Content of the notice.
	 * @param string $classes Notice CSS classes.
	 * @param bool   $render_in_paragraph Wrap the notification with a paragraph.
	 *
	 * @return Notice
	 */
	public static function display_error( string $message, string $classes = '', bool $render_in_paragraph = true ): Notice {
		return self::create_notice( self::TYPE_ERROR, $message, $classes, $render_in_paragraph );
	}

	/**
	 * Display warning notice
	 *
	 * @param string $message Content of the notice.
	 * @param string $classes Notice CSS classes.
	 * @param bool   $render_in_paragraph Wrap the notification with a paragraph.
	 *
	 * @return Notice
	 */
	public static function display_warning( string $message, string $classes = '', bool $render_in_paragraph = true ): Notice {
		return self::create_notice( self::TYPE_WARNING, $message, $classes, $render_in_paragraph );
	}

	/**
	 * Display success notice
	 *
	 * @param string $message Content of the notice.
	 * @param string $classes Notice CSS classes.
	 * @param bool   $render_in_paragraph Wrap the notification with a paragraph.
	 *
	 * @return Notice
	 */
	public static function display_success( string $message, string $classes = '', bool $render_in_paragraph = true ): Notice {
		return self::create_notice( self::TYPE_SUCCESS, $message, $classes, $render_in_paragraph );
	}

	/**
	 * Display info notice
	 *
	 * @param string $message Content of the notice.
	 * @param string $classes Notice CSS classes.
	 * @param bool   $render_in_paragraph Wrap the notification with a paragraph.
	 *
	 * @return Notice
	 */
	public static function display_info( string $message, string $classes = '', bool $render_in_paragraph = true ): Notice {
		return self::create_notice( self::TYPE_INFO, $message, $classes, $render_in_paragraph );
	}

	/**
	 * Display notice of selected type
	 *
	 * @param string $type Type of the notice.
	 * @param string $message Content of the notice.
	 * @param string $classes Notice CSS classes.
	 * @param bool   $render_in_paragraph Wrap the notification with a paragraph.
	 *
	 * @return Notice
	 */
	protected static function create_notice( string $type, string $message, string $classes, bool $render_in_paragraph ): Notice {
		$notice = new Notice( $type, $message, $classes, $render_in_paragraph );
		add_action( 'admin_notices', [ $notice, 'display_notice' ] );

		return $notice;
	}

	public function display_notice(): void {
		$class   = sprintf( 'notice notice-%s fibofilters-notice %s', $this->type, $this->classes );
		$message = nl2br( $this->message );

		if ( $this->render_in_paragraph ) {
			echo wp_kses_post(
				sprintf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message )
			);

		} else {
			echo wp_kses_post(
				sprintf( '<div class="%1$s">%2$s</div>', $class, $message )
			);
		}
	}
}
