# 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/>.
from importlib.resources import files

import cherrypy
from cherrypy.lib.static import serve_file
from cherrypy_foundation.components import StaticMiddleware


@cherrypy.tools.auth(on=False)
@cherrypy.tools.i18n(on=False)
@cherrypy.tools.ratelimit(on=False)
@cherrypy.tools.secure_headers(on=False)
@cherrypy.tools.sessions(on=False)
class Static:

    components = StaticMiddleware()

    @cherrypy.expose
    @cherrypy.tools.staticdir(
        section="", match=r".*(\.js|\.css|\.png|\.woff|\.woff2)$", dir=files('bfh_science_ssp.controller') / 'static'
    )
    def default(self, *args, **kwargs):
        """This entry point is used to serve content of /static/ folder."""
        raise cherrypy.HTTPError(400)

    @cherrypy.tools.staticfile(filename=str(files(__name__) / 'bg.webp'))
    def login_bg(self):
        raise cherrypy.HTTPError(400)

    @cherrypy.expose
    def favicon(self, **kwargs):
        cfg = cherrypy.tree.apps[''].cfg
        filename = cfg.favicon if cfg.favicon else (files('bfh_science_ssp.controller.static') / 'favicon.svg')
        return serve_file(str(filename))

    favicon_ico = favicon
