<?php

namespace KupShop\ContentBundle\Page;

use KupShop\ContentBundle\Entity\Placeholder;
use KupShop\ContentBundle\View\OrderView;

class OrderSuccessPage extends BasePage
{
    protected static $name = 'Objednávka úspěšně vytvořena';
    protected static $type = 'ORDER_SUCCESS';
    protected static $priority = 9;

    /** @var OrderView */
    protected $view;

    public function getUrl(): string
    {
        $order = new \Order();
        $order->createFromDB(returnSQLResult('SELECT MAX(id) FROM orders'));

        return path('kupshop_content_orders_order', ['id' => $order->id, 'cf' => $order->getSecurityCode(), 'status' => 1]);
    }

    public function getPlaceholders(): array
    {
        return [
            new Placeholder('KOD', 'Kód objednávky', fn () => $this->getOrder()->order_no),
            new Placeholder('ODKAZ', 'Odkaz na detail objednávku', fn () => $this->getOrder()->getUrl()),
            new Placeholder('SHOP_EMAIL', 'Email e-shopu', fn () => \Settings::getDefault()->shop_email),
            new Placeholder('INSTRUKCE_PLATBY', 'Popis způsobu platby', fn () => $this->getPaymentDescription()),
            new Placeholder('DOKONCENI_REGISTRACE', 'Odkaz na dokončení registrace', fn () => $this->getOrderRegisterUrl()),
        ];
    }

    public function getOrderRegisterUrl(): string
    {
        $order = $this->getOrder();

        return path('kupshop_ordering_order_orderregister', ['id' => $order->id, 'cf' => $order->getSecurityCode()]);
    }

    public function getPaymentDescription()
    {
        $payment = $this->getOrder()->getDeliveryType()->getPayment();
        if ($payment) {
            $payment->setOrder($this->getOrder());

            return $payment->getOrderViewDescription();
        }

        return '';
    }

    protected function getOrder(): \Order
    {
        return $this->view->getOrder();
    }
}
