"""
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.state import NewCompensationStateModalForm, EditCompensationStateModalForm, \
    RemoveCompensationStateModalForm
from compensation.models import CompensationState
from konova.utils.message_templates import COMPENSATION_STATE_ADDED, COMPENSATION_STATE_EDITED, \
    COMPENSATION_STATE_REMOVED


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


class AbstractNewCompensationStateView(AbstractCompensationStateView):
    class Meta:
        abstract = True

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

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

        Returns:

        """
        obj = get_object_or_404(self.model, id=id)
        form = NewCompensationStateModalForm(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 AbstractEditCompensationStateView(AbstractCompensationStateView):
    class Meta:
        abstract = True

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

        Args:
            request (HttpRequest): The incoming request
            id (str): The object id
            state_id (str): The state's id

        Returns:

        """
        obj = get_object_or_404(self.model, id=id)
        state = get_object_or_404(CompensationState, id=state_id)
        form = EditCompensationStateModalForm(request.POST or None, instance=obj, state=state, 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, state_id: str):
        return self.get(request, id, state_id)
    

class AbstractRemoveCompensationStateView(AbstractCompensationStateView):
    class Meta:
        abstract = True

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

        Args:
            request (HttpRequest): The incoming request
            id (str): The object id
            state_id (str): The state's id

        Returns:

        """
        obj = get_object_or_404(self.model, id=id)
        state = get_object_or_404(CompensationState, id=state_id)
        form = RemoveCompensationStateModalForm(request.POST or None, instance=obj, state=state, 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, state_id: str):
        return self.get(request, id, state_id)
    
