<?php

defined('TYPO3_MODE') || die();

(function () {
    if (TYPO3_MODE == 'BE') {
        $modulePrefix = 'extensions-meilisearch-module';
        $svgProvider = \TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider::class;
        /* @var \WapplerSystems\Meilisearch\System\Configuration\ExtensionConfiguration $extensionConfiguration */
        $extensionConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
            \WapplerSystems\Meilisearch\System\Configuration\ExtensionConfiguration::class
        );

        // register all module icons with extensions-meilisearch-module-modulename
        $extIconPath = 'EXT:meilisearch/Resources/Public/Images/Icons/';
        /* @var \TYPO3\CMS\Core\Imaging\IconRegistry $iconRegistry */
        $iconRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Imaging\IconRegistry::class);
        $iconRegistry->registerIcon($modulePrefix . '-main', $svgProvider,
            ['source' => $extIconPath . 'ModuleMeilisearchMain.svg']);
        $iconRegistry->registerIcon($modulePrefix . '-meilisearch-core-optimization', $svgProvider,
            ['source' => $extIconPath . 'ModuleCoreOptimization.svg']);
        $iconRegistry->registerIcon($modulePrefix . '-index-administration', $svgProvider,
            ['source' => $extIconPath . 'ModuleIndexAdministration.svg']);
        // all connections
        $iconRegistry->registerIcon($modulePrefix . '-initmeilisearchconnections', $svgProvider,
            ['source' => $extIconPath . 'InitMeilisearchConnections.svg']);
        // single connection - context menu
        $iconRegistry->registerIcon($modulePrefix . '-initmeilisearchconnection', $svgProvider,
            ['source' => $extIconPath . 'InitMeilisearchConnection.svg']);
        // register plugin icon
        $iconRegistry->registerIcon('extensions-meilisearch-plugin-contentelement', $svgProvider,
            ['source' => $extIconPath . 'ContentElement.svg']);

        // Add Main module "APACHE SOLR".
        // Acces to a main module is implicit, as soon as a user has access to at least one of its submodules. To make it possible, main module must be registered in that way and without any Actions!
        \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule(
            'searchbackend',
            '',
            '',
            null,
            [
                'name' => 'searchbackend',
                'labels' => 'LLL:EXT:meilisearch/Resources/Private/Language/locallang_mod.xlf',
                'iconIdentifier' => 'extensions-meilisearch-module-main'
            ]
        );

        $treeComponentId = 'TYPO3/CMS/Backend/PageTree/PageTreeElement';

        \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerModule(
            'WapplerSystems.meilisearch',
            'searchbackend',
            'Info',
            '',
            [
                'Backend\\Search\\InfoModule' => 'index, switchSite, switchCore, documentsDetails',
            ],
            [
                'access' => 'user,group',
                'icon' => 'EXT:meilisearch/Resources/Public/Images/Icons/ModuleInfo.svg',
                'labels' => 'LLL:EXT:meilisearch/Resources/Private/Language/locallang_mod_info.xlf',
                'navigationComponentId' => $treeComponentId
            ]
        );


        \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerModule(
            'WapplerSystems.meilisearch',
            'searchbackend',
            'IndexQueue',
            '',
            [
                'Backend\\Search\\IndexQueueModule' => 'index, initializeIndexQueue, requeueDocument, resetLogErrors, showError, doIndexingRun, switchSite'
            ],
            [
                'access' => 'user,group',
                'icon' => 'EXT:meilisearch/Resources/Public/Images/Icons/ModuleIndexQueue.svg',
                'labels' => 'LLL:EXT:meilisearch/Resources/Private/Language/locallang_mod_indexqueue.xlf',
                'navigationComponentId' => $treeComponentId
            ]
        );

        \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerModule(
            'WapplerSystems.meilisearch',
            'searchbackend',
            'IndexAdministration',
            '',
            [
                'Backend\\Search\\IndexAdministrationModule' => 'index, emptyIndex, clearIndexQueue, reloadIndexConfiguration, switchSite'
            ],
            [
                'access' => 'user,group',
                'icon' => 'EXT:meilisearch/Resources/Public/Images/Icons/ModuleIndexAdministration.svg',
                'labels' => 'LLL:EXT:meilisearch/Resources/Private/Language/locallang_mod_indexadmin.xlf',
                'navigationComponentId' => $treeComponentId
            ]
        );

        // registering reports
        $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']['tx_reports']['status']['providers']['meilisearch'] = [
            \WapplerSystems\Meilisearch\Report\SiteHandlingStatus::class,
            \WapplerSystems\Meilisearch\Report\SchemaStatus::class,
            \WapplerSystems\Meilisearch\Report\MeilisearchConfigStatus::class,
            \WapplerSystems\Meilisearch\Report\MeilisearchConfigurationStatus::class,
            \WapplerSystems\Meilisearch\Report\MeilisearchStatus::class,
            \WapplerSystems\Meilisearch\Report\MeilisearchVersionStatus::class,
            \WapplerSystems\Meilisearch\Report\AccessFilterPluginInstalledStatus::class,
            \WapplerSystems\Meilisearch\Report\AllowUrlFOpenStatus::class,
            \WapplerSystems\Meilisearch\Report\FilterVarStatus::class
        ];
    }

    if ((TYPO3_MODE === 'BE') || (TYPO3_MODE === 'FE' && isset($_POST['TSFE_EDIT']))) {
        // the order of registering the garbage collector and the record monitor is important!
        // for certain scenarios items must be removed by GC first, and then be re-added to to Index Queue

        // hooking into TCE Main to monitor record updates that may require deleting documents from the index
        $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][] = \WapplerSystems\Meilisearch\GarbageCollector::class;
        $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = \WapplerSystems\Meilisearch\GarbageCollector::class;

        // hooking into TCE Main to monitor record updates that may require reindexing by the index queue
        $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][] = \WapplerSystems\Meilisearch\IndexQueue\RecordMonitor::class;
        $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = \WapplerSystems\Meilisearch\IndexQueue\RecordMonitor::class;
    }
})();


# ----- # ----- # ----- # ----- # ----- # ----- # ----- # ----- # ----- #

$isComposerMode = defined('TYPO3_COMPOSER_MODE') && TYPO3_COMPOSER_MODE;
if (!$isComposerMode) {
    // we load the autoloader for our libraries
    $dir = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('meilisearch');
    require $dir . '/Resources/Private/Php/ComposerLibraries/vendor/autoload.php';
}