<?php

namespace KupShop\OrderingBundle\View;

use KupShop\KupShopBundle\Context\ContextManager;
use KupShop\KupShopBundle\Exception\RedirectException;
use KupShop\KupShopBundle\Util\Database\QueryHint;
use KupShop\KupShopBundle\Views\Traits\RequestTrait;
use KupShop\KupShopBundle\Views\View;

class PaymentView extends View
{
    use RequestTrait;

    protected $template = 'payment.tpl';

    /** @var ContextManager */
    protected $contextManager;

    private $payment;

    private $step;

    private $class;

    private $orderId;

    private $message;

    public function getTemplateVariables()
    {
        $vars = parent::getTemplateVariables();

        $vars['payment'] = $this->getPayment();

        return $vars;
    }

    public function getBodyVariables()
    {
        $vars = parent::getBodyVariables();

        if (!findModule('payments')) {
            redirection('MODUL_NOT_FOUND');
        }

        $this->getPayment();

        $vars['returnNav'] = '';
        $vars['step'] = $this->getStep();
        $vars['message'] = $this->getMessage();

        return $vars;
    }

    public function getPayment()
    {
        QueryHint::routeToMaster();

        if (!isset($this->payment)) {
            if ($this->getOrderId() >= 0) {
                $this->payment = \Payment::getClass($this->getClass());
                $this->payment->setRequest($this->request);

                if ($this->getOrderId() > 0) {
                    $this->payment->setOrder($this->getOrderId());
                    $this->payment->checkAuth();
                }

                if (!$this->payment->hasOnlinePayment() && $this->payment->order) {
                    throw new RedirectException($this->payment->order->getDetailUrl());
                }

                if (($this->payment->order && $this->payment->order->order_no !== null) && $this->contextManager) {
                    $this->contextManager->activateOrder($this->payment->order, function () {
                        $this->payment->processStep($this->getStep());
                    });
                } else {
                    $this->payment->processStep($this->getStep());
                }

                $this->setTemplate($this->payment->template);
            }
        }

        return $this->payment;
    }

    public function getTitle()
    {
        $title = translate('title', 'payment');

        return isset($title) ? $title : 'online platba';
    }

    public function getMessage()
    {
        if ($this->message == null) {
            $this->message = getVal('message');
        }

        return $this->message;
    }

    public function getOrderId()
    {
        if ($this->orderId == null) {
            $this->orderId = intval(getVal('IDo', null, 0));
        }

        return $this->orderId;
    }

    public function getStep()
    {
        return $this->step;
    }

    public function getClass()
    {
        return $this->class;
    }

    public function setTemplate(string $template): self
    {
        $this->template = $template;

        return $this;
    }

    public function setClass($class)
    {
        $this->class = $class;
    }

    public function setStep($step)
    {
        $this->step = $step;
    }

    public function setOrderId($orderId)
    {
        $this->orderId = $orderId;
    }

    /**
     * @required
     */
    public function setContextManager(ContextManager $contextManager)
    {
        $this->contextManager = $contextManager;
    }
}
