<?php

namespace PWH_DCFH\App\Admin\Controllers;

if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('PWH_DCFH_Admin_Notice')) {
    class PWH_DCFH_Admin_Notice
    {
        private static $_instance;

        const NOTICE_FIELD = '_pwh_dcfh_temp_admin_notice';

        /**
         * Returns the singleton instance of the class.
         * @return self
         */
        public static function instance()
        {
            if (self::$_instance === null) {
                self::$_instance = new self();
            }

            return self::$_instance;
        }

        /**
         * Show Notice
         * @return void
         */
        public static function show_admin_notice()
        {
            $options = get_option(self::NOTICE_FIELD);
            if (!empty($options)) {
                foreach ($options as $type => $messages) {
                    $messages = array_filter($messages, 'strlen');
                    $messages = array_values($messages);
                    echo "<div class='notice $type is-dismissible'>"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                    if (count($messages) === 1) {
                        echo sprintf('<p>%s</p>', $messages[0]); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                    } elseif (count($messages) > 1) {
                        echo '<ul>';
                        foreach ($messages as $message) {
                            echo "<li>$message</li>"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                        }
                        echo '</ul>';
                    }
                    echo "</div>";
                }
                delete_option(self::NOTICE_FIELD);
            }
        }

        /**
         * Add Error Message
         *
         * @param string $message Error message to display
         *
         * @return void
         */
        public static function add_error_message($message)
        {
            self::save($message, 'notice-error');
        }

        /**
         * Add Warning Message
         *
         * @param string $message Warning message to display
         *
         * @return void
         */
        public static function add_warning_message($message)
        {
            self::save($message, 'notice-warning');
        }

        /**
         * Add Info Message
         *
         * @param string $message Information message to display
         *
         * @return void
         */
        public static function add_info_message($message)
        {
            self::save($message, 'notice-info');
        }

        /**
         * Add Success Message
         *
         * @param string $message Success message to display
         *
         * @return void
         */
        public static function add_success_message($message)
        {
            self::save($message, 'notice-success');
        }

        /**
         * Get Settings Errors
         * @return void
         */
        public static function get_settings_errors()
        {
            $alerts = get_settings_errors();
            if (!empty($alerts[0]['message'])) {
                $is_error = ($alerts[0]['type'] === 'error');
                $messages = explode(',', $alerts[0]['message']);
                if (!empty($messages)) {
                    foreach ($messages as $message) {
                        if ($is_error) {
                            self::add_error_message($message);
                        } else {
                            self::add_success_message($message);
                        }
                    }
                }
            }
        }

        /**
         * Save Notice
         *
         * @param string $message Notice message to save
         * @param string $notice_level Notice level (error, warning, info, success)
         *
         * @return void
         */
        protected static function save($message, $notice_level)
        {
            $options = get_option(self::NOTICE_FIELD);
            $options[$notice_level][] = $message;
            update_option(self::NOTICE_FIELD, $options, 'no');
        }
    }
}
