<?php

/*
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:     function
 * Name:     get_product_info
 * Purpose:  returns various information about product
 * -------------------------------------------------------------
 */

use KupShop\CatalogBundle\Util\FavoriteProductsUtil;
use KupShop\KupShopBundle\Context\UserContext;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\KupShopBundle\Util\Contexts;

function smarty_function_get_product_info($params, &$smarty)
{
    $type = null;
    $id = null;
    /** @var Product $product */
    $product = null;
    $result = null;

    extract($params);

    if (empty($type)) {
        trigger_error("Chybějící parametr 'type'");
    }

    if (empty($id) && empty($product)) {
        trigger_error("Chybějící parametr 'id' nebo 'product'");
    }

    if (empty($id)) {
        $id = $product->id;
    }

    switch ($type) {
        case 'is_favorite':
            $userContext = Contexts::get(UserContext::class);
            $favoriteProductsUtil = ServiceContainer::getService(FavoriteProductsUtil::class);

            $userId = null;
            if ($userContext->getActiveId()) {
                $userId = (int) $userContext->getActiveId();
            }

            $result = $favoriteProductsUtil->isProductFavorite((int) $id, $userId, FavoriteProductsUtil::getCookieProducts());
            break;

        case 'is_watchdog':
            $userContext = Contexts::get(UserContext::class);
            $watchdog = ServiceContainer::getService(\KupShop\WatchdogBundle\Util\Watchdog::class);
            if (!$userContext->getActiveId()) {
                return false;
            }

            $result = $watchdog->isWatchdog((int) $userContext->getActiveId(), (int) $id, $id_variation ? (int) $id_variation : null);
            break;

        default:
            trigger_error("Neexistující 'type': {$type}");
    }

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