<?php

use idoit\AddOn\RoutingAwareInterface;
use idoit\Component\Settings\SettingsCollection;
use idoit\Component\Settings\Types\SelectSetting;
use idoit\Event\System\Settings\ExtendTenantSettings;
use idoit\Module\Pro\Model\Language;
use idoit\Module\Pro\SettingPage\AttributeSettings\AttributeSettings;
use idoit\Module\Pro\SettingPage\CategoryFolders\CategoryFolders;
use idoit\Module\Pro\SettingPage\CustomTranslation\CustomTranslation;
use idoit\Module\UserSettings\SettingPage\TfaManagement;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\PhpFileLoader;

/**
 * i-doit
 *
 * Pro module class.
 *
 * @package     i-doit
 * @subpackage  modules
 * @copyright   synetics GmbH
 * @license     http://www.i-doit.com/license
 */
class isys_module_pro extends isys_module implements RoutingAwareInterface
{
    // Define, if this module shall be displayed in the named menus.
    const DISPLAY_IN_MAIN_MENU   = false;
    const DISPLAY_IN_SYSTEM_MENU = false;
    const MAIN_MENU_REWRITE_LINK = false;

    /**
     * Callback function for construction of breadcrumb navigation.
     *
     * @param array $gets
     *
     * @return array|null
     * @throws isys_exception_database
     */
    public function breadcrumb_get(&$gets)
    {
        $dataViewMapping = [
            'attribute-settings' => 'LC__CMDB__TREE__SYSTEM__ATTRIBUTE_SETTINGS',
            'custom-translations' => 'LC__CUSTOM_TRANSLATIONS'
        ];

        if (isset($dataViewMapping[$gets[C__GET__SETTINGS_PAGE]])) {
            return [
                [
                    $this->language->get('LC__MODULE__SYSTEM__TREE__DATA_VIEW') => [
                        C__GET__MODULE_ID     => C__MODULE__SYSTEM,
                        C__GET__MODULE_SUB_ID => C__MODULE__PRO,
                        C__GET__SETTINGS_PAGE => $gets[C__GET__SETTINGS_PAGE]
                    ]
                ],
                [
                    $this->language->get($dataViewMapping[$gets[C__GET__SETTINGS_PAGE]]) => null
                ]
            ];
        }

        return null;
    }

    /**
     * @return void
     */
    public function start()
    {
        $template = isys_application::instance()->container->get('template');
        $database = isys_application::instance()->container->get('database');
        $navMode = (int) ($_POST[C__GET__NAVMODE] ?: $_GET[C__GET__NAVMODE]);

        // Nothing to do
        if (isset($_GET[C__GET__SETTINGS_PAGE])) {
            if ($_GET[C__GET__SETTINGS_PAGE] === 'custom-translations') {
                (new CustomTranslation($template, $database, $this->language))->renderPage($navMode);
            }

            if ($_GET[C__GET__SETTINGS_PAGE] === 'attribute-settings') {
                (new AttributeSettings($template, $database, $this->language))->renderPage($navMode);
            }

            if ($_GET[C__GET__SETTINGS_PAGE] === 'category-folders') {
                (new CategoryFolders($template, $database, $this->language))->renderPage($navMode);
            }

            if (class_exists(TfaManagement::class) && $_GET[C__GET__SETTINGS_PAGE] === 'tfa-management') {
                (new TfaManagement($template, $database, $this->language))->renderPage($navMode);
            }
        }
    }

    /**
     * Initializes the module.
     *
     * @param isys_module_request $p_req
     */
    public function init(isys_module_request $p_req)
    {
        // Nothing to do
    }

    /**
     * @param isys_component_tree $tree
     * @param int                 $parentId
     *
     * @return void
     * @throws Exception
     */
    public static function extendDataViewTree(isys_component_tree $tree, int $parentId): void
    {
        $imageDir = isys_application::instance()->www_path . 'images/axialis/';
        $language = isys_application::instance()->container->get('language');

        if (isys_module_cmdb::getAuth()->is_allowed_to(isys_auth::VIEW, 'attribute_visibility')) {
            $currentCount = $tree->count() + 1;

            $tree->add_node(
                $currentCount,
                $parentId,
                $language->get('LC__CMDB__TREE__SYSTEM__ATTRIBUTE_SETTINGS'),
                isys_helper_link::create_url([
                    C__GET__MODULE_ID     => C__MODULE__SYSTEM,
                    C__GET__MODULE_SUB_ID => C__MODULE__PRO,
                    C__GET__SETTINGS_PAGE => 'attribute-settings'
                ]),
                '',
                "{$imageDir}basic/eye.svg",
                $_GET[C__GET__MODULE_ID] == C__MODULE__SYSTEM && $_GET[C__GET__MODULE_SUB_ID] == C__MODULE__PRO && $_GET[C__GET__SETTINGS_PAGE] === 'attribute-settings'
            );
        }

        if (isys_module_system::getAuth()->is_allowed_to(isys_auth::VIEW, 'custom-translations')) {
            $currentCount = $tree->count() + 1;

            $tree->add_node(
                $currentCount,
                $parentId,
                $language->get('LC__CUSTOM_TRANSLATIONS'),
                isys_helper_link::create_url([
                    C__GET__MODULE_ID     => C__MODULE__SYSTEM,
                    C__GET__MODULE_SUB_ID => C__MODULE__PRO,
                    C__GET__SETTINGS_PAGE => 'custom-translations'
                ]),
                '',
                "{$imageDir}flags-languages/application-language.svg",
                $_GET[C__GET__MODULE_ID] == C__MODULE__SYSTEM && $_GET[C__GET__MODULE_SUB_ID] == C__MODULE__PRO && $_GET[C__GET__SETTINGS_PAGE] === 'custom-translations'
            );
        }

        if (isys_module_system::getAuth()->is_allowed_to(isys_auth::VIEW, 'category-folders')) {
            $currentCount = $tree->count() + 1;

            $tree->add_node(
                $currentCount,
                $parentId,
                $language->get('LC__CATEGORY_FOLDERS'),
                isys_helper_link::create_url([
                    C__GET__MODULE_ID     => C__MODULE__SYSTEM,
                    C__GET__MODULE_SUB_ID => C__MODULE__PRO,
                    C__GET__SETTINGS_PAGE => 'category-folders'
                ]),
                '',
                "{$imageDir}development/control-tree-green.svg",
                $_GET[C__GET__MODULE_ID] == C__MODULE__SYSTEM && $_GET[C__GET__MODULE_SUB_ID] == C__MODULE__PRO && $_GET[C__GET__SETTINGS_PAGE] === 'category-folders'
            );
        }
    }

    /**
     * @return void
     */
    public static function registerRouting(): void
    {
        isys_application::instance()->container->get('routes')
            ->addCollection((new PhpFileLoader(new FileLocator(__DIR__)))->load('config/routes.php'));
    }

    /**
     * @param ExtendTenantSettings $event
     *
     * @return void
     * @throws isys_exception_database
     */
    public static function extendTenantSettings(ExtendTenantSettings $event): void
    {
        $event->addSettingsCollection(
            new SettingsCollection('LC__UNIVERSAL__CMDB', [
                new SelectSetting(
                    'cmdb.gui.remember-cmdb-status',
                    'LC__MYDOIT__SAVE_CMDB_STATUS',
                    '',
                    '0',
                    [
                        '0' => 'LC__UNIVERSAL__NO',
                        '1' => 'LC__UNIVERSAL__YES'
                    ]
                )
            ])
        );

        $languages = Language::instance(isys_application::instance()->container->get('database_system'))->getAll();
        $availableLanguages = [
            C__USE_BROWSER_DEFAULT => 'LC__CMDB__SETTINGS__USER__BROWSER_LANGUAGE'
        ];

        while ($result = $languages->get_row()) {
            if ($result['available']) {
                $availableLanguages[$result['short']] = $result['title'];
            }
        }

        $event->addSettingsCollection(
            new SettingsCollection('LC__LANGUAGE_LANGUAGE', [
                new SelectSetting(
                    'system.default-language',
                    'LC__LANGUAGE_DEFAULT_LANGUAGE',
                    '',
                    'en',
                    $availableLanguages
                )
            ])
        );
    }
}
