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

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

from compensation.forms.modals.compensation_action import RemoveCompensationActionModalForm, \
    EditCompensationActionModalForm, NewCompensationActionModalForm
from compensation.models import Compensation, CompensationAction
from konova.decorators import shared_access_required, default_group_required
from konova.utils.message_templates import COMPENSATION_ACTION_REMOVED, COMPENSATION_ACTION_EDITED, \
    COMPENSATION_ACTION_ADDED


@login_required
@default_group_required
@shared_access_required(Compensation, "id")
def action_new_view(request: HttpRequest, id: str):
    """ Renders a form for adding new actions for a compensation

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

    Returns:

    """
    comp = get_object_or_404(Compensation, id=id)
    form = NewCompensationActionModalForm(request.POST or None, instance=comp, request=request)
    return form.process_request(
        request,
        msg_success=COMPENSATION_ACTION_ADDED,
        redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
    )


@login_required
@default_group_required
@shared_access_required(Compensation, "id")
def action_edit_view(request: HttpRequest, id: str, action_id: str):
    """ Renders a form for editing actions for a compensation

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

    Returns:

    """
    comp = get_object_or_404(Compensation, id=id)
    action = get_object_or_404(CompensationAction, id=action_id)
    form = EditCompensationActionModalForm(request.POST or None, instance=comp, action=action, request=request)
    return form.process_request(
        request,
        msg_success=COMPENSATION_ACTION_EDITED,
        redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
    )


@login_required
@default_group_required
@shared_access_required(Compensation, "id")
def action_remove_view(request: HttpRequest, id: str, action_id: str):
    """ Renders a form for removing a compensation action

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

    Returns:

    """
    comp = get_object_or_404(Compensation, id=id)
    action = get_object_or_404(CompensationAction, id=action_id)
    form = RemoveCompensationActionModalForm(request.POST or None, instance=comp, action=action, request=request)
    return form.process_request(
        request,
        msg_success=COMPENSATION_ACTION_REMOVED,
        redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
    )

