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

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


class EditLabelParent(View):
    """
    edit a labels name
    """
    # pylint: disable=arguments-differ
    methods = ['POST']

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


    def dispatch_request(self, project_id: int, label_id: int):
        # extract request data
        data = request.get_json(force=True)
        parent = data.get('parent')

        if parent is None:
            abort(400, 'parent argument is missing')

        # find label
        label = Label.query.filter(
            Label.project_id == project_id,
            Label.id == label_id).one_or_none()

        if label is None:
            abort(404)

        label.set_parent(parent)

        # send notification
        self.nm.edit_label(label)

        # return success response
        return make_response()
