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 ReplicationSource
from ....mixins.frontend import TitleMixin
from ....mixins.auth import SuperuserRequiredMixin


class AdminReplicationSourcesListView(TitleMixin, SuperuserRequiredMixin, TemplateView):
    title = "Replication Sources"
    template_name = "core/admin/replication_sources_list.html"


class AdminReplicationSourcesListDataTableView(SuperuserRequiredMixin, AjaxDatatableView):
    model = ReplicationSource
    title = 'Replication Sources'
    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': 'location', 'visible': True, },
    ]

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


class AdminReplicationSourcesCreateView(TitleMixin, SuperuserRequiredMixin, CreateView):
    model = ReplicationSource
    title = "Create Replication Source"
    template_name = "core/admin/replication_sources_new.html"
    fields = ["name", "location"]

    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_sources")


class AdminReplicationSourcesUpdateView(TitleMixin, SuperuserRequiredMixin, UpdateView):
    model = ReplicationSource
    title = "Edit Replication Source"
    template_name = "core/admin/replication_sources_edit.html"
    fields = ["name", "location"]

    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_sources")


class AdminReplicationSourcesDeleteView(TitleMixin, SuperuserRequiredMixin, DeleteView):
    model = ReplicationSource
    title = "Delete Replication Source"
    template_name = "core/admin/replication_sources_delete.html"

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