<?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\Router\Route;
use Joomla\CMS\MVC\Controller\FormController;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;

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

	/**
	 * Download PDF gift card.
	 *
	 * @return  boolean
	 *
	 * @since   2.2.0
	 */
	public function download()
	{
		Session::checkToken('get') or jexit(Text::_('JINVALID_TOKEN'));

		$app = Factory::getApplication();
		$cardId = $this->input->getInt('id');
		$card = BaseDatabaseModel::getInstance('Card', 'CMGiftCardModel')->getItem($cardId);

		if (empty($card->id))
		{
			jexit(Text::_('COM_CMGIFTCARD_ERROR_CARD_NOT_EXIST'));
		}

		if ($card->card_type != 'electronic')
		{
			jexit(Text::_('COM_CMGIFTCARD_ERROR_CARD_NOT_ELECTRONIC'));
		}

		jimport('mpdf.mpdf');

		$params = ComponentHelper::getParams('com_cmgiftcard');
		$pdf = $params->get('electronic_pdf', 0);
		$mpdfExists = class_exists('\Mpdf\Mpdf') ? true : false;

		if (!$pdf || !$mpdfExists)
		{
			jexit(Text::_('COM_CMGIFTCARD_ERROR_PDF_DISABLED'));
		}

		$qrCodeSize = (int) $params->get('pdf_qrcode_size', 2);
		$qrCode = '<barcode code="' . $card->card_code . '" type="QR" class="barcode" size="' . $qrCodeSize . '" error="M" disableborder="1" />';
		$fileName = 'gift-card-' . $card->card_code . '.pdf';
		$cardImagePath = JPATH_ROOT . '/' . str_replace('%20', ' ', CMGiftCardHelper::getImageUrl($card->design_image));
		$created = $card->created ? HTMLHelper::_('date', $card->created, 'DATE_FORMAT_LC2') : '';
		$completed = $card->completed ? HTMLHelper::_('date', $card->completed, 'DATE_FORMAT_LC2') : '';

		$pdfTags = [
			'{sender_name}'			=> $card->from_name,
			'{recipient_name}'		=> $card->to_name,
			'{gift_card_image}'		=> '<img src="var:cardImage" style="width: 100%;">',
			'{gift_card_amount}'	=> HTMLHelper::_('cmgiftcard.formatPrice', $card->amount),
			'{gift_card_code}'		=> $card->card_code,
			'{gift_card_qr_code}'	=> $qrCode,
			'{gift_card_message}'	=> $card->message,
			'{order_date}'			=> $created,
			'{completed_date}'		=> $completed,
		];

		$giftCardContent = $params->get('electronic_pdf_template');

		foreach ($pdfTags as $tag => $value)
		{
			$giftCardContent = str_replace($tag, $value, $giftCardContent);
		}

		$tmpPath = $app->get('tmp_path');

		$mpdf = new \Mpdf\Mpdf([
			'mode'	=> 'utf-8',
			'format'=> 'A4',
			'tempDir' => $tmpPath
		]);

		$mpdf->imageVars['cardImage'] = file_get_contents($cardImagePath);
		$mpdf->WriteHTML($giftCardContent);
		$mpdf->Output($fileName, 'I');
		$app->close();
	}
}
