from flask import abort
from flask import make_response
from flask.views import View
from flask.views import request

from pycs.database.File import File
from pycs.frontend.notifications.NotificationManager import NotificationManager


class ResetResults(View):
    """
    removes all results from a given file
    """
    # pylint: disable=arguments-differ
    methods = ['POST']

    def __init__(self, nm: NotificationManager):
        # pylint: disable=invalid-name
        self.nm = nm

    def dispatch_request(self, file_id: int):
        file = File.get_or_404(file_id)

        # extract request data
        data = request.get_json(force=True)

        if not data.get('reset', False):
            abort(400)


        removed = []

        for result in file.results.all():
            removed.append(result.serialize())

        file.results.delete()

        for result in removed:
            self.nm.remove_result(result)

        return make_response()
