<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;
use Fab\View;

/**
 * Redirect Add New Fab Notice.
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Redirect_Add_New_Fab_Notice extends Notice implements JIT_Interface {

    /**
     * Constructor.
     *
     * @return void
     */
    public function __construct() {
        $this->id = 'fab_redirect_add_new_fab_notice';
    }

    /**
     * Metabox redirect.
     *
     * @return void
     */
    public function metabox_redirect() {
        if (
            isset( $_GET['post_type'], $_SERVER['SCRIPT_NAME'] ) &&
            'fab' === $_GET['post_type'] &&
            basename( sanitize_text_field( wp_unslash( $_SERVER['SCRIPT_NAME'] ) ) ) === 'post-new.php'
        ) {
            add_meta_box(
                'fab-metabox-redirect-add-new-fab',
                __( 'Redirect', 'floating-awesome-button' ),
                array( $this, 'fab_show_admin_notice' ),
                'fab',
                'side',
                'high'
            );

        }
    }

    /**
     * Show admin rating notice.
     *
     * @return void
     */
    public function fab_show_admin_notice() {
        // Redirect to the templates page.
        View::RenderStatic(
            'Backend.redirect-notice',
            array_merge(
                $this->get_default_data(),
                array(
                    'title'        => __( 'Warning! We are moving to a templates based workflow.', 'floating-awesome-button' ),
                    'message'      => __( 'We\'re moving to a templates based workflow. Please create a new FAB using a template. This page will be redirected shortly to the templates page.', 'floating-awesome-button' ),
                    'redirect_url' => admin_url( 'edit.php?post_type=fab&page=floating-awesome-button-templates' ),
                    'timeout'      => 30,
                    'buttons'      => array(
                        array(
                            'url'     => admin_url( 'edit.php?post_type=fab&page=floating-awesome-button-templates' ),
                            'message' => __( 'Create an Amazing FAB Now!', 'floating-awesome-button' ),
                            'classes' => 'text-center text-xs px-2.5 py-1.5 bg-danger-600 hover:bg-primary-600 border border-primary-600 text-white transition-all font-semibold animate__animated animate__pulse animate__infinite',
                        ),
                    ),
                ),
            )
        );
    }

    /*
    |--------------------------------------------------------------------------
    | Fulfill implemented interface contracts
    |--------------------------------------------------------------------------
     */

    /**
     * JIT method.
     *
     * @return void
     */
    public static function jit() {
        // Add metabox to Fab CPT.
        add_action( 'add_meta_boxes', array( __CLASS__, 'jit_metabox_redirect' ), 10, 0 );
    }
}
