<?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\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\MVC\Controller\AdminController;
use Joomla\CMS\Session\Session;
use Joomla\Utilities\ArrayHelper;

/**
 * Controller class for gift card list.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardControllerCards extends AdminController
{
	/**
	 * @var    string  The prefix to use with controller messages.
	 *
	 * @since  1.0.0
	 */
	protected $text_prefix = 'COM_CMGIFTCARD_CARD';

	/**
	 * Proxy for getModel.
	 *
	 * @param   string  $name    The model name. Optional.
	 * @param   string  $prefix  The class prefix. Optional.
	 * @param   array   $config  The array of possible config values. Optional.
	 *
	 * @return  object  The model.
	 *
	 * @since   1.0.0
	 */
	public function getModel($name = 'Card', $prefix = 'CMGiftCardModel', $config = array('ignore_request' => true))
	{
		$model = parent::getModel($name, $prefix, $config);

		return $model;
	}

	/**
	 * Removes an item.
	 *
	 * @return  void
	 *
	 * @since   1.0.0
	 */
	public function delete()
	{
		$this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list, false));
	}

	/**
	 * Mark gift cards as delivered
	 *
	 * @return  boolean
	 *
	 * @since   1.2.0
	 */
	public function markDelivered()
	{
		Session::checkToken() or die(Text::_('JINVALID_TOKEN'));

		$cid = $this->input->get('cid', array(), 'array');
		$date = $this->input->get('delivery_date', '', 'string');
		$date = Factory::getDate($date, Factory::getApplication()->get('offset'))->toSql();
		ArrayHelper::toInteger($cid);

		$this->setRedirect(Route::_('index.php?option=com_cmgiftcard&view=cards', false));

		if (empty($cid))
		{
			$this->setMessage(Text::_($this->text_prefix . '_NO_ITEM_SELECTED'), 'error');

			return false;
		}

		if (empty($date))
		{
			$this->setMessage(Text::_('COM_CMGIFTCARD_ERROR_EMPTY_DELIVERY_DATE'), 'error');

			return false;
		}

		$model = $this->getModel();

		try
		{
			$model->markDelivered($cid, $date);
			$errors = $model->getErrors();
		}
		catch (Exception $e)
		{
			$this->setMessage($e->getMessage(), 'error');

			return false;
		}

		$ntext = $this->text_prefix . '_N_ITEMS_MARKED_DELIVERED';
		$this->setMessage(Text::plural($ntext, count($cid)));

		return true;
	}

	/**
	 * Mark gift cards as redeemed.
	 *
	 * @return  boolean
	 *
	 * @since   2.2.0
	 */
	public function markRedeemed()
	{
		Session::checkToken() or die(Text::_('JINVALID_TOKEN'));

		$cid = $this->input->get('cid', array(), 'array');
		$date = $this->input->get('delivery_date', '', 'string');
		$date = Factory::getDate($date, Factory::getApplication()->get('offset'))->toSql();
		ArrayHelper::toInteger($cid);

		$this->setRedirect(Route::_('index.php?option=com_cmgiftcard&view=cards', false));

		if (empty($cid))
		{
			$this->setMessage(Text::_($this->text_prefix . '_NO_ITEM_SELECTED'), 'error');

			return false;
		}

		if (empty($date))
		{
			$this->setMessage(Text::_('COM_CMGIFTCARD_ERROR_EMPTY_DELIVERY_DATE'), 'error');

			return false;
		}

		$model = $this->getModel();

		try
		{
			$model->setRedemptionStatus($id, 1, $date);
			$errors = $model->getErrors();
		}
		catch (Exception $e)
		{
			$this->setMessage($e->getMessage(), 'error');

			return false;
		}

		$ntext = $this->text_prefix . '_N_ITEMS_MARKED_DELIVERED';
		$this->setMessage(Text::plural($ntext, count($cid)));

		return true;
	}
}
