<?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\Language\Text;

/**
 * Gift card purchase form.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardModelForm extends FormModel
{
	/**
	 * Model context string.
	 *
	 * @var  string
	 */
	protected $_context = 'com_cmgiftcard.gift_card';

	/**
	 * 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.gift_card', 'form', array('control' => 'jform', 'load_data' => true));

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

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

		if (isset($data['card_type']))
		{
			$cardType = $data['card_type'];
		}
		else
		{
			$cardType = Factory::getApplication()->input->get('type', 'electronic', 'word');
		}

		if ($cardType != 'physical' && $cardType != 'electronic')
			$cardType = 'electronic';

		if ($cardType == 'physical')
		{
			$amountType = $params->get('physical_amount_type', 'custom');
			$form->removeField('deliver_date');

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

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

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

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

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

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

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

			if (!$params->get('to_country', 0))
				$form->removeField('to_country');
		}
		else
		{
			$selectDeliveryDate = $params->get('electronic_date_selection', 1);

			if (!$selectDeliveryDate)
			{
				$form->removeField('deliver_date');
			}
			else
			{
				$dateFormat = $params->get('date_format', 'COM_CMGIFTCARD_DATE_FORMAT_2');
				$form->setFieldAttribute('deliver_date', 'format', Text::_($dateFormat));
			}

			$amountType = $params->get('electronic_amount_type', 'custom');
			$form->removeField('to_address_1');
			$form->removeField('to_address_2');
			$form->removeField('to_address_3');
			$form->removeField('to_city');
			$form->removeField('to_state');
			$form->removeField('to_postal_code');
			$form->removeField('to_postal_box');
			$form->removeField('to_country');
		}

		if ($amountType == 'custom')
		{
			$form->removeField('defined_amount');
		}
		elseif ($amountType == 'defined')
		{
			$form->removeField('defined_amount');
			$form->setFieldAttribute('amount', 'type', 'cmgcdefinedamount');
		}
		elseif ($amountType == 'defined_custom')
		{
			$form->setFieldAttribute('amount', 'description', 'COM_CMGIFTCARD_FORM_FIELD_DEFINED_AMOUNT_DESC');
		}

		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();
		$itemId = $app->input->get('item_id', 0, 'uint');

		if ($itemId <= 0)
		{
			$data = (array) $app->getUserState('com_cmgiftcard.gift_card.data', array());
		}
		else
		{
			$session = Factory::getSession();
			$cart = $session->get('cart', array(), 'CMGiftCard');

			if (isset($cart['items'][$itemId - 1]))
			{
				$data = $cart['items'][$itemId - 1];
			}
		}

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

		if (!isset($data['deliver_date']) || $data['deliver_date'] == '')
		{
			$user = Factory::getUser();
			$date = Factory::getDate('now');

			if ($user->get('id'))
				$date->setTimezone($user->getTimezone());

			$data['deliver_date'] = $date->format('Y-m-d H:i:s', true, false);
		}

		if (!isset($data['card_type']) || $data['card_type'] == '' || ($data['card_type'] != 'physical' && $data['card_type'] != 'electronic'))
		{
			$cardType = Factory::getApplication()->input->get('type', 'electronic', 'word');

			if ($cardType != 'physical' && $cardType != 'electronic')
				$cardType = 'electronic';

			$data['card_type'] = $cardType;
		}

		return $data;
	}
}
