<?php
/**
 * @package         Quick Index
 * @version         3.4.0PRO
 * 
 * @author          Peter van Westen <info@regularlabs.com>
 * @link            http://regularlabs.com
 * @copyright       Copyright © 2023 Regular Labs All Rights Reserved
 * @license         http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

namespace RegularLabs\Plugin\System\QuickIndex;

defined('_JEXEC') or die;

use RegularLabs\Library\Parameters as RL_Parameters;
use RegularLabs\Library\PluginTag as RL_PluginTag;
use RegularLabs\Library\RegEx as RL_RegEx;

class Params
{
    protected static $params         = null;
    protected static $regex          = null;
    protected static $regex_endtag   = null;
    protected static $regex_noindex  = null;
    protected static $regex_starttag = null;

    public static function get()
    {
        if ( ! is_null(self::$params))
        {
            return self::$params;
        }

        $params = RL_Parameters::getPlugin('quickindex');

        $params->tag = RL_PluginTag::clean($params->tag);

        $params->noindex_classes = explode(',', str_replace(' ', '', $params->noindex_classes));
        $params->title_classes   = explode(',', str_replace(' ', '', $params->title_classes));

        self::$params = $params;

        return self::$params;
    }

    public static function getRegex()
    {
        if ( ! is_null(self::$regex))
        {
            return self::$regex;
        }

        self::$regex =
            '(?:<!-- QI: START -->)?'
            . '(?<tag>' . self::getRegexStartTag() . ')'
            . '(?<text>.*?)'
            . '(?<endtag>'
            // till the following is found:
            // ending {/index} tag
            . self::getRegexEndTag()
            // or a new starting {index} tag
            . '|<!-- QI: START -->'
            // or the end of the string
            . '|$'
            . ')';

        return self::$regex;
    }

    public static function getRegexEndTag()
    {
        if ( ! is_null(self::$regex_endtag))
        {
            return self::$regex_endtag;
        }

        $params = self::get();

        // Tag character start and end
        [$tag_start, $tag_end] = Params::getTagCharacters(true);

        // pre/post tags are all block elements except the divs
        $pre  = RL_PluginTag::getRegexSurroundingTagsPre([], [], ['div']);
        $post = RL_PluginTag::getRegexSurroundingTagsPost([], [], ['div']);

        self::$regex_endtag = $pre . $tag_start
            . '/' . RL_RegEx::quote($params->tag)
            . $tag_end . $post;

        return self::$regex_endtag;
    }

    public static function getRegexNoIndex()
    {
        if ( ! is_null(self::$regex_noindex))
        {
            return self::$regex_noindex;
        }

        $params = self::get();

        // Tag character start and end
        [$tag_start, $tag_end] = Params::getTagCharacters(true);

        $open_tag  = $tag_start . RL_RegEx::quote($params->noindex_tag) . $tag_end;
        $close_tag = $tag_start . '/' . RL_RegEx::quote($params->noindex_tag) . $tag_end;

        self::$regex_noindex =
            '(?:<p>\s*' . $open_tag . '\s*</p>|' . $open_tag . ')'
            . '(.*?)'
            . '(?:<p>\s*' . $close_tag . '\s*</p>|' . $close_tag . ')';

        return self::$regex_noindex;
    }

    public static function getRegexStartTag()
    {
        if ( ! is_null(self::$regex_starttag))
        {
            return self::$regex_starttag;
        }

        $params = self::get();

        // pre/post tags are all block elements except the divs
        $pre  = RL_PluginTag::getRegexSurroundingTagsPre([], [], ['div']);
        $post = RL_PluginTag::getRegexSurroundingTagsPost([], [], ['div']);

        [$tag_start, $tag_end] = Params::getTagCharacters();

        $inside_tag = RL_PluginTag::getRegexInsideTag($tag_start, $tag_end);
        $spaces     = RL_PluginTag::getRegexSpaces();

        [$tag_start, $tag_end] = Params::getTagCharacters(true);

        self::$regex_starttag = $pre . $tag_start
            . RL_RegEx::quote($params->tag) . '(?<settings>(?:(?:' . $spaces . '|<)' . $inside_tag . ')?)'
            . $tag_end . $post;

        return self::$regex_starttag;
    }

    public static function getTagCharacters($regex_quote = false)
    {
        $params = self::get();

        if ( ! isset($params->tag_character_start))
        {
            self::setTagCharacters();
        }

        if ($regex_quote)
        {
            return [
                RL_RegEx::quote($params->tag_character_start),
                RL_RegEx::quote($params->tag_character_end),
            ];
        }

        return [$params->tag_character_start, $params->tag_character_end];
    }

    public static function getTags($only_start_tags = false, $include_noindex = false)
    {
        $params = self::get();

        [$tag_start, $tag_end] = self::getTagCharacters();

        $tags = [
            [
                $tag_start . $params->tag,
            ],
            [
                $tag_start . '/' . $params->tag . $tag_end,
            ],
        ];

        if ($include_noindex)
        {
            $tags[0][] = $tag_start . $params->noindex_tag;
            $tags[1][] = $tag_start . '/' . $params->noindex_tag . $tag_end;
        }

        return $only_start_tags ? $tags[0] : $tags;
    }

    public static function setAutoIndex()
    {
        self::get()->auto_index = true;
    }

    public static function setTagCharacters()
    {
        $params = self::get();

        [self::$params->tag_character_start, self::$params->tag_character_end] = explode('.', $params->tag_characters);
    }
}
