<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;
use Fab\View;
use Fab\Helper\FABItem;
use Fab\Metabox\FABMetaboxLocation;

/**
 * Metabox Location.
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Metabox_Location extends Base implements JIT_Interface {

    /**
     * Register metabox location on custom post type Fab
     * - This is placed here to add provide upsell for free user.
     *
     * @return      void
     */
    public function metabox_location() {
        add_meta_box(
            'fab-metabox-location',
            __( 'Location (Premium)', 'floating-awesome-button' ),
            array( $this, 'metabox_location_callback_free' ),
            'fab',
            'normal',
            'default',
            array( 'is_display' => false )
        );
    }

    /**
     * Metabox Location callback for free user.
     *
     * @return      void
     */
    public function metabox_location_callback_free() {
        // Render View.
        View::RenderStatic(
            'Backend.Metabox.location-free',
            array(
                'plugin_url'     => FAB_PLUGIN_URL,
                'upgrade_url'    => \FAB()->Helper->getUpgradeURL(),
                'location_rules' => array(
                    array(
                        'title'       => __( 'Page & Post Rules', 'floating-awesome-button' ),
                        'description' => __( 'Show or hide buttons on specific pages, posts, products and other content. Target individual URLs to display your call-to-action exactly where you want it, whether it\'s your homepage, contact page, a particular blog post, or product page.', 'floating-awesome-button' ),
                        'link'        => \FAB()->Helper->get_documentation( 12487 ),
                    ),
                    array(
                        'title'       => __( 'Post Type Rules', 'floating-awesome-button' ),
                        'description' => __( 'Transform your website engagement by displaying targeted buttons on specific post types. Whether it\'s blog posts, product pages, or custom content - deliver the perfect call-to-action exactly where your visitors need it most!', 'floating-awesome-button' ),
                        'link'        => \FAB()->Helper->get_documentation( 12474 ),
                    ),
                    array(
                        'title'       => __( 'Date Rules', 'floating-awesome-button' ),
                        'description' => __( 'Supercharge your seasonal promotions with precise date-based button control! Schedule buttons to appear exactly when you need them - whether it\'s for holiday sales, special events, or limited-time offers.', 'floating-awesome-button' ),
                        'link'        => \FAB()->Helper->get_documentation( 12497 ),
                    ),
                    array(
                        'title'       => __( 'Time Rules', 'floating-awesome-button' ),
                        'description' => __( 'Take control of when your buttons appear throughout the day! Show different CTAs during business hours, display special offers at peak shopping times, or schedule support buttons only when your team is online.', 'floating-awesome-button' ),
                        'link'        => \FAB()->Helper->get_documentation( 12518 ),
                    ),
                ),
            )
        );
    }

    /**
     * Save Metabox Data.
     *
     * @param int     $post_id The post ID.
     * @param WP_Post $post    The post object.
     * @param bool    $update  Whether this is an update.
     */
    public function save_metabox_data( $post_id, $post, $update ) {
        // Ignore save metabox data if the post status is trash.
        if ( 'trash' === $post->post_status ) {
            return;
        }

        // Save Metabox Location.
        if ( \Fab()->Helper->check_input( FABMetaboxLocation::$input ) ) {
            $metabox = new FABMetaboxLocation();
            $metabox->sanitize();
            $metabox->setDefaultInput();
            $metabox->save();
        } else {
            delete_post_meta( $post->ID, FABMetaboxLocation::$post_metas['locations']['meta_key'] );
        }
    }

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

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Add Location metabox to Fab CPT.
        if ( ! \FAB()->Helper->isPremiumPlan() ) { // Hide metabox location upsell for premium user.
            add_action( 'add_meta_boxes', array( __CLASS__, 'jit_metabox_location' ), 10, 0 );
        }
    }
}
