<?php

namespace Fab\Controller;

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

use Fab\Interfaces\Model_Interface;
use Fab\View;

/**
 * WooCommerce Metabox Product
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class WC_Metabox_Product extends Base implements Model_Interface {

    /**
     * Register metabox product on custom post type WooCommerce Product
     *
     * @return      void
     */
    public function metabox_product() {
        // Don't show metabox on add new screens.
        $screen = get_current_screen();
        if ( ! $screen || 'add' === $screen->action ) {
            return;
        }

        // Check if WooCommerce integrataion is enabled.
        if ( \Fab\Plugin\Helper::getInstance()->check_plugin_integration( FAB_WOOCOMMERCE_PLUGIN_FILE ) !== 'enabled' ) {
            return;
        }

        // Get recommendations.
        $max_recommendations = 4;
        $recommendations     = apply_filters( 'fab_metabox_post_recommendations', array(), $max_recommendations );
        // If no recommendations, return.
        if ( empty( $recommendations ) ) {
            return;
        }

        // Add metabox.
        add_meta_box(
            'fab-metabox-product',
            __( 'Floating Awesome Button (FAB)', 'floating-awesome-button' ),
            array( $this, 'metabox_product_callback' ),
            'product',
            'normal',
            'default'
        );
    }

    /**
     * Metabox Product set view template
     */
    public function metabox_product_callback() {
        global $post;

        // Get recommendations.
        $max_recommendations = 4;
        $recommendations     = apply_filters( 'fab_metabox_product_recommendations', array(), $max_recommendations );

        // Enqueue Script & component.
        \FAB()->Helper->wp_enqueue_script_component( 'wc-metabox-product-component', 'assets/components/wc-metabox-product/main.js', array(), FAB_VERSION, true );
        wp_localize_script(
            'wc-metabox-product-component',
            'FAB_WC_METABOX_PRODUCT',
            apply_filters(
                'fab_backend_enqueue_wc_metabox_product_localize',
                array(
                    'urls'            => array(
                        'woocommerce_tips' => ARTISTUDIO_URL . 'tag/woocommerce/',
                    ),
                    'data'            => array(),
                    'labels'          => array_merge(
                        \Fab\Helper\Metabox::getInstance()->get_metabox_labels(),
                        array(
                            'get_tips'         => __( 'Get Tips', 'floating-awesome-button' ),
                            'woocommerce_tips' => __( 'Get expert WooCommerce tips to help you optimize your products, increase sales, and build an effective product strategy for your online store. Learn proven techniques for product optimization, pricing strategies, inventory management, and customer engagement to maximize your product marketing success.', 'floating-awesome-button' ),
                        )
                    ),
                    'documentations'  => array(),
                    'recommendations' => $recommendations,
                    'post_id'         => $post->ID,
                    'nonce'           => wp_create_nonce( 'wp_rest' ),
                )
            )
        );

        // Enqueue style.
        \FAB()->Helper->wp_enqueue_style_sass( 'fab', 'assets/css/backend/style.scss' );

        // Load vendors.
        \FAB()->Helper->wp_enqueue_style( 'fontawesome', apply_filters( 'fab_fontawesome_css', 'vendor/fontawesome/css/all.min.css' ) );

        // Render View.
        View::RenderStatic( 'Backend.WooCommerce.metabox-product' );
    }

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

    /**
     * Execute Class.
     *
     * @return void
     */
    public function run() {
        // Prevent error if plugin is not active.
        if ( ! FAB_WOOCOMMERCE_PLUGIN_ACTIVE ) {
            return;
        }

        // @backend - Add metabox to WooCommerce Product
        add_action( 'add_meta_boxes', array( $this, 'metabox_product' ), 10, 0 );
    }
}
