<?php

namespace Fab\Controller;

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

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

/**
 * Usage Tracking Notice.
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Usage_Tracking_Notice extends Notice implements JIT_Interface {

    /**
     * Constructor.
     *
     * @return void
     */
    public function __construct() {
        $this->id                         = 'fab_usage_tracking';
        $this->non_dismissible_by_screens = array(
            'fab_page_floating-awesome-button-setting',
            'fab_page_floating-awesome-button-templates',
            'fab_page_fab-dashboard',
        );
    }

    /**
     * Show admin usage tracking notice.
     *
     * @return void
     */
    public function fab_show_admin_notice() {
        // Get the current screen.
        $screen = get_current_screen();

        // Check if the user has already allowed usage tracking.
        if ( get_option( 'fab_usage_tracking', 'no' ) === 'yes' ) {
            return;
        }

        // Check if the notice should be shown.
        if ( ! $this->should_show_notice() ) {
            return;
        }

        // Setup Buttons.
        $buttons   = array();
        $buttons[] = array(
            'message' => __( 'Allow tracking', 'floating-awesome-button' ),
            'action'  => 'fab_allow_usage_tracking',
            'classes' => 'text-white text-center text-xs px-2.5 py-1.5 bg-primary-600 hover:bg-primary-700 transition-all font-semibold shadow-md animate__animated animate__pulse animate__infinite animate__slow',
        );
        if ( ! in_array( $screen->id, $this->non_dismissible_by_screens, true ) ) {
            $buttons[] = array(
                'message' => __( 'Maybe later', 'floating-awesome-button' ),
                'action'  => 'fab_maybe_later_usage_tracking',
                'classes' => 'text-center text-xs px-2.5 py-1.5 bg-white hover:bg-red-600 border border-primary-600 hover:border-red-600 hover:text-red-600 hover:text-white transition-all font-semibold',
            );
        }

        // Prepare the data for the admin notice.
        $data = array_merge(
            $this->get_default_data(),
            array(
                'title'   => __( 'Allow FAB to track plugin usage?', 'floating-awesome-button' ),
                'message' => __( 'Help us improve your experience! By enabling usage tracking, we can understand how you use FAB and ensure compatibility with your specific WordPress setup, themes and plugins. This allows us to deliver better features and fixes tailored to your needs. Your data helps make FAB better for everyone!', 'floating-awesome-button' ),
                'buttons' => $buttons,
            )
        );

        View::RenderStatic( 'Backend.admin-notice', $data );
    }

    /**
     * Handle notice action.
     *
     * @param WP_REST_Request $request The request object.
     * @return void
     */
    public function fab_handle_notice_action( $request ) {
        if ( 'fab_allow_usage_tracking' === $request->get_param( 'action' ) ) {
            // Dismiss notice.
            update_option( sprintf( 'fab_admin_notice_%s', $this->id ), true );

            // Enable usage tracking.
            update_option( 'fab_usage_tracking', 'yes' );
        } elseif ( 'fab_maybe_later_usage_tracking' === $request->get_param( 'action' ) ) {
            // Dismiss notice.
            update_option( sprintf( 'fab_admin_notice_%s', $this->id ), true );
        }
    }

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

    /**
     * JIT method.
     *
     * @return void
     */
    public static function jit() {
        // Show admin usage tracking notice.
        add_action( 'admin_notices', array( __CLASS__, 'jit_fab_show_admin_notice' ) );

        // Handle notice action.
        add_action( 'fab_notice_action', array( __CLASS__, 'jit_fab_handle_notice_action' ) );
    }
}
