"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 15.08.22

"""
from django import forms
from django.utils.translation import gettext_lazy as _

from konova.forms.modals.base_form import BaseModalForm
from konova.models import BaseObject


class RemoveModalForm(BaseModalForm):
    """ Generic removing modal form

    Can be used for anything, where removing shall be confirmed by the user a second time.

    """
    confirm = forms.BooleanField(
        label=_("Confirm"),
        label_suffix=_(""),
        widget=forms.CheckboxInput(),
        required=True,
    )
    label_input_ratio = (2, 10)

    def __init__(self, *args, **kwargs):
        self.template = "modal/modal_form.html"
        super().__init__(*args, **kwargs)
        self.form_title = _("Remove")
        self.form_caption = _("Are you sure?")
        # Disable automatic w-100 setting for this type of modal form. Looks kinda strange
        self.fields["confirm"].widget.attrs["class"] = ""

    def save(self):
        if isinstance(self.instance, BaseObject):
            self.instance.mark_as_deleted(self.user)
        else:
            # If the class does not provide restorable delete functionality, we must delete the entry finally
            self.instance.delete()


class RemoveDeadlineModalForm(RemoveModalForm):
    """ Removing modal form for deadlines

    Can be used for anything, where removing shall be confirmed by the user a second time.

    """
    deadline = None

    def __init__(self, *args, **kwargs):
        deadline = kwargs.pop("deadline", None)
        self.deadline = deadline
        super().__init__(*args, **kwargs)

    def save(self):
        self.instance.remove_deadline(self)