"""
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 import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpRequest
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse

from intervention.forms.modals.revocation import NewRevocationModalForm, EditRevocationModalForm, \
    RemoveRevocationModalForm
from intervention.models import Intervention, RevocationDocument, Revocation
from konova.decorators import default_group_required, shared_access_required, login_required_modal
from konova.utils.documents import get_document
from konova.utils.message_templates import REVOCATION_ADDED, DATA_UNSHARED, REVOCATION_EDITED, REVOCATION_REMOVED


@login_required
@default_group_required
@shared_access_required(Intervention, "id")
def new_revocation_view(request: HttpRequest, id: str):
    """ Renders sharing form for an intervention

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

    Returns:

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


@login_required
@default_group_required
def get_revocation_view(request: HttpRequest, doc_id: str):
    """ Returns the revocation document as downloadable file

    Wraps the generic document fetcher function from konova.utils.

    Args:
        request (HttpRequest): The incoming request
        doc_id (str): The document id

    Returns:

    """
    doc = get_object_or_404(RevocationDocument, id=doc_id)
    # File download only possible if related instance is shared with user
    if not doc.instance.legal.intervention.users.filter(id=request.user.id):
        messages.info(
            request,
            DATA_UNSHARED
        )
        return redirect("intervention:detail", id=doc.instance.id)
    return get_document(doc)


@login_required
@default_group_required
@shared_access_required(Intervention, "id")
def edit_revocation_view(request: HttpRequest, id: str, revocation_id: str):
    """ Renders a edit view for a revocation

    Args:
        request (HttpRequest): The incoming request
        id (str): The intervention's id as string
        revocation_id (str): The revocation's id as string

    Returns:

    """
    intervention = get_object_or_404(Intervention, id=id)
    revocation = get_object_or_404(Revocation, id=revocation_id)

    form = EditRevocationModalForm(request.POST or None, request.FILES or None, instance=intervention, revocation=revocation, request=request)
    return form.process_request(
        request,
        REVOCATION_EDITED,
        redirect_url=reverse("intervention:detail", args=(intervention.id,)) + "#related_data"
    )


@login_required_modal
@login_required
@default_group_required
@shared_access_required(Intervention, "id")
def remove_revocation_view(request: HttpRequest, id: str, revocation_id: str):
    """ Renders a remove view for a revocation

    Args:
        request (HttpRequest): The incoming request
        id (str): The intervention's id as string
        revocation_id (str): The revocation's id as string

    Returns:

    """
    intervention = get_object_or_404(Intervention, id=id)
    revocation = get_object_or_404(Revocation, id=revocation_id)

    form = RemoveRevocationModalForm(request.POST or None, instance=intervention, revocation=revocation, request=request)
    return form.process_request(
        request,
        REVOCATION_REMOVED,
        redirect_url=reverse("intervention:detail", args=(intervention.id,)) + "#related_data"
    )

