"""
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.db.models import Sum
from django.http import HttpRequest, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from ema.forms import NewEmaForm, EditEmaForm
from ema.models import Ema
from ema.tables import EmaTable
from konova.contexts import BaseContext
from konova.decorators import shared_access_required, conservation_office_group_required, login_required_modal
from konova.forms import SimpleGeomForm
from konova.forms.modals import RemoveModalForm
from konova.settings import DEFAULT_GROUP, ZB_GROUP, ETS_GROUP
from konova.sub_settings.context_settings import TAB_TITLE_IDENTIFIER
from konova.utils.message_templates import RECORDED_BLOCKS_EDIT, IDENTIFIER_REPLACED, FORM_INVALID, \
    DO_NOT_FORGET_TO_SHARE
from konova.utils.user_checks import in_group


@login_required
def index_view(request: HttpRequest):
    """ Renders the index view for EMAs

    Args:
        request (HttpRequest): The incoming request

    Returns:

    """
    template = "generic_index.html"
    emas = Ema.objects.filter(
        deleted=None,
    ).order_by(
        "-modified__timestamp"
    )

    table = EmaTable(
        request,
        queryset=emas
    )
    context = {
        "table": table,
        TAB_TITLE_IDENTIFIER: _("EMAs - Overview"),
    }
    context = BaseContext(request, context).context
    return render(request, template, context)


@login_required
@conservation_office_group_required
def new_view(request: HttpRequest):
    """
    Renders a view for a new eco account creation

    Args:
        request (HttpRequest): The incoming request

    Returns:

    """
    template = "ema/form/view.html"
    data_form = NewEmaForm(request.POST or None)
    geom_form = SimpleGeomForm(request.POST or None, read_only=False)
    if request.method == "POST":
        if data_form.is_valid() and geom_form.is_valid():
            generated_identifier = data_form.cleaned_data.get("identifier", None)
            ema = data_form.save(request.user, geom_form)
            if generated_identifier != ema.identifier:
                messages.info(
                    request,
                    IDENTIFIER_REPLACED.format(
                        generated_identifier,
                        ema.identifier
                    )
                )
            messages.success(request, _("EMA {} added").format(ema.identifier))
            return redirect("ema:detail", id=ema.id)
        else:
            messages.error(request, FORM_INVALID, extra_tags="danger",)
    else:
        # For clarification: nothing in this case
        pass
    context = {
        "form": data_form,
        "geom_form": geom_form,
        TAB_TITLE_IDENTIFIER: _("New EMA"),
    }
    context = BaseContext(request, context).context
    return render(request, template, context)


@login_required
@conservation_office_group_required
def new_id_view(request: HttpRequest):
    """ JSON endpoint

    Provides fetching of free identifiers for e.g. AJAX calls

    """
    tmp = Ema()
    identifier = tmp.generate_new_identifier()
    while Ema.objects.filter(identifier=identifier).exists():
        identifier = tmp.generate_new_identifier()
    return JsonResponse(
        data={
            "gen_data": identifier
        }
    )


@login_required
def detail_view(request: HttpRequest, id: str):
    """ Renders the detail view of an EMA

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

    Returns:

    """
    template = "ema/detail/view.html"
    ema = get_object_or_404(Ema, id=id, deleted=None)

    geom_form = SimpleGeomForm(instance=ema)
    parcels = ema.get_underlying_parcels()
    _user = request.user
    is_data_shared = ema.is_shared_with(_user)

    # Order states according to surface
    before_states = ema.before_states.all().order_by("-surface")
    after_states = ema.after_states.all().order_by("-surface")

    # Precalculate logical errors between before- and after-states
    # Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
    sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
    sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
    diff_states = abs(sum_before_states - sum_after_states)

    ema.set_status_messages(request)

    requesting_user_is_only_shared_user = ema.is_only_shared_with(_user)
    if requesting_user_is_only_shared_user:
        messages.info(
            request,
            DO_NOT_FORGET_TO_SHARE
        )

    context = {
        "obj": ema,
        "geom_form": geom_form,
        "parcels": parcels,
        "has_access": is_data_shared,
        "before_states": before_states,
        "after_states": after_states,
        "sum_before_states": sum_before_states,
        "sum_after_states": sum_after_states,
        "diff_states": diff_states,
        "is_default_member": in_group(_user, DEFAULT_GROUP),
        "is_zb_member": in_group(_user, ZB_GROUP),
        "is_ets_member": in_group(_user, ETS_GROUP),
        "LANIS_LINK": ema.get_LANIS_link(),
        TAB_TITLE_IDENTIFIER: f"{ema.identifier} - {ema.title}",
        "has_finished_deadlines": ema.get_finished_deadlines().exists(),
    }
    context = BaseContext(request, context).context
    return render(request, template, context)


@login_required
@conservation_office_group_required
@shared_access_required(Ema, "id")
def edit_view(request: HttpRequest, id: str):
    """
    Renders a view for editing compensations

    Args:
        request (HttpRequest): The incoming request

    Returns:

    """
    template = "compensation/form/view.html"
    # Get object from db
    ema = get_object_or_404(Ema, id=id)
    if ema.is_recorded:
        messages.info(
            request,
            RECORDED_BLOCKS_EDIT
        )
        return redirect("ema:detail", id=id)

    # Create forms, initialize with values from db/from POST request
    data_form = EditEmaForm(request.POST or None, instance=ema)
    geom_form = SimpleGeomForm(request.POST or None, read_only=False, instance=ema)
    if request.method == "POST":
        if data_form.is_valid() and geom_form.is_valid():
            # The data form takes the geom form for processing, as well as the performing user
            ema = data_form.save(request.user, geom_form)
            messages.success(request, _("EMA {} edited").format(ema.identifier))
            return redirect("ema:detail", id=ema.id)
        else:
            messages.error(request, FORM_INVALID, extra_tags="danger",)
    else:
        # For clarification: nothing in this case
        pass
    context = {
        "form": data_form,
        "geom_form": geom_form,
        TAB_TITLE_IDENTIFIER: _("Edit {}").format(ema.identifier),
    }
    context = BaseContext(request, context).context
    return render(request, template, context)


@login_required_modal
@login_required
@conservation_office_group_required
@shared_access_required(Ema, "id")
def remove_view(request: HttpRequest, id: str):
    """ Renders a modal view for removing the EMA

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

    Returns:

    """
    ema = get_object_or_404(Ema, id=id)
    form = RemoveModalForm(request.POST or None, instance=ema, request=request)
    return form.process_request(
        request=request,
        msg_success=_("EMA removed"),
        redirect_url=reverse("ema:index"),
    )

