"""
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 ema.forms import NewEmaDocumentModalForm
from ema.models import Ema, EmaDocument
from konova.decorators import shared_access_required, conservation_office_group_required
from konova.forms.modals import EditDocumentModalForm
from konova.utils.documents import get_document, remove_document
from konova.utils.message_templates import DOCUMENT_ADDED, DOCUMENT_EDITED


@login_required
@conservation_office_group_required
@shared_access_required(Ema, "id")
def document_new_view(request: HttpRequest, id: str):
    """ Renders a form for uploading new documents

    Args:
        request (HttpRequest): The incoming request
        id (str): The EMA's id to which the new document will be related
    Returns:

    """
    ema = get_object_or_404(Ema, id=id)
    form = NewEmaDocumentModalForm(request.POST or None, request.FILES or None, instance=ema, request=request)
    return form.process_request(
        request,
        msg_success=DOCUMENT_ADDED,
        redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
    )


@login_required
@conservation_office_group_required
@shared_access_required(Ema, "id")
def get_document_view(request: HttpRequest, id: str, doc_id: str):
    """ Returns the document as downloadable file

    Wraps the generic document fetcher function from konova.utils.

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

    Returns:

    """
    ema = get_object_or_404(Ema, id=id)
    doc = get_object_or_404(EmaDocument, id=doc_id)
    return get_document(doc)


@login_required
@conservation_office_group_required
@shared_access_required(Ema, "id")
def edit_document_view(request: HttpRequest, id: str, doc_id: str):
    """ Removes the document from the database and file system

    Wraps the generic functionality from konova.utils.

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

    Returns:

    """
    ema = get_object_or_404(Ema, id=id)
    doc = get_object_or_404(EmaDocument, id=doc_id)
    form = EditDocumentModalForm(request.POST or None, request.FILES or None, instance=ema, document=doc, request=request)
    return form.process_request(
        request,
        DOCUMENT_EDITED,
        reverse("ema:detail", args=(id,)) + "#related_data"
    )


@login_required
@conservation_office_group_required
@shared_access_required(Ema, "id")
def remove_document_view(request: HttpRequest, id:str, doc_id: str):
    """ Removes the document from the database and file system

    Wraps the generic functionality from konova.utils.

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

    Returns:

    """
    ema = get_object_or_404(Ema, id=id)
    doc = get_object_or_404(EmaDocument, id=doc_id)
    return remove_document(
        request,
        doc
    )

