<?php

namespace Autogedal\InternalLinks\Controller\Index;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\PageFactory;
use Autogedal\InternalLinks\Model\Config;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\ForwardFactory;

class Index implements HttpGetActionInterface
{
    private $resultPageFactory;
    private $config;
    private $forwardFactory;

    public function __construct(
        PageFactory $resultPageFactory,
        Config $config,
        ForwardFactory $forwardFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->config = $config;
        $this->forwardFactory = $forwardFactory;
    }

    public function execute()
    {
        if (!$this->config->isEnabled()) {
            $resultForward = $this->forwardFactory->create();
            return $resultForward->forward('noroute');
        }

        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__($this->config->getPageTitle()));
        $resultPage->getConfig()->setMetadata('description', __($this->config->getMetaDescription()));
        $resultPage->getConfig()->setMetadata('keywords', __($this->config->getMetaKeywords()));

        return $resultPage;
    }
}
