<?php

namespace Fab\Controller;

use Fab\Interfaces\JIT_Interface;

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

/**
 * Upsell Controller.
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Upsell extends Base implements JIT_Interface {

    /**
     * Enqueue upsell popup
     * - Upsell popup is upsell method we use when user click specific DOM element with specific #ID then it will show upsell popup.
     *
     * NOTE: THIS METHOD IS NOT USED, BUT WILL BE USED IN THE FUTURE.
     *
     * @param   array $hook_suffix     The current admin page.
     * @return  void
     */
    public function backend_enequeue_upsell_popup( $hook_suffix ) {
        global $post;
        $screen      = \FAB()->Helper->getScreen();
        $allowedPage = array( 'post.php', 'post-new.php' );
        if ( ! isset( $post->post_type ) || $post->post_type !== 'fab' || ! in_array( $screen->pagenow, $allowedPage ) ) {
            return;
        }

        // Add metaboxes / container for upsell.
        $metabox_ids = apply_filters( 'fab_upsell_metabox_ids', array() );

        /** Add Inline Script */
        wp_localize_script(
            'fab-local',
            'FAB_METABOX_UPSELL',
            array(
                'upsells' => $metabox_ids,
                'logo'    => json_decode( FAB_PATH )->plugin_url . '/assets/img/icon.png',
                'title'   => __( 'Upgrade To Get Floating Awesome Button Premium Features', 'floating-awesome-button' ),
                'content' => sprintf(
                    '<p>%s</p><p>%s</p><p>%s</p>',
                    __( 'Upgrade to our Premium version to gain full control over when and where your Floating Awesome Button appears.', 'floating-awesome-button' ),
                    __( 'You can create rules based to precisely target where your button appears. Additionally, choose from advanced triggers like Adblock, Exit Intent, Time Delay, or customize your own trigger behavior.', 'floating-awesome-button' ),
                    __( 'Upgrade today to enhance engagement and maximize conversions with targeted button placement!', 'floating-awesome-button' )
                ),
                'button'  => '<a target="_blank" href="' . esc_url( \FAB()->Helper->getUpgradeURL() ) . '" class="button-class">' . __( 'Upgrade Now →', 'floating-awesome-button' ) . '</a>',
            )
        );

        \FAB()->Helper->wp_enqueue_script( 'upsell-component', 'build/components/upsell/bundle.js', array(), FAB_VERSION, true );
    }

    /*
    |--------------------------------------------------------------------------
    | Upsell WoCommerce Integration
    |--------------------------------------------------------------------------
     */

    /**
     * Add metaboxes setting tabs upsell.
     *
     * @param array $tabs Existing tabs passed through the filter.
     * @return array Modified tabs with the new WooCommerce settings type.
     */
    public function add_fab_upsell_metabox_setting_tabs( $tabs ) {
        $tabs['trigger'] = array(
            'label'  => __( 'Trigger (Premium)', 'floating-awesome-button' ),
            'url'    => \FAB()->Helper->getUpgradeURL(),
            'fields' => array(),
        );

        return $tabs;
    }

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

    /**
     * JIT method.
     *
     * @return void
     */
    public static function jit() {
        if ( \FAB()->Helper->isPremiumPlan() ) {
            return;
        }

        // Enqueue backend upsell popup.
        // NOTE: THIS METHOD IS NOT USED, BUT WILL BE USED IN THE FUTURE.
        // add_action( 'admin_enqueue_scripts', array( __CLASS__, 'jit_backend_enequeue_upsell_popup' ) );

        // Add metaboxes setting tabs upsell.
        add_filter( 'fab_metabox_setting_tabs', array( __CLASS__, 'jit_add_fab_upsell_metabox_setting_tabs' ) );
    }
}
