<?php

namespace Openguru\OpenCartFramework\OpenCart;

use App\Services\SettingsService;
use Cart\Currency as OpenCartCurrency;
use Cart\Tax;

class PriceCalculator
{
    private OpenCartCurrency $currency;
    private Tax $tax;
    private SettingsService $settings;

    public function __construct(OpenCartCurrency $currency, Tax $tax, SettingsService $settings)
    {
        $this->currency = $currency;
        $this->tax = $tax;
        $this->settings = $settings;
    }

    public function getPriceNumeric($price, $taxClassId): float
    {
        return $this->tax->calculate(
            $price,
            $taxClassId,
            $this->settings->config()->getStore()->isOcConfigTax(),
        );
    }

    public function format($price, $taxClassId): string
    {
        $priceNumeric = $this->getPriceNumeric($price, $taxClassId);

        $zeroPriceText = $this->settings->get('texts.zero_price_text');

        if ($zeroPriceText && $priceNumeric === 0.0) {
            $price = $zeroPriceText;
        } else {
            $currency = $this->settings->config()->getStore()->getOcDefaultCurrency();
            $price = $this->currency->format($priceNumeric, $currency);
        }

        return $price;
    }
}
