<?php

use KupShop\CDNBundle\CDN;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

/**
 * Smarty {url} plugin.
 *
 * Type:     function<br>
 * Name:     get_video<br>
 * Purpose:  return array describing video
 *
 * @param array                    $params parameters
 * @param Smarty_Internal_Template $smarty template object
 *
 * @return array
 */
function smarty_function_get_video($params, &$smarty)
{
    if (!isset($params['id'])) {
        throw new InvalidArgumentException("Missing parameter 'id' in smarty function 'get_video'!");
    }
    $id = $params['id'];
    $resolution = $params['resolution'] ?? '240p';
    $autoplay = $params['autoplay'] ?? true;
    $cdn = ServiceContainer::getService(CDN::class);
    $result = [
        'mp4' => $cdn->getMP4Video($id, $resolution),
        'hls' => $cdn->getHLSPlaylist($id),
        'direct' => $cdn->getIframeUrl($id, $autoplay),
    ];

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