"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 22.08.22

"""
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views import View

from compensation.forms.modals.deadline import NewDeadlineModalForm, EditDeadlineModalForm
from konova.forms.modals import RemoveDeadlineModalForm
from konova.models import Deadline
from konova.utils.message_templates import DEADLINE_ADDED, DEADLINE_EDITED, DEADLINE_REMOVED


class AbstractNewDeadlineView(View):
    model = None
    redirect_url = None

    class Meta:
        abstract = True

    def get(self, request, id: str):
        """ Renders a form for adding new deadlines

        Args:
            request (HttpRequest): The incoming request
            id (str): The account's id to which the new state will be related

        Returns:

        """
        obj = get_object_or_404(self.model, id=id)
        form = NewDeadlineModalForm(request.POST or None, instance=obj, request=request)
        return form.process_request(
            request,
            msg_success=DEADLINE_ADDED,
            redirect_url=reverse(self.redirect_url, args=(id,)) + "#related_data"
        )

    def post(self, request, id: str):
        return self.get(request, id)


class AbstractEditDeadlineView(View):
    model = None
    redirect_url = None

    class Meta:
        abstract = True

    def get(self, request, id: str, deadline_id: str):
        """ Renders a form for editing deadlines

        Args:
            request (HttpRequest): The incoming request
            id (str): The compensation's id
            deadline_id (str): The deadline's id

        Returns:

        """
        obj = get_object_or_404(self.model, id=id)
        deadline = get_object_or_404(Deadline, id=deadline_id)
        form = EditDeadlineModalForm(request.POST or None, instance=obj, deadline=deadline, request=request)
        return form.process_request(
            request,
            msg_success=DEADLINE_EDITED,
            redirect_url=reverse(self.redirect_url, args=(id,)) + "#related_data"
        )

    def post(self, request, id: str, deadline_id: str):
        return self.get(request, id, deadline_id)


class AbstractRemoveDeadlineView(View):
    model = None
    redirect_url = None

    class Meta:
        abstract = True

    def get(self, request, id: str, deadline_id: str):
        """ Renders a form for removing deadlines

        Args:
            request (HttpRequest): The incoming request
            id (str): The compensation's id
            deadline_id (str): The deadline's id

        Returns:

        """
        obj = get_object_or_404(self.model, id=id)
        deadline = get_object_or_404(Deadline, id=deadline_id)
        form = RemoveDeadlineModalForm(request.POST or None, instance=obj, deadline=deadline, request=request)
        return form.process_request(
            request,
            msg_success=DEADLINE_REMOVED,
            redirect_url=reverse(self.redirect_url, args=(id,)) + "#related_data"
        )

    def post(self, request, id: str, deadline_id: str):
        return self.get(request, id, deadline_id)
