<?php

namespace Fab\Controller;

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

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

/**
 * Featured Product Frontend.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class WC_Featured_Product_Frontend extends WC_Featured_Product_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;
        }

        // Get product id.
        $product_id = get_post_meta( $instance->getID(), FABMetaboxSetting::get_post_metas()['woocommerce_featured_product']['meta_key'], true );

        // Get product object.
        $product = wc_get_product( $product_id );

        // Check if product is valid.
        if ( $product ) {
            // Get product data.
            $product_data                            = $product->get_data();
            $product_data['post_title']              = $product->get_title();
            $product_data['image']                   = get_the_post_thumbnail_url( $product_id );
            $product_data['url']                     = get_permalink( $product_id );
            $product_data['price_html']              = $product->get_price_html();
            $product_data['regular_price_formatted'] = wc_price( $product->get_regular_price() );
            $product_data['sale_price_formatted']    = $product->is_on_sale() ? wc_price( $product->get_sale_price() ) : '';

            if ( ! $product->is_on_sale() ) {
                $product_data['sale_price'] = false;
            }

            // Set FAB Item attributes.
            $instance->setLink( $product_data['url'] );
            $instance->set_third_party_data(
                array(
                    'product' => $product_data,
                )
            );

            // If on product page, and the product is the current product, prevent display.
            if ( is_product() && get_the_ID() === (int) $product_id ) {
                $instance->setToBeDisplayed( false );
            }

            // If on cart or checkout page, and the product is already in the cart, prevent display.
            if ( is_cart() || is_checkout() ) {
                // Check if the product is already in the cart.
                $product_in_cart = false;

                foreach ( WC()->cart->get_cart() as $cart_item ) {
                    if ( $cart_item['product_id'] === (int) $product_id ) {
                        $product_in_cart = true;
                        break;
                    }
                }

                // If the product is in the cart, set to not be displayed.
                if ( $product_in_cart ) {
                    $instance->setToBeDisplayed( false );
                }
            }
        }

        // Set Link Href.
        $instance->setLinkHref( true );
    }

    /*
    |--------------------------------------------------------------------------
    | 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 );
    }
}
