<?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
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory as JFactory;
use Joomla\CMS\Form\Form as JForm;
use Joomla\CMS\Language\Text as JText;
use RegularLabs\Library\Document as RL_Document;
use RegularLabs\Library\Extension as RL_Extension;
use RegularLabs\Library\Protect as RL_Protect;
use RegularLabs\Library\RegEx as RL_RegEx;
use RegularLabs\Library\SystemPlugin as RL_SystemPlugin;
use RegularLabs\Plugin\System\AdvancedModules\Document;
use RegularLabs\Plugin\System\AdvancedModules\Helper;
use RegularLabs\Plugin\System\AdvancedModules\ModuleForm;
use RegularLabs\Plugin\System\AdvancedModules\Params;

if ( ! is_file(JPATH_LIBRARIES . '/regularlabs/regularlabs.xml')
    || ! is_file(JPATH_LIBRARIES . '/regularlabs/src/SystemPlugin.php')
    || ! is_file(JPATH_LIBRARIES . '/regularlabs/src/DownloadKey.php')
)
{
    JFactory::getLanguage()->load('plg_system_advancedmodules', __DIR__);
    JFactory::getApplication()->enqueueMessage(
        JText::sprintf('AMM_EXTENSION_CAN_NOT_FUNCTION', JText::_('ADVANCEDMODULEMANAGER'))
        . ' ' . JText::_('AMM_REGULAR_LABS_LIBRARY_NOT_INSTALLED'),
        'error'
    );

    return;
}

if ( ! RL_Document::isJoomlaVersion(4, 'ADVANCEDMODULEMANAGER'))
{
    RL_Extension::disable('advancedmodules', 'plugin');

    RL_Document::adminError(
        JText::sprintf('RL_PLUGIN_HAS_BEEN_DISABLED', JText::_('ADVANCEDMODULEMANAGER'))
    );

    return;
}

if (true)
{
    class PlgSystemAdvancedModules extends RL_SystemPlugin
    {
        public $_title              = 'ADVANCEDMODULEMANAGER';
        public $_lang_prefix        = 'AMM';
        public $_page_types         = ['html'];
        public $_enable_in_admin    = true;
        public $_can_disable_by_url = false;
        public $_jversion           = 4;

        protected function extraChecks()
        {
            if ( ! RL_Protect::isComponentInstalled('advancedmodules'))
            {
                return false;
            }

            return true;
            //return parent::extraChecks();
        }

        /**
         * @param JForm    $form The form
         * @param stdClass $data The data
         *
         * @return  bool
         */
        protected function handleOnContentPrepareForm(JForm $form, $data)
        {
            if ( ! RL_Document::isHtml())
            {
                return true;
            }

            ModuleForm::cleanup($form);

            return true;
        }

        /**
         * @param string $html
         *
         * @return  bool
         */
        protected function changeFinalHtmlOutput(&$html)
        {
            Document::removeAssignmentsTabFromMenuItems($html);
            Document::replaceLinks($html);

            return true;
        }

        protected function cleanFinalHtmlOutput(&$html)
        {
        }

        public function onPrepareModuleList(&$modules)
        {
            if (RL_Document::isAdmin())
            {
                return;
            }

            Helper::prepareModuleList($modules);
        }

        public function onRenderModule(&$module)
        {
            if (RL_Document::isAdmin())
            {
                return;
            }

            Helper::renderModule($module);
        }
    }
}
