<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;
use Fab\Metabox\FABMetaboxSetting;

/**
 * Featured Product Toast Frontend.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class WC_Featured_Product_Toast_Frontend extends WC_Featured_Product_Toast_Type implements JIT_Interface {

    /**
     * Adds WooCommerce Features data to the FAB item instance.
     *
     * @param object $instance The FAB item instance.
     */
    public function add_fab_item_data( $instance ) {
        if ( $instance->getType() !== $this->type ) {
            return;
        }

        // Prevent warning error when WooCommerce is not initialized.
        if ( ! did_action( 'woocommerce_init' ) || ! did_action( 'woocommerce_after_register_post_type' ) ) {
            add_action(
                'woocommerce_init',
                function () use ( $instance ) {
                    $this->add_fab_item_data( $instance );
                },
                20
            );
            return;
        }

        $instance->setToBeDisplayed( true );

        $product_id = get_post_meta( $instance->getID(), 'fab_setting_woocommerce_featured_product_toast_product', true );

        if ( empty( $product_id ) ) {
            $instance->setToBeDisplayed( false );
        }

        $product_image          = get_the_post_thumbnail_url( $product_id );
        $default_toast          = FABMetaboxSetting::get_input()['fab_setting_toast']['default'];
        $featured_product_toast = get_post_meta( $instance->getID(), FABMetaboxSetting::$post_metas['toast']['meta_key'], true );
        $featured_product_toast = ( $featured_product_toast ) ? \Fab\Plugin\Helper::getInstance()->ArrayMergeRecursive( (array) $default_toast, (array) $featured_product_toast ) : $default_toast;

        $additional_toast = array(
            'url_button' => get_permalink( $product_id ),
            'image'      => $product_image,
        );

        $placeholders = \Fab\Controller\WooCommerce::get_instance()->add_fab_placeholders( array(), $product_id );
        if ( ! empty( $placeholders ) ) {
            $instance->setContent( str_replace( array_keys( $placeholders ), array_values( $placeholders ), $instance->getContent() ) );
        }

        $final_toast = \Fab\Plugin\Helper::getInstance()->ArrayMergeRecursive( (array) $featured_product_toast, $additional_toast );
        $instance->setToast( $final_toast );
        $instance->setIsButton( false );
    }

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

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Prevent error if woocommerce plugin is not active.
        if ( ! FAB_WOOCOMMERCE_PLUGIN_ACTIVE ) {
            return;
        }

        // Adjust item data.
        add_action( 'fab_item_data', array( __CLASS__, 'jit_add_fab_item_data' ), 10, 1 );
    }
}
