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

from pycs.database.Database import Database
from pycs.interfaces.MediaStorage import MediaStorage


class GetProjectResults(View):
    """
    get a list of all files and annotations for a project
    """
    # pylint: disable=arguments-differ
    methods = ['GET']

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

    def dispatch_request(self, project_id: int):
        # get project from database
        project = self.db.project(project_id)
        if project is None:
            return abort(404)

        # map media files to a dict
        storage = MediaStorage(self.db, project.identifier, None)
        files = list(map(lambda f: f.serialize(), storage.files().iter()))

        # return result
        return jsonify(files)
