<?php
/**
 * @package         Advanced Module Manager
 * @version         9.5.6
 * 
 * @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\AdvancedModules;

defined('_JEXEC') or die;

use Joomla\CMS\Factory as JFactory;
use Joomla\CMS\Language\Text as JText;
use Joomla\CMS\Router\Route as JRoute;
use RegularLabs\Library\Document as RL_Document;
use RegularLabs\Library\Language as RL_Language;
use RegularLabs\Library\RegEx as RL_RegEx;

class Document
{
    public static function removeAssignmentsTabFromMenuItems(&$html)
    {
        $input = JFactory::getApplication()->input;

        if ( ! RL_Document::isAdmin(true)
            || $input->get('option', '') !== 'com_menus'
            || $input->get('view', '') !== 'item'
            || $input->getInt('client_id') !== 0
        )
        {

            return;
        }

        $parts = explode('<joomla-tab-element id="modules"', $html, 2);

        if (count($parts) !== 2)
        {
            return;
        }

        $last_parts = explode('</joomla-tab-element>', $parts[1], 2);

        if (count($last_parts) !== 2)
        {
            return;
        }

        $html = $parts[0] . $last_parts[1];
    }

    /**
     * Replace links to com_modules with com_advancedmodules
     */
    public static function replaceLinks(&$string)
    {
        if (RL_Document::isClient('administrator')
            && JFactory::getApplication()->input->get('option', '') == 'com_modules'
        )
        {
            self::replaceLinksInCoreModuleManager();

            return;
        }

        // Replace remaining links in modules in admin and frontend
        self::replaceRemainingLinks($string);
    }

    private static function replaceLinksInCoreModuleManager()
    {
        RL_Language::load('com_advancedmodules');

        $body = JFactory::getApplication()->getBody();

        $url = 'index.php?option=com_advancedmodules';

        if (JFactory::getApplication()->input->get('view', '') == 'module')
        {
            $url .= '&task=module.edit&id=' . (int) JFactory::getApplication()->input->getInt('id');
        }

        $link = '<a style="float:right;" href="' . JRoute::_($url) . '">' . JText::_('AMM_SWITCH_TO_ADVANCED_MODULE_MANAGER') . '</a><div style="clear:both;"></div>';
        $body = RL_RegEx::replace('(</div>\s*</form>\s*(<\!--.*?-->\s*)*</div>)', $link . '\1', $body);

        JFactory::getApplication()->setBody($body);
    }

    private static function replaceRemainingLinks(&$string)
    {
        if (strpos($string, 'com_modules') === false)
        {
            return;
        }

        $string = RL_RegEx::replace(
            '((["\'])[^\s"\'%]*\?option=com_)(modules(\2|[^a-z-].*?\2))',
            '\1advanced\3',
            $string
        );

        $string = str_replace(
            [
                '?option=com_advancedmodules&force=1',
                '?option=com_advancedmodules&amp;force=1',
            ],
            '?option=com_modules',
            $string
        );
    }
}
