<?php

namespace KupShop\OrderDiscountBundle\Actions;

use KupShop\OrderDiscountBundle\Actions\Frontend\BannerHandler;
use KupShop\OrderDiscountBundle\Actions\Frontend\HandlerInterface;
use KupShop\OrderDiscountBundle\Entity\OrderDiscount;
use KupShop\OrderingBundle\Entity\Purchase\PurchaseState;

class BannerAction extends AbstractAction
{
    protected static $type = 'banner';
    protected static $position = 199;
    protected $adminTemplate = 'actions/banner.tpl';

    private $bannerHandler;

    public function __construct(BannerHandler $bannerHandler)
    {
        $this->bannerHandler = $bannerHandler;
    }

    public function applyResult(PurchaseState &$purchaseState, OrderDiscount $orderDiscount, array $data)
    {
        $purchaseState->addUsedDiscount($orderDiscount->getId());
    }

    public function getFrontendHandler(): ?HandlerInterface
    {
        return $this->bannerHandler;
    }

    protected function getVars($vars)
    {
        $vars['sliders'] = ['0' => 'Žádný banner'];
        if (findModule('sliders')) {
            if ($sliders = sqlFetchAll(sqlQuery('SELECT id, name FROM sliders'), ['id' => 'name'])) {
                $vars['sliders'] = $sliders;
            }
        }

        return $vars;
    }
}
