/**
 * BFH Science Self-Service Portal
 * Copyright (C) 2025-2026 IKUS Software
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses />.
 */

/**
 * Handle nerd mode
 */
jQuery(function() {
    const STORAGE_KEY = 'nerdModeEnabled';
    const $nerdToggle = $('#nerdMode');
    const $body = $('body');

    function applyNerdMode(enabled) {
        // Toogle class
        $nerdToggle.prop('checked', enabled);
        $body.toggleClass('nerd-mode', enabled);
        // Show/hide nerd-column
        $('table[data-ajax]').each(function() {
            if($.fn.DataTable.isDataTable(this)){
                const table = $(this).dataTable().api();
                const columns = table.columns('.nerd-column');
                columns.visible(enabled);
            }
        });
    }

    // Load preference from localStorage
    const saved = localStorage.getItem(STORAGE_KEY);
    const enabled = saved === 'true';

    // Listen to toggle changes
    $nerdToggle.on('change', function() {
        const isChecked = $(this).is(':checked');
        applyNerdMode(isChecked);
        localStorage.setItem(STORAGE_KEY, isChecked);
    });

    // Listen to datatable preInit
    $('table[data-ajax]').on('preInit.dt', function() {
        $(this).dataTable().api().columns('.nerd-column').visible(enabled);
    });

    // Apply initial state
    applyNerdMode(enabled)
});

/*
 * Enable bootstrap tooltips everywhere
 */
jQuery(function() {
    window.appTooltips = new bootstrap.Tooltip(document.body, {
        selector: '[data-bs-toggle="tooltip"]',
        container: 'body',
        delay:  {'show': 500, 'hide': 500},
    });
});