<?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\MVC\Model\FormModel;
use Joomla\CMS\Plugin\PluginHelper;

/**
 * Shopping cart's form.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardModelCart extends FormModel
{
	/**
	 * Model context string.
	 *
	 * @var  string
	 */
	protected $_context = 'com_cmgiftcard.shopping_cart';

	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @return  void
	 *
	 * @since   1.0.0
	 */
	protected function populateState()
	{
		$app = Factory::getApplication('site');

		$params = $app->getParams();
		$this->setState('params', $params);
	}

	/**
	 * Method to build to form.
	 *
	 * @param   array    $data      An optional array of data for the form to interrogate.
	 * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
	 *
	 * @return  Form  A Form object on success, false on failure
	 *
	 * @since   1.0.0
	 */
	public function getForm($data = array(), $loadData = true)
	{
		// Get the form.
		$form = $this->loadForm('com_cmgiftcard.shopping_cart', 'cart', array('control' => 'jform', 'load_data' => true));

		if (empty($form))
		{
			return false;
		}

		$params = ComponentHelper::getParams('com_cmgiftcard');

		if (!$params->get('street_address_1', 0))
			$form->removeField('address_1');

		if (!$params->get('street_address_2', 0))
			$form->removeField('address_2');

		if (!$params->get('street_address_3', 0))
			$form->removeField('address_3');

		if (!$params->get('postal_box', 0))
			$form->removeField('postal_box');

		if (!$params->get('postal_code', 0))
			$form->removeField('postal_code');

		if (!$params->get('city', 0))
			$form->removeField('city');

		if (!$params->get('state', 0))
			$form->removeField('state');

		if (!$params->get('country', 0))
			$form->removeField('country');

		if (!$params->get('tos', 0))
			$form->removeField('tos');

		$app = Factory::getApplication();

		PluginHelper::importPlugin('cmgiftcard');

		$payments = $app->triggerEvent('onCMGCGetPaymentIdentity');

		if (count($payments) == 1 && $params->get('hide_lone_payment_method', 0) == 1)
		{
			$form->removeField('payment_method');
		}
		else
		{
			$form->setFieldAttribute('payment_method', 'required', 'true');
		}

		return $form;
	}

	/**
	 * Method to get the data that should be injected in the form.
	 *
	 * @return  array    The default data is an empty array.
	 *
	 * @since   1.0.0
	 */
	protected function loadFormData()
	{
		$app = Factory::getApplication();

		$data = (array) $app->getUserState('com_cmgiftcard.shopping_cart.data', array());

		if (isset($data['tos']))
			unset($data['tos']);

		$this->preprocessData('com_cmgiftcard.shopping_cart', $data);

		return $data;
	}
}
