<?php
/**
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @copyright   Copyright (C) 2016-2024 CMExension
 * @license     GNU General Public License version 2 or later
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Plugin\PluginHelper;

/**
 * HTML utility class for com_cmgiftcard.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
abstract class JHtmlCMGiftCard
{
	/**
	 * Format prices.
	 *
	 * @param   float   $price    The price.
	 * @param   object  $params   The component's params.
	 * @param   string  $postfix  The price's postfix text.
	 *
	 * @return  string
	 *
	 * @since   1.0.0
	 */
	public static function formatPrice($price = 0, $params = null, $postfix = '')
	{
		if ($params === null)
		{
			$params = ComponentHelper::getParams('com_cmgiftcard');
		}

		$decimals = $params->get('currency_decimals', 0, 'unint');
		$decimalPoint = $params->get('decimal_point', '.');
		$thousandSeparator = $params->get('thousand_separator', ',');
		$currencySymbol = $params->get('currency_symbol', '');
		$currencySymbolPosition = $params->get('currency_symbol_position', 'before');

		if ($thousandSeparator != ',' && $thousandSeparator != '.')
		{
			$thousandSeparator = '';
		}

		$amount = floatval($price);
		$formattedPrice = number_format($amount, $decimals, $decimalPoint, $thousandSeparator) . $postfix;

		return ($currencySymbolPosition == 'after') ? $formattedPrice . $currencySymbol : $currencySymbol . $formattedPrice;
	}

	/**
	 * Get payment method name.
	 *
	 * @param   string  $paymentMethodId  The ID of payment method.
	 *
	 * @return  string
	 *
	 * @since   1.0.0
	 */
	public static function getPaymentMethodName($paymentMethodId)
	{
		PluginHelper::importPlugin('cmgiftcard');
		$app = Factory::getApplication();
		$payments = $app->triggerEvent('onCMGCGetPaymentIdentity');

		if (count($payments))
		{
			foreach ($payments as $payment)
			{
				if ($paymentMethodId == $payment['id'])
					return $payment['name'];
			}
		}

		return '';
	}
}
