<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class AIEO_DMM_Main_Price_Filter {

    
    private static $instance = null;

    
    private $hooked = false;

    public static function instance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    
    public function maybe_register() {
        if ($this->hooked) {
            return;
        }
        if (!class_exists('AIEO_DMM_Settings')) {
            return;
        }
        if (!AIEO_DMM_Settings::get('apply_recipe_to_main_price', false)) {
            return;
        }
        $this->register_hooks();
        $this->hooked = true;
    }

    
    private function register_hooks() {
        
        add_filter('woocommerce_product_get_price',         array($this, 'filter_product_price'),   20, 2);
        add_filter('woocommerce_product_get_sale_price',    array($this, 'filter_product_price'),   20, 2);

        
        add_filter('woocommerce_product_variation_get_price',       array($this, 'filter_product_price'), 20, 2);
        add_filter('woocommerce_product_variation_get_sale_price',  array($this, 'filter_product_price'), 20, 2);

        
        
        add_filter('woocommerce_variation_prices_price',       array($this, 'filter_variation_prices_price'),      20, 3);
        add_filter('woocommerce_variation_prices_sale_price',  array($this, 'filter_variation_prices_price'),      20, 3);

        
        
        
        
        
        
        
        
        

        
        
        add_filter('woocommerce_get_variation_prices_hash', array($this, 'filter_variation_prices_hash'), 20, 3);
    }

    
    public function filter_product_price($price, $product = null) {
        if (!is_numeric($price) || $price <= 0) {
            return $price;
        }
        if (!$product || !is_a($product, 'WC_Product')) {
            return $price;
        }
        if (!class_exists('AIEO_DMM_Pricing_Engine')) {
            return $price;
        }
        return AIEO_DMM_Pricing_Engine::instance()->apply_recipe_only(
            (float) $price,
            (int) $product->get_id()
        );
    }

    
    public function filter_variation_prices_price($price, $variation = null, $product = null) {
        if (!is_numeric($price) || $price <= 0) {
            return $price;
        }
        $target_id = 0;
        if ($variation && is_a($variation, 'WC_Product')) {
            $target_id = (int) $variation->get_id();
        } elseif ($product && is_a($product, 'WC_Product')) {
            $target_id = (int) $product->get_id();
        }
        if ($target_id <= 0 || !class_exists('AIEO_DMM_Pricing_Engine')) {
            return $price;
        }
        return AIEO_DMM_Pricing_Engine::instance()->apply_recipe_only(
            (float) $price,
            $target_id
        );
    }

    
    public function filter_cart_item($cart_item, $key = null, $values = array()) {
        return $cart_item;
    }

    
    public function filter_variation_prices_hash($hash, $product = null, $for_display = false) {
        if (!is_array($hash)) {
            $hash = array();
        }
        $hash['aieo_dmm_recipe'] = array(
            'user' => class_exists('AIEO_DMM_Cache') ? AIEO_DMM_Cache::user_context_key() : '',
            'v'    => implode(':', array(
                (int) get_option('aieo_dmm_cache_v_brands',   1),
                (int) get_option('aieo_dmm_cache_v_gifts',    1),
                (int) get_option('aieo_dmm_cache_v_loyalty',  1),
                (int) get_option('aieo_dmm_cache_v_prices',   1),
                (int) get_option('aieo_dmm_cache_v_needs',    1),
                (int) get_option('aieo_dmm_cache_v_shipping', 1),
            )),
        );
        return $hash;
    }
}
