<?php

namespace Autogedal\Retur\Controller\Index;

use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Customer\Model\Session as CustomerSession;

class Success extends \Magento\Framework\App\Action\Action implements HttpGetActionInterface
{
    protected $resultPageFactory;
    protected $customerSession;

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        CustomerSession $customerSession
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
        $this->customerSession = $customerSession;
    }

    public function execute()
    {
        $returnData = $this->customerSession->getReturnSuccessData();
        if (empty($returnData)) {
            return $this->resultRedirectFactory->create()->setPath('retur/index');
        }

        $this->customerSession->unsReturnSuccessData();
        //@todo: Refactor it to match CQRS
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getLayout()->getBlock('returns.success')->setReturnData($returnData);

        return $resultPage;
    }
}
