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


class AdminVesselsListView(TitleMixin, SuperuserRequiredMixin, TemplateView):
    title = "Vessels"
    template_name = "core/admin/vessels_list.html"


class AdminVesselsListDataTableView(SuperuserRequiredMixin, AjaxDatatableView):
    model = Vessel
    title = 'Vessels'
    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': 'imo', 'visible': True, },
        {'name': 'mmsi', 'visible': True, },
        {'name': 'edit', 'title': 'Options', 'placeholder': True, 'searchable': False, 'orderable': False, },
    ]

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


class AdminVesselsCreateView(TitleMixin, SuperuserRequiredMixin, CreateView):
    model = Vessel
    title = "Create Vessel"
    template_name = "core/admin/vessels_new.html"
    fields = ["name", "imo", "mmsi"]

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


class AdminVesselsUpdateView(TitleMixin, SuperuserRequiredMixin, UpdateView):
    model = Vessel
    title = "Edit Vessel"
    template_name = "core/admin/vessels_edit.html"
    fields = ["name", "imo", "mmsi"]

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


class AdminVesselsDeleteView(TitleMixin, SuperuserRequiredMixin, DeleteView):
    model = Vessel
    title = "Delete Vessel"
    template_name = "core/admin/vessels_delete.html"

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