<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_AdvanceSeoManager
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */

namespace Webkul\AdvanceSeoManager\Plugin;

use Magento\Framework\App\Request\Http;
use Magento\Framework\View\Page\Config as PageConfig;
use Magento\Framework\View\Page\Config\Renderer;

class Robots
{

    public function __construct(
        PageConfig $pageConfig,
        Http $request,
        \Webkul\AdvanceSeoManager\Helper\Data $helper,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Framework\UrlInterface $urlInterface
    ) {
        $this->pageConfig = $pageConfig;
        $this->request = $request;
        $this->helperData = $helper;
        $this->scopeConfig = $scopeConfig;
        $this->urlInterface = $urlInterface;
    }

    /**
     * @param Renderer $subject
     */
    public function beforeRenderMetadata(Renderer $subject)
    {
        if ($this->helperData->getIsEnable()) {
            $pages = explode(',', $this->getPagesList());

            foreach ($pages as $page) {
                if ($this->getFullActionName() == $page || $this->getPathInfo() == $page) {
                    $this->pageConfig->setMetadata('robots', $this->getMetaKeywords());
                }
            }
            if ($this->getAdditionalLinks() != '') {
                $additionalLinks = explode(',', $this->getAdditionalLinks());
                foreach ($additionalLinks as $link) {
                    $linkUrl = rtrim($this->getCustomUrl($link), "/ ");
                    $linkUrl = $linkUrl . '.html';
                    $pathInfo = trim($this->getPathInfo(), '/');
                    if ($this->getUrlString() == $linkUrl || $pathInfo == $link) {
                        $this->pageConfig->setMetadata('robots', $this->getMetaKeywords());
                    }
                }
            }
        }
    }

    /**
     * Get full action name
     * @return string
     */
    public function getFullActionName()
    {
        return $this->request->getFullActionName();
    }

    public function getUrlString()
    {
        return $this->request->getUriString();
    }

    /**
     * get path info of page
     *
     * @return string
     */
    public function getPathInfo()
    {
        $path = $this->request->getPathInfo();
        return $path;
    }

    /**
     * is module enable
     *
     * @return boolean
     */
    public function getMetaKeywords()
    {
        return $this->scopeConfig->getValue(
            "seomanager/search_engine_robots/default_robots",
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
    /**
     * get selected pages list
     *
     * @return string
     */
    public function getPagesList()
    {
        return $this->scopeConfig->getValue(
            "seomanager/search_engine_robots/pages",
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }

    /**
     * get Additional links 
     * for disallowing crawler
     * @return void
     */
    public function getAdditionalLinks()
    {
        return $this->scopeConfig->getValue(
            "seomanager/search_engine_robots/additional_link",
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }

    /**
     * get url
     *
     * @param [type] $path
     * @return string
     */
    public function getCustomUrl($path)
    {
        return $this->urlInterface->getUrl($path);
    }
}
