<?php

namespace Fab\Controller;

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

use Fab\Interfaces\Model_Interface;
use Fab\View;
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;

/**
 * WooCommerce Metabox Order
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class WC_Metabox_Order extends Base implements Model_Interface {

    /**
     * Register metabox order on custom post type WooCommerce Order
     *
     * @return void
     */
    public function metabox_order() {
        // 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;
        }

        // Check if WooCommerce is using the new orders table.
        $screen = class_exists( '\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) && wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled()
        ? wc_get_page_screen_id( 'shop-order' )
        : 'shop_order';

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

    /**
     * Metabox Order set view template
     *
     * @return void
     */
    public function metabox_order_callback() {
        global $post;

        // Supported Templates.
        $supported_templates = array(
            'woocommerce-add-to-cart',
            'woocommerce-apply-coupon-cart-quantity',
            'woocommerce-apply-coupon-cart-subtotal',
            'woocommerce-apply-coupon-logged-in-customer',
            'woocommerce-apply-coupon',
            'woocommerce-buy-now',
            'woocommerce-cart-reminder',
            'woocommerce-customer-button',
        );
        $supported_templates = \Fab\Helper\FAB_Template::getInstance()->transform_metabox_templates( $supported_templates );

        // Enqueue style.
        \FAB()->Helper->wp_enqueue_style_sass( 'fab', 'assets/css/backend/style.scss' );
        \FAB()->Helper->wp_enqueue_style( 'fontawesome', apply_filters( 'fab_fontawesome_css', 'vendor/fontawesome/css/all.min.css' ) );

        // Enqueue Script & component.
        \FAB()->Helper->wp_enqueue_script_component( 'wc-metabox-order-component', 'assets/components/wc-metabox-order/main.js', array(), FAB_VERSION, true );
        wp_localize_script(
            'wc-metabox-order-component',
            'FAB_WC_METABOX_ORDER',
            apply_filters(
                'fab_backend_enqueue_wc_metabox_order_localize',
                array(
                    'templates' => apply_filters( 'fab_wc_metabox_supported_templates', $supported_templates ),
                    'urls'      => array(
                        'woocommerce_tips' => ARTISTUDIO_URL . 'tag/woocommerce/',
                    ),
                    '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 orders, streamline fulfillment, and improve customer satisfaction. Learn proven techniques for order management, shipping optimization, and handling customer communications effectively.', 'floating-awesome-button' ),
                        )
                    ),
                )
            )
        );

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

    /*
    |--------------------------------------------------------------------------
    | 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 Order
        add_action( 'add_meta_boxes', array( $this, 'metabox_order' ), 10, 0 );
    }
}
