from django.views.generic import TemplateView, UpdateView, DeleteView, CreateView
from django.urls import reverse_lazy

from ajax_datatable.views import AjaxDatatableView
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

from ....models.replication import ReplicationTarget
from ....mixins.frontend import TitleMixin
from ....mixins.auth import SuperuserRequiredMixin


class AdminReplicationTargetsListView(TitleMixin, SuperuserRequiredMixin, TemplateView):
    title = "Replication Targets"
    template_name = "core/admin/replication_targets_list.html"


class AdminReplicationTargetsListDataTableView(SuperuserRequiredMixin, AjaxDatatableView):
    model = ReplicationTarget
    title = 'Replication Targets'
    initial_order = [["name", "asc"], ]
    length_menu = [[10, 20, 50, 100, -1], [10, 20, 50, 100, 'all']]
    search_values_separator = '+'

    column_defs = [
        AjaxDatatableView.render_row_tools_column_def(),
        {'name': 'id', 'visible': False, },
        {'name': 'name', 'visible': True, },
        {'name': 'address', 'visible': True, },
    ]

    def customize_row(self, row, obj):
        row['edit'] = f"""
            <a href="#" class="btn btn-info btn-edit"
               onclick="editReplicationTarget(this); return false;">
               <i class="fas fa-pencil"></i> Edit
            </a>
            <a href="#" class="btn btn-danger"
               onclick="deleteReplicationTarget(this); return false;">
               <i class="fas fa-trash-can"></i> Delete
            </a>
        """


class AdminReplicationTargetsCreateView(TitleMixin, SuperuserRequiredMixin, CreateView):
    model = ReplicationTarget
    title = "Create Replication Target"
    template_name = "core/admin/replication_targets_new.html"
    fields = ["name", "address", "username"]

    def get_form(self, form_class=None):
        form = super().get_form(form_class)
        form.helper = FormHelper()
        form.helper.add_input(Submit('submit', 'Create', css_class='btn-primary'))
        return form

    def get_success_url(self):
        return reverse_lazy("core:admin_replication_targets")


class AdminReplicationTargetsUpdateView(TitleMixin, SuperuserRequiredMixin, UpdateView):
    model = ReplicationTarget
    title = "Edit Replication Target"
    template_name = "core/admin/replication_targets_edit.html"
    fields = ["name", "directory", "username"]

    def get_form(self, form_class=None):
        form = super().get_form(form_class)
        form.helper = FormHelper()
        form.helper.add_input(Submit('submit', 'Edit', css_class='btn-primary'))
        return form

    def get_success_url(self):
        return reverse_lazy("core:admin_replication_targets")


class AdminReplicationTargetsDeleteView(TitleMixin, SuperuserRequiredMixin, DeleteView):
    model = ReplicationTarget
    title = "Delete Replication Target"
    template_name = "core/admin/replication_targets_delete.html"

    def get_success_url(self):
        return reverse_lazy("core:admin_replication_targets")