<?php

namespace Fab\Controller;

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

/**
 * Notice.
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Notice extends Base {

    /**
     * ID of the notice.
     *
     * @var string
     */
    public string $id = '';

    /**
     * Screens where the notice is non dismissible.
     *
     * @var string[]
     */
    public array $non_dismissible_by_screens = array();

    /**
     * Get the default data for the admin notice.
     *
     * @return array
     */
    public function get_default_data() {
        // Get the current screen.
        $screen = get_current_screen();

        // Prepare the data for the admin notice.
        return array(
            'id'          => $this->id,
            'title'       => '',
            'message'     => '',
            'dismissible' => ! in_array( $screen->id, $this->non_dismissible_by_screens, true ),
            'classes'     => 'px-4 py-3',
            'logo'        => FAB_PLUGIN_URL . '/assets/img/logo.gif',
            'loading'     => FAB_PLUGIN_URL . '/assets/img/loading.gif',
            'nonce'       => wp_create_nonce( 'wp_rest' ),
            'rest_api'    => array(
                'dismiss' => get_rest_url( null, 'fab/v1/notice/dismiss' ),
                'action'  => get_rest_url( null, 'fab/v1/notice/action' ),
            ),
            'buttons'     => array(),
        );
    }

    /**
     * Default filter for admin notice.
     *
     * @return bool
     */
    public function should_show_notice() {
        // Get the current screen.
        $screen = get_current_screen();

        // Only show messages to admins.
        if ( function_exists( 'current_user_can' )
            && ! current_user_can( 'manage_options' )
        ) {
            return false;
        }

        // Check if the admin notice been dismissed.
        $is_dismissed             = get_option( sprintf( 'fab_admin_notice_%s', $this->id ) );
        $supposed_to_be_dismissed = in_array( $screen->id, $this->non_dismissible_by_screens, true );
        if ( $is_dismissed && ! $supposed_to_be_dismissed ) {
            return false;
        }

        return true;
    }
}
