"""
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.deadline import EditDeadlineModalForm, NewDeadlineModalForm
from compensation.models import Compensation
from konova.decorators import shared_access_required, default_group_required
from konova.forms.modals import RemoveDeadlineModalForm
from konova.models import Deadline
from konova.utils.message_templates import DEADLINE_REMOVED, DEADLINE_EDITED, DEADLINE_ADDED


@login_required
@default_group_required
@shared_access_required(Compensation, "id")
def deadline_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 = NewDeadlineModalForm(request.POST or None, instance=comp, request=request)
    return form.process_request(
        request,
        msg_success=DEADLINE_ADDED,
        redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
    )


@login_required
@default_group_required
@shared_access_required(Compensation, "id")
def deadline_edit_view(request: HttpRequest, id: str, deadline_id: str):
    """ Renders a form for editing deadlines from a compensation

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

    Returns:

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


@login_required
@default_group_required
@shared_access_required(Compensation, "id")
def deadline_remove_view(request: HttpRequest, id: str, deadline_id: str):
    """ Renders a form for removing deadlines from a compensation

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

    Returns:

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

