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


@login_required
@default_group_required
@shared_access_required(Compensation, "id")
def state_new_view(request: HttpRequest, id: str):
    """ Renders a form for adding new states 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 = NewCompensationStateModalForm(request.POST or None, instance=comp, request=request)
    return form.process_request(
        request,
        msg_success=COMPENSATION_STATE_ADDED,
        redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
    )


@login_required
@default_group_required
@shared_access_required(Compensation, "id")
def state_remove_view(request: HttpRequest, id: str, state_id: str):
    """ Renders a form for removing a compensation state

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

    Returns:

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


@login_required
@default_group_required
@shared_access_required(Compensation, "id")
def state_edit_view(request: HttpRequest, id: str, state_id: str):
    """ Renders a form for editing a compensation state

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

    Returns:

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

