"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 09.08.21

"""
from django.urls import reverse
from django.contrib.auth.decorators import login_required
from django.http import HttpRequest
from django.shortcuts import get_object_or_404

from compensation.forms.modals.payment import NewPaymentForm, RemovePaymentModalForm, EditPaymentModalForm
from compensation.models import Payment
from intervention.models import Intervention
from konova.decorators import default_group_required, shared_access_required
from konova.utils.message_templates import PAYMENT_ADDED, PAYMENT_REMOVED, PAYMENT_EDITED


@login_required
@default_group_required
@shared_access_required(Intervention, "id")
def new_payment_view(request: HttpRequest, id: str):
    """ Renders a modal view for adding new payments

    Args:
        request (HttpRequest): The incoming request
        id (str): The intervention's id for which a new payment shall be added

    Returns:

    """
    intervention = get_object_or_404(Intervention, id=id)
    form = NewPaymentForm(request.POST or None, instance=intervention, request=request)
    return form.process_request(
        request,
        msg_success=PAYMENT_ADDED,
        redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
    )


@login_required
@default_group_required
@shared_access_required(Intervention, "id")
def payment_remove_view(request: HttpRequest, id: str, payment_id: str):
    """ Renders a modal view for removing payments

    Args:
        request (HttpRequest): The incoming request
        id (str): The intervention's id
        payment_id (str): The payment's id

    Returns:

    """
    intervention = get_object_or_404(Intervention, id=id)
    payment = get_object_or_404(Payment, id=payment_id)
    form = RemovePaymentModalForm(request.POST or None, instance=intervention, payment=payment, request=request)
    return form.process_request(
        request=request,
        msg_success=PAYMENT_REMOVED,
        redirect_url=reverse("intervention:detail", args=(payment.intervention_id,)) + "#related_data"
    )


@login_required
@default_group_required
@shared_access_required(Intervention, "id")
def payment_edit_view(request: HttpRequest, id: str, payment_id: str):
    """ Renders a modal view for editing payments

    Args:
        request (HttpRequest): The incoming request
        id (str): The intervention's id
        payment_id (str): The payment's id

    Returns:

    """
    intervention = get_object_or_404(Intervention, id=id)
    payment = get_object_or_404(Payment, id=payment_id)
    form = EditPaymentModalForm(request.POST or None, instance=intervention, payment=payment, request=request)
    return form.process_request(
        request=request,
        msg_success=PAYMENT_EDITED,
        redirect_url=reverse("intervention:detail", args=(payment.intervention_id,)) + "#related_data"
    )

