<?php

namespace Inspire_Labs\Alsendo_Connect;

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


/**
 * Alerts
 */
class Alerts {

	/**
	 * $notice
	 *
	 * @var array
	 */
	public static $notice = array();

	/**
	 * $error
	 *
	 * @var array
	 */
	public static $error = array();

	/**
	 * $success
	 *
	 * @var array
	 */
	public static $success = array();

	/**
	 * $alerts_rendered
	 *
	 * @var bool
	 */
	public static $alerts_rendered = false;

	/**
	 * Alerts constructor.
	 */
	public function __construct() {
		$this->print_alerts_once();
	}


	/**
	 * Short description
	 *
	 * @param array|string $notice $notice.
	 */
	public function add_notice( $notice, $context = 'global' ) {
		self::$notice[ $context ][] = $notice;
	}

	/**
	 * Short description
	 *
	 * @param array|string $success $success.
	 * @param string       $context $context.
	 */
	public function add_success( $success, $context = 'global' ) {
		self::$success[ $context ][] = $success;
	}

	/**
	 * Short description
	 *
	 * @param array|string $error $error.
	 * @param string       $context $context.
	 */
	public function add_error( $error, $context = 'global' ) {
		self::$error[ $context ][] = $error;
	}

	/**
	 * Short description
	 */
	public function print_alerts_once() {
		if ( ! self::$alerts_rendered ) {
			add_action(
				'admin_notices',
				function () {
					$this->print_alerts();
					self::$alerts_rendered = true;
				}
			);
		}
	}

	/**
	 * Print alerts
	 *
	 * @return void
	 */
	public function print_alerts() {

		foreach ( array_keys( self::$success ) as $context ) {
			self::$success[ $context ] = array_unique( self::$success[ $context ] );

			foreach ( self::$success[ $context ] as $k => $v ) {
				if ( ! empty( $v ) ) {

					$output = sprintf(
						"<div class='notice notice-success'><p>%s</p></div>",
						$v
					);
					echo wp_kses(
						$output,
						array(
							'div' => array( 'class' => array() ),
							'p'   => array(),
							'b'   => array(),
						)
					);
				}
			}
		}

		foreach ( array_keys( self::$notice ) as $context ) {
			self::$notice[ $context ] = array_unique( self::$notice[ $context ] );

			foreach ( self::$notice[ $context ] as $k => $v ) {
				if ( ! empty( $v ) ) {

					$output = sprintf(
						"<div class='notice notice-info'><p>%s</p></div>",
						$v
					);
					echo wp_kses(
						$output,
						array(
							'div' => array( 'class' => array() ),
							'p'   => array(),
							'b'   => array(),
						)
					);
				}
			}
		}

		foreach ( array_keys( self::$error ) as $context ) {
			self::$error[ $context ] = array_unique( self::$error[ $context ] );

			foreach ( self::$error[ $context ] as $k => $v ) {
				if ( ! empty( $v ) ) {

					$output = sprintf(
						"<div class='notice notice-error error'><p>%s</p></div>",
						$v
					);
					echo wp_kses(
						$output,
						array(
							'div' => array( 'class' => array() ),
							'p'   => array(),
							'b'   => array(),
						)
					);
				}
			}
		}

		self::$error   = array();
		self::$success = array();
		self::$notice  = array();
	}


	/**
	 * Get alerts unformatted
	 *
	 * @return array
	 */
	public function get_alerts_unformatted(): array {
		$success = array();
		$notices = array();
		$errors  = array();

		foreach ( array_keys( self::$success ) as $context ) {
			$success[ $context ] = array_unique( self::$success[ $context ] );
		}

		foreach ( array_keys( self::$notice ) as $context ) {
			$notices[ $context ] = array_unique( self::$notice[ $context ] );
		}

		foreach ( array_keys( self::$error ) as $context ) {
			$errors[ $context ] = array_unique( self::$error[ $context ] );
		}

		return array(
			'success' => self::$success,
			'notice'  => self::$notice,
			'error'   => self::$error,
		);
	}

	/**
	 * Get alerts unformatted by context
	 *
	 * @param string $context $context.
	 *
	 * @return array
	 */
	public function get_alerts_unformatted_by_context( $context ): array {
		$ret = array();

		foreach ( $this->get_alerts_unformatted() as $type => $v ) {
			foreach ( $v as $context_in_loop => $messages ) {
				if ( $context === $context_in_loop ) {
					$ret[ $type ][] = $messages;
				}
			}
		}

		return $ret;
	}

	/**
	 * Clean errors
	 *
	 * @param string $context $context.
	 */
	public function clean_errors( $context = '' ) {
		self::$error = array();
	}
}
