from flask import jsonify
from flask import request
from flask.views import View

from pycs.database.File import File


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


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


        with_annotations = request.args.get("only_with_annotations")

        kwargs = dict(with_annotations=None)

        if with_annotations is not None:
            kwargs["with_annotations"] = with_annotations == "1"

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

        # return data
        return jsonify(result)
