<?php

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\OrderingBundle\Util\Order\QRPayment;

function smarty_function_get_qrcodeimage_url($params, &$smarty)
{
    $QRPayment = ServiceContainer::getService(QRPayment::class);

    if (empty($params['order'])) {
        throw new InvalidArgumentException("Missing parameter 'order' in smarty function 'get_qrcodeimage_url'!");
    }

    $size = 200;
    if (!empty($params['size'])) {
        $size = $params['size'];
    }

    $url = $QRPayment->getQRCodeImageUrl($params['order'], $size, $params['qr_parameters'] ?? []);
    if (!empty($params['get_contents']) && $url) {
        $img = file_get_contents($url);
        if ($img) {
            $url = 'data:image/png;base64,'.base64_encode($img);
        } else {
            $url = '';
        }
    }

    if (!empty($params['assign'])) {
        $smarty->assign($params['assign'], $url);
    } else {
        return $url;
    }
}
