"""
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.compensation_action import NewCompensationActionModalForm, \
    EditCompensationActionModalForm, RemoveCompensationActionModalForm
from compensation.models import CompensationAction
from konova.utils.message_templates import COMPENSATION_STATE_ADDED, COMPENSATION_STATE_EDITED, \
    COMPENSATION_STATE_REMOVED


class AbstractCompensationActionView(View):
    model = None
    redirect_url = None
    
    class Meta:
        abstract = True


class AbstractNewCompensationActionView(AbstractCompensationActionView):
    class Meta:
        abstract = True

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

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

        Returns:

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

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

class AbstractEditCompensationActionView(AbstractCompensationActionView):
    class Meta:
        abstract = True

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

        Args:
            request (HttpRequest): The incoming request
            id (str): The object id
            action_id (str): The action's id

        Returns:

        """
        obj = get_object_or_404(self.model, id=id)
        action = get_object_or_404(CompensationAction, id=action_id)
        form = EditCompensationActionModalForm(request.POST or None, instance=obj, action=action, request=request)
        return form.process_request(
            request,
            msg_success=COMPENSATION_STATE_EDITED,
            redirect_url=reverse(self.redirect_url, args=(id,)) + "#related_data"
        )

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

class AbstractRemoveCompensationActionView(AbstractCompensationActionView):
    class Meta:
        abstract = True

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

        Args:
            request (HttpRequest): The incoming request
            id (str): The object id
            action_id (str): The action's id

        Returns:

        """
        obj = get_object_or_404(self.model, id=id)
        action = get_object_or_404(CompensationAction, id=action_id)
        form = RemoveCompensationActionModalForm(request.POST or None, instance=obj, action=action, request=request)
        return form.process_request(
            request,
            msg_success=COMPENSATION_STATE_REMOVED,
            redirect_url=reverse(self.redirect_url, args=(id,)) + "#related_data"
        )

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

