<?php

namespace Fab\Controller;

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

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

/**
 * WooCommerce Metabox Coupon
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class WC_Metabox_Coupon extends Base implements Model_Interface {

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

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

    /**
     * Metabox Product set view template
     *
     * @return      string              Html template string from view View/Template/backend/metabox_coupon.php
     * @param       object $post      global $post object
     */
    public function metabox_coupon_callback() {
        global $post;

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

        // 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-coupon-component', 'assets/components/wc-metabox-coupon/main.js', array(), FAB_VERSION, true );
        wp_localize_script(
            'wc-metabox-coupon-component',
            'FAB_WC_METABOX_COUPON',
            apply_filters(
                'fab_backend_enqueue_wc_metabox_coupon_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 coupons, increase sales, and build an effective discount strategy for your online store. Learn proven techniques for creating compelling offers, timing your promotions, and targeting specific customer segments.', 'floating-awesome-button' ),
                        )
                    ),
                )
            )
        );

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

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