from flask import render_template
from flask_login import current_user

def render(*args, styles=[], scripts=[], **kwargs):
    # Pour jouer sur les feuilles de style ou les scripts :
    # render('page.html', styles=['-css/form.css', '+css/admin/forms.css'])

    styles_ = [
        'css/global.css',
        'css/navbar.css',
        'css/header.css',
        'css/container.css',
        'css/widgets.css',
        'css/form.css',
        'css/footer.css',
        'css/flash.css',
        'css/table.css',
        'css/pagination.css',
        'css/debugger.css',
        'css/programs.css',
        'css/editor.css',
    ]
    scripts_ = [
        'scripts/trigger_menu.js',
        'scripts/pc-utils.js',
        'scripts/smartphone_patch.js',
        'scripts/gallery.js',
        'scripts/filter.js',
        'scripts/tag_selector.js',
        'scripts/editor.js',
    ]

    # Apply theme from user settings
    theme = current_user.theme if current_user.is_authenticated else ''
    theme = theme if theme else 'default_theme'
    styles_ = [f'css/themes/{theme}.css'] + styles_

    # Corresponding Pygments styles (might want to abstract later)
    code = {
        'default_theme': 'default',
        'FK_dark_theme': 'material',
        'Tituya_v43_theme': 'friendly',
    }
    code = code.get(theme, 'default')
    styles_.append(f'css/pygments-{code}.css')

    for s in styles:
        if s[0] == '-':
            styles_.remove(s[1:])
        if s[0] == '+':
            styles_.append(s[1:])

    for s in scripts:
        if s[0] == '-':
            scripts_.remove(s[1:])
        if s[0] == '+':
            scripts_.append(s[1:])

    return render_template(*args, **kwargs, styles=styles_, scripts=scripts_)
