<?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\Router\Route;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Factory;

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

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

    /**
     * Process search form
     *
     * @param array $searchTemplateFormMatch Form match
     *
     * @return string
     */
    private function _processSearchTemplateForm($searchTemplateFormMatch) {
        $searchTemplateFormHtml = $searchTemplateFormMatch[1];
        $searchTemplateFormHtml = preg_replace('/action="[^"]*?"/', 'action="' . $this->_searchFormData['action'] .'"', $searchTemplateFormHtml);
        $searchTemplateFormHtml = preg_replace_callback('/<\!--search_template_form_input-->([\s\S]+?)<\!--\/search_template_form_input-->/', array(&$this, '_processSearchTemplateFormInput'), $searchTemplateFormHtml);
        return $searchTemplateFormHtml . $this->_searchFormData['totalText'];
    }

    /**
     * Process seach form input
     *
     * @param array $searchTemplateFormInputMatch Form input match
     *
     * @return string
     */
    private function _processSearchTemplateFormInput($searchTemplateFormInputMatch) {
        $searchTemplateFormInputHtml = $searchTemplateFormInputMatch[1];
        $searchTemplateFormInputHtml = preg_replace('/value="[^"]*?"/', 'value="' . $this->_searchFormData['input'] .'"', $searchTemplateFormInputHtml);
        return $searchTemplateFormInputHtml . $this->_searchFormData['hiddenFields'];
    }

    /**
     * Get search data
     *
     * @return array
     */
    public function getSearchFormData() {
        $searchFormData = array(
            'action' => Route::_($this->_templateData['query']->toUri()),
            'input' => $this->_component->escape($this->_templateData['query']->input),
            'totalText' => '',
            'hiddenFields' => '<input type="hidden" name="view" value="search"><input type="hidden" name="option" value="com_finder">'
        );
        if (($this->_templateData['total'] === 0) || ($this->_templateData['total'] === null)) {
            ob_start();
            ?>
            <div style="padding: 30px;" class="u-search-not-found-results u-text u-align-center">
                <div id="search-result-empty" class="com-finder__empty">
                    <h2><?php echo Text::_('COM_FINDER_SEARCH_NO_RESULTS_HEADING'); ?></h2>
                    <?php $multilang = Factory::getApplication()->getLanguageFilter() ? '_MULTILANG' : ''; ?>
                    <p><?php echo Text::sprintf('COM_FINDER_SEARCH_NO_RESULTS_BODY' . $multilang, $searchFormData['input']); ?></p>
                </div>
            </div>
            <?php
            $searchFormData['totalText'] = ob_get_clean();
        }
        return $searchFormData;
    }
}
