<?php

namespace KupShop\OrderDiscountBundle\Triggers;

use KupShop\OrderDiscountBundle\Entity\OrderDiscount;
use KupShop\OrderDiscountBundle\Triggers\Frontend\BaseTriggerHandler;
use KupShop\OrderingBundle\Entity\Purchase\PurchaseState;

abstract class AbstractTrigger implements TriggerInterface
{
    protected static $type;
    protected static $position = 999;
    protected $adminTemplate;
    protected $translationSection = 'OrderDiscountsTrigger';
    protected $icon_class = 'glyphicon glyphicon-check';

    /**
     * @return string
     */
    public function getAdminTemplate()
    {
        return $this->adminTemplate;
    }

    /**
     * @return string
     *
     * @throws \Exception
     */
    public static function getType()
    {
        if (!static::$type) {
            throw new \Exception('Missing type');
        }

        return static::$type;
    }

    public static function getPosition()
    {
        return static::$position;
    }

    /**
     * @return string
     *
     * @throws \Exception
     */
    public function getName()
    {
        return translate(static::$type, $this->translationSection);
    }

    /**
     * @return string
     */
    public function getDescription()
    {
        return translate(static::$type.'Description', $this->translationSection);
    }

    /**
     * @return string
     */
    public function getIconClass()
    {
        return $this->icon_class;
    }

    public function getDiscountFilterSpec(PurchaseState $purchaseState)
    {
        return null;
    }

    public function isApplicable(PurchaseState $purchaseState, OrderDiscount $orderDiscount, array $data, ?array $persistentData = null): bool
    {
        return true;
    }

    public function getMultiplier(PurchaseState $purchaseState, OrderDiscount $orderDiscount, array $data): ?array
    {
        return null;
    }

    public function getErrorMessage(PurchaseState $purchaseState, array $data): string
    {
        return '';
    }

    public function handleData($data)
    {
        return $data;
    }

    protected function getVars($vars): array
    {
        return $vars;
    }

    /**
     * @param string $name
     * @param array  $data
     *
     * @return string
     *
     * @throws \SmartyException
     */
    public function printTriggerSettings($name = 'data', $data = [])
    {
        if (!$this->getAdminTemplate()) {
            throw new \Exception('Missing template');
        }

        $smarty = createSmarty(true, true);

        $templateVars = [
            'name' => $name,
            'data' => $data,
            'object' => $this,
        ];

        $smarty->assign($this->getVars($templateVars));

        return $smarty->fetch($this->getAdminTemplate());
    }

    /**
     * @return bool
     */
    public function checkValidity(&$data)
    {
        return true;
    }

    public function isRangeValid($number, \Range $range)
    {
        if (($number >= $range->min() || $range->min() === null) && ($number <= $range->max() || $range->max() === null)) {
            return true;
        }

        return false;
    }

    public function getFrontendHandler(): ?BaseTriggerHandler
    {
        return null;
    }

    public function getHandlerData($purchaseState): array
    {
        return $purchaseState->getCustomData($this::$type) ?: [];
    }
}
