from flask import abort, jsonify
from flask.views import View

from pycs.database.Database import Database


class GetPreviousAndNextFile(View):
    """
    return the previous and the next file
    """
    # pylint: disable=arguments-differ
    methods = ['GET']

    def __init__(self, db: Database):
        # pylint: disable=invalid-name
        self.db = db

    def dispatch_request(self, file_id: int):
        # get file from database
        file = self.db.file(file_id)

        if file is None:
            return abort(404)

        # get previous and next
        result = {
            'previous': file.previous(),
            'next': file.next(),
            'previousInCollection': file.previous_in_collection(),
            'nextInCollection': file.next_in_collection()
        }

        # return data
        return jsonify(result)
