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

from pycs.database.Database import Database
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, db: Database, nm: NotificationManager):
        # pylint: disable=invalid-name
        self.db = db
        self.nm = nm

    def dispatch_request(self, file_id: int):
        # extract request data
        data = request.get_json(force=True)

        if 'reset' not in data or data['reset'] is not True:
            abort(400)

        # find file
        file = self.db.file(file_id)
        if file is None:
            return abort(404)

        # get results
        results = file.results()

        # start transaction
        with self.db:
            for result in results:
                result.remove()

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

        return make_response()
