"""Scheduled Activity views."""

import django_filters

from django.conf.urls import url
from django.core.urlresolvers import reverse_lazy
from django.views.generic import CreateView, DetailView, UpdateView
from django_filters.views import FilterView

from .mixins import AJAXRedirectMixin
from ..forms import sch_activity as sch_activity_forms
from ..models.activity import ScheduledActivity


class SchActivityCreateView(CreateView):
    """Create an Scheduled Activity."""

    model = ScheduledActivity
    template_name = 'sch_activity/create.html'
    form_class = sch_activity_forms.ScheduledActivityForm

    def get_success_url(self):
        """Go to the Activity details view."""

        return reverse_lazy('ach_activity_details', args=[self.object.id])


class SchActivityDetailView(DetailView):
    """Scheduled Activity Details"""

    model = ScheduledActivity
    template_name = 'sch_activity/activity.html'


class SchActivityInlineDetailView(DetailView):
    """Display Scheduled Activity details in a table row."""

    model = ScheduledActivity
    template_name = 'sch_activity/inline_details.html'


class SchActivityEditView(AJAXRedirectMixin, UpdateView):
    """Edit Scheduled Activity details."""

    model = ScheduledActivity
    template_name = 'sch_activity/edit.html'
    form_class = sch_activity_forms.ScheduledActivityForm

    def get_success_url(self):
        """Go to the Activity details view."""

        return reverse_lazy('sch_activity_details', args=[self.object.id])


class SchActivityInlineEditView(AJAXRedirectMixin, UpdateView):
    """Display a form in a table row."""

    model = ScheduledActivity
    template_name = 'sch_activity/inline_edit.html'
    fields = [
        'id',
        'short_description',
        'long_description',
        'to_dos',
        'places',
    ]

    def get_success_url(self):
        """Go to the Scheduled Activity details view."""

        return reverse_lazy(
            'sch_activity_inline_details',
            args=[self.object.id]
        )


class SchActivityFilter(django_filters.FilterSet):
    """Filter for Scheduled Activities."""

    all_choices = [('', '---------')]

    id = django_filters.CharFilter(  # pylint:disable=invalid-name
        lookup_expr='icontains',
        help_text='',
    )

    activity__short_description = django_filters.CharFilter(
        lookup_expr='icontains',
        help_text='',
    )

    class Meta:
        model = ScheduledActivity
        fields = ['id', 'activity__short_description']
        order_by = ['id']


class SchActivityListView(AJAXRedirectMixin, FilterView):
    """List Scheduled Activities and provide a filter."""

    model = ScheduledActivity
    template_name = 'sch_activity/filtered_list.html'
    filterset_class = SchActivityFilter


urlpatterns = [
    url(
        '^create$',
        SchActivityCreateView.as_view(),
        name='sch_activity_create',
    ),
    url(
        r'^(?P<pk>\d+)/$',
        SchActivityDetailView.as_view(),
        name='sch_activity_details',
    ),
    url(
        r'^(?P<pk>\d+)/edit$',
        SchActivityEditView.as_view(),
        name='sch_activity_edit'
    ),
    url(
        r'^list/$',
        SchActivityListView.as_view(),
        name='sch_activity_list',
    ),
    url(
        r'^inline/(?P<pk>\d+)/$',
        SchActivityInlineDetailView.as_view(),
        name='sch_activity_inline_details',
    ),
    url(
        r'^inline/(?P<pk>\d+)/edit$',
        SchActivityInlineEditView.as_view(),
        name='sch_activity_inline_edit',
    ),
]
