<?php
/**
 * @package   Nicepage Website Builder
 * @author    Nicepage https://www.nicepage.com
 * @copyright Copyright (c) 2016 - 2019 Nicepage
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
 */

namespace NP\Processor;

defined('_JEXEC') or die;

use Joomla\CMS\Helper\ModuleHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Factory;

class Page404Processor
{
    private $_templateData;
    private $_component;

    /**
     * Page404Processor constructor.
     *
     * @param array $templateData Template data
     */
    public function __construct($templateData = null)
    {
        $this->_templateData = $templateData;
        if ($this->_templateData && isset($this->_templateData['errorComponent'])) {
            $this->_component = $this->_templateData['errorComponent'];
        }
    }

    /**
     * Process page404 template
     *
     * @param string $content Content
     *
     * @return string|string[]|null
     */
    public function process($content) {
        if (!$this->_component) {
            return $content;
        }
        $content = preg_replace_callback('/<\!--text_404-->([\s\S]+?)<\!--\/text_404-->/', array(&$this, '_processTextMessage'), $content);
        return $content;
    }

    /**
     * Process error message
     *
     * @param array $textMsgMatch Text error match
     *
     * @return array|false|string|string[]
     * @throws \Exception
     */
    private function _processTextMessage($textMsgMatch) {
        $textMsgHtml = $textMsgMatch[1];
        $text = $this->_getErrorMsg();
        $textMsgHtml = preg_replace_callback(
            '/<\!--text_404_content-->([\s\S]+?)<\!--\/text_404_content-->/',
            function () use ($text) {
                return $text;
            },
            $textMsgHtml
        );
        $infoMsg = $this->_getInfoMsg();
        return str_replace('[[error_text]]', $textMsgHtml, $infoMsg);
    }

    /**
     * Get error message
     *
     * @return string
     */
    private function _getErrorMsg() {
        return '#' . $this->_component->error->getCode() . '&nbsp;' . htmlspecialchars($this->_component->error->getMessage(), ENT_QUOTES, 'UTF-8');
    }

    /**
     * Get info message
     *
     * @return false|string
     * @throws \Exception
     */
    private function _getInfoMsg() {
        $app = Factory::getApplication();
        ob_start();
        ?>
        <div id="errorboxbody">
            <h2>
                <?php echo Text::_('JERROR_LAYOUT_PAGE_NOT_FOUND'); ?>
            </h2>
            <h3><?php echo Text::_('JERROR_LAYOUT_ERROR_HAS_OCCURRED_WHILE_PROCESSING_YOUR_REQUEST'); ?></h3>
            <p class="u-align-left u-text"><?php echo Text::_('JERROR_LAYOUT_NOT_ABLE_TO_VISIT'); ?></p>
            <ul class="u-align-left u-text">
                <li><?php echo Text::_('JERROR_LAYOUT_AN_OUT_OF_DATE_BOOKMARK_FAVOURITE'); ?></li>
                <li><?php echo Text::_('JERROR_LAYOUT_MIS_TYPED_ADDRESS'); ?></li>
                <li><?php echo Text::_('JERROR_LAYOUT_SEARCH_ENGINE_OUT_OF_DATE_LISTING'); ?></li>
                <li><?php echo Text::_('JERROR_LAYOUT_YOU_HAVE_NO_ACCESS_TO_THIS_PAGE'); ?></li>
            </ul>
            <h3>
                <?php echo Text::_('JERROR_LAYOUT_PLEASE_CONTACT_THE_SYSTEM_ADMINISTRATOR'); ?>
            </h3>
            [[error_text]]
            <?php if ($this->_component->debug) : ?>
                <br/><?php echo htmlspecialchars($this->_component->error->getFile(), ENT_QUOTES, 'UTF-8');?>:<?php echo $this->_component->error->getLine(); ?>
            <?php endif; ?>
            <br />
        </div>
        <?php
        return ob_get_clean();
    }
}
