<?php

namespace CouponsPlus\App\Handlers;

use CouponsPlus\App\Offers\DiscountState;
use CouponsPlus\Original\Events\Handler\EventHandler;

/**
 * For the classic cart
 */
#[\AllowDynamicProperties]
Class ClassicCartCartItemPriceModifierHandler extends EventHandler
{
    protected $numberOfArguments = 1;
    protected $priority = 10;

    public function execute()
    {
/*        add_filter(
            'woocommerce_cart_item_price', 
            [$this, 'modifyCartItemPrice'], 
            $priority = 20, 
            $numberOfArguments = 2
        );*/

        add_filter(
            'woocommerce_cart_item_subtotal', 
            [$this, 'modifyCartItemSubtotal'], 
            $priority = 20, 
            $numberOfArguments = 2
        );   
    }

    /**
     */
    public function modifyCartItemPrice(string $priceHtml, array $cartItem)
    {
        $discountState = DiscountState::getInstance();

        if (!$discountState->cartItemHasBeenDiscounted($cartItem['key'])) {
            return $priceHtml;
        }

        $newPrice = wc_price($discountState->getSumOfCartItemDiscounts($cartItem['key']));

        return "
            <s>{$priceHtml}</s>
            <span class=\"cp-modified-price\">{$newPrice}</span>
        ";
    }

    public function modifyCartItemSubtotal(string $subtotalHtml, array $cartItem)
    {
        $discountState = DiscountState::getInstance();

        if (!$discountState->cartItemHasBeenDiscounted($cartItem['key'])) {
            return $subtotalHtml;
        }

        $newPrice = wc_price($discountState->getSumOfCartItemDiscounts($cartItem['key']));

        return "
            {$subtotalHtml}
            <span>(-{$newPrice})</span>
        ";
    }
}
