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

defined('JPATH_BASE') or die();

require_once JPATH_ROOT . '/administrator/components/com_cmgiftcard/models/base_fields/radio.php';

use Joomla\CMS\Factory;
use Joomla\CMS\Form\Field\RadioField;
use Joomla\CMS\Plugin\PluginHelper;

/**
 * Form field for payment method selection in front-end.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class JFormFieldCMGCPaymentMethod extends CMGiftCardRadioField
{
	/**
	 * The form field type.
	 *
	 * @var    string
	 * @since  1.0.0
	 */
	protected $type = 'CMGCPaymentMethod';

	/**
	 * Method to get the field options.
	 *
	 * @return  array  The field option objects.
	 *
	 * @since   1.0.0
	 */
	public function getOptions()
	{
		$options = array();

		PluginHelper::importPlugin('cmgiftcard');
		$app = Factory::getApplication();
		$payments = $app->triggerEvent('onCMGCGetPaymentIdentity');

		if (count($payments))
		{
			foreach ($payments as $payment)
			{
				$option = new StdClass;
				$option->value = $payment['id'];
				$option->text = $payment['name'];

				$options[] = $option;
			}
		}

		if (count($payments) == 1)
		{
			$this->value = $payments[0]['id'];
		}

		return $options;
	}
}
