<?php

/**
 * DRBU_Product_Display — automatski prikaz na single product strani.
 *
 * Ubacuje buy box na woocommerce_single_product_summary i, po želji,
 * uklanja default WooCommerce "Add to cart" formu.
 *
 * @package discount-rules-box-upsell
 */
defined( 'ABSPATH' ) || exit;
class DRBU_Product_Display {
    /**
     * Singleton instanca.
     *
     * @var DRBU_Product_Display|null
     */
    private static $instance = null;

    /**
     * Vraća jedinu instancu.
     *
     * @return DRBU_Product_Display
     */
    public static function instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Privatan konstruktor.
     */
    private function __construct() {
        add_action( 'template_redirect', array($this, 'maybe_replace_add_to_cart') );
        add_action( 'woocommerce_single_product_summary', array($this, 'render_box'), 35 );
        add_filter( 'body_class', array($this, 'body_class') );
    }

    /**
     * Uklanja default add-to-cart formu (i opciono default cenu)
     * pre iscrtavanja summary-ja.
     */
    public function maybe_replace_add_to_cart() {
        if ( !is_product() ) {
            return;
        }
        $settings = DRBU_Settings::instance();
        if ( !$this->is_box_active() ) {
            return;
        }
        if ( 'yes' === $settings->get( 'drbu_replace_add_to_cart', 'yes' ) ) {
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        }
    }

    /**
     * Iscrtava buy box na strani proizvoda.
     */
    public function render_box() {
        if ( !is_product() || !$this->is_box_active() ) {
            return;
        }
        $product_id = (int) get_queried_object_id();
        echo DRBU_Shortcode::instance()->render( $product_id );
        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- render() već escape-uje ceo izlaz.
    }

    /**
     * Dodaje body class za skrivanje default cene i forme (block teme).
     *
     * @param array $classes Postojeće body klase.
     * @return array
     */
    public function body_class( $classes ) {
        if ( !is_product() || !$this->is_box_active() ) {
            return $classes;
        }
        if ( 'yes' === DRBU_Settings::instance()->get( 'drbu_replace_add_to_cart', 'yes' ) ) {
            $classes[] = 'drbu-hide-default-price';
            $classes[] = 'drbu-replace-add-to-cart';
        }
        return $classes;
    }

    /**
     * Proverava da li je buy box aktivan na trenutnom proizvodu.
     *
     * @return bool
     */
    private function is_box_active() {
        if ( !is_product() ) {
            return false;
        }
        $product_id = (int) get_queried_object_id();
        if ( 'yes' !== DRBU_Settings::instance()->get( 'drbu_auto_insert_product', 'yes' ) ) {
            return false;
        }
        if ( !$this->is_allowed( $product_id ) ) {
            return false;
        }
        $product = wc_get_product( $product_id );
        return $product && $product->is_purchasable() && $product->is_type( 'simple' );
    }

    /**
     * Proverava da li je proizvod dozvoljen (ograničenje na proizvode i/ili
     * kategorije — Pro funkcija). Free: ograničenja ne postoje, box se
     * prikazuje svuda. Pro: prava provera u is_allowed__premium_only().
     *
     * @param int $product_id ID proizvoda.
     * @return bool
     */
    private function is_allowed( $product_id ) {
        if ( !(function_exists( 'drbu_fs' ) && drbu_fs() && drbu_fs()->is__premium_only()) ) {
            return true;
        }
        return $this->is_allowed__premium_only( $product_id );
    }

}
