# Source code of the Ubuntu-it website
# Copyright (C) 2023 shadMod <m.allegro@shadmod.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import flask

from ... import cache
from .utils import GetNews

CACHE_FILE = "news-cache.json"


class News:

    @staticmethod
    def prepare_blueprint(app):
        """
        Prepare a blueprint with the news pages
        """
        bp = flask.Blueprint("news", __name__)

        nw_cl = GetNews(os.path.join(app.instance_path, "news"))

        @bp.route("/")
        @bp.route("/<year>")
        @cache.enable
        def index(year: int = None):
            return flask.render_template(
                "news/index.html",
                news=nw_cl.list_news(year),
            )

        @bp.route("/<year>/<filename>")
        def detail(year: int, filename: str):
            return flask.render_template(
                "news/detail.html",
                news=nw_cl.get_data(f"{filename}.md", year)
            )

        return bp
