from django.shortcuts import render
from django.urls import reverse_lazy, reverse
from django.views.generic.base import TemplateView
from django.views.generic.edit import FormView, CreateView, UpdateView, DeleteView
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView

from charts.models import Chart, Ambulation
from charts.forms import AmbulationForm


from django.db.models import (Exists, OuterRef,
    Prefetch, Subquery, Count, Sum, F, Q, Value)
from django.db.models.functions import Length, Upper

# Entries
class ListView(ListView):
    model = Ambulation
    pk_url_kwarg = 'entry_pk'

class CreateView(CreateView):
    model = Ambulation
    pk_url_kwarg = 'entry_pk'
    template_name = 'charts/entry_create_form.html'
    form_class = AmbulationForm

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['chart'] = Chart.objects.get(pk=self.kwargs['pk'])
        return context

    def form_valid(self, form):
        form.instance.chart = Chart.objects.get(pk=self.kwargs['pk'])
        return super().form_valid(form)

    def get_success_url(self):
        return reverse('chart-detail', kwargs={'pk': self.kwargs['pk']})

class DetailView(DetailView):
    model = Ambulation
    pk_url_kwarg = 'entry_pk'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['chart'] = Chart.objects.get(pk=self.kwargs['pk'])
        return context

class UpdateView(UpdateView):
    model = Ambulation
    pk_url_kwarg = 'entry_pk'
    template_name = 'charts/entry_form.html'
    form_class = AmbulationForm

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['chart'] = Chart.objects.get(pk=self.kwargs['pk'])
        return context

class DeleteView(DeleteView):
    model = Ambulation
    pk_url_kwarg = 'entry_pk'
    
    def get_success_url(self):
        return reverse('chart-detail', kwargs={'pk': self.kwargs['pk']})
