import requests
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse, Http404
from django.shortcuts import render
from django.views.generic import ListView, UpdateView, DetailView, FormView, CreateView
from django.core.exceptions import PermissionDenied
from studenteval.list_pdf_export import render_to_pdf

from studenteval.models import cl_Student_eval
from studenteval.filters import cl_Student_eval_Filter

def show_pdf(request, uuid):
    # Votre logique pour l'action 1 ici
    return HttpResponse("Action 1 a été effectuée pour l'objet avec l'ID : {}".format(uuid))


def is_member(user, group):
    return user.groups.filter(name=group).exists()
class student_eval_detail(LoginRequiredMixin,DetailView):
    model =cl_Student_eval
    login_url = ""

    def dispatch(self, request, *args, **kwargs):
        user_obj = self.request.user

        author_id = self.get_object().Author.id if self.get_object().Author is not None else 0
        student_id = self.get_object().Student.id if self.get_object().Student is not None else 0

        if not user_obj.id == author_id and not user_obj.id == student_id and not is_member(user_obj,"FI-Encadrants"):
            raise PermissionDenied

        return super().dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["dtDate__range__gte"] = self.request.GET.get('dtDate__range__gte')
        context["dtDate__range__lte"] = self.request.GET.get('dtDate__range__lte')

        self.object.get_all_evals_for_student()
        self.object.get_mirror_eval(self.object.a_evals)


        return context
class student_eval_list(LoginRequiredMixin,ListView):
    model = cl_Student_eval
    login_url = ""



    def get_queryset(self):
        print(self.kwargs)
        object = cl_Student_eval.objects.get(pk= self.kwargs['pk'])
        return cl_Student_eval.objects.filter(Student=object.Student)



    def dispatch(self, request, *args, **kwargs):
        user_obj = self.request.user

        #self.object_list.filter(Student=requests.GET['Student'])


        '''    
        author_id = self.get_object().Author.id if self.get_object().Author is not None else 0
        student_id = self.get_object().Student.id if self.get_object().Student is not None else 0

        if not user_obj.id == author_id and not user_obj.id == student_id and not is_member(user_obj,"FI-Encadrants"):
            raise PermissionDenied
        '''

        return super().dispatch(request, *args, **kwargs)
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        context['my_objects'] =[]

        print(self.get_queryset())
        for object in self.get_queryset():
            object.get_all_evals_for_student()
            object.get_mirror_eval(object.a_evals)
            print(object.mirrored)
            if object.mirrored and int(object.nEval_Mode) == 1:
                continue
            else:
                context['my_objects'].append(object)

        context["uuid"] = object.uuid


        return context

def export_pdf_all(request,pk):
    '''
    if not "dtDate__range__gte" in request.GET.keys():
        start = datetime.today().replace(day=1,hour=0, minute=0)
    else:
        start = request.GET['dtDate__range__gte']
    if not "dtDate__range__lte" in request.GET.keys():
        end = datetime.now()
    else:
        end = request.GET['dtDate__range__lte']
    '''
    template_name = "studenteval/all_pdf_template.html"
    records = cl_Student_eval.objects.filter(Student=cl_Student_eval.objects.get(pk=pk).Student)
    objects=[]
    for record in records:
        record.get_all_evals_for_student()
        record.get_mirror_eval(record.a_evals)
        if record.mirrored and int(record.nEval_Mode) == 1:
            continue
        objects.append(record)
    return render_to_pdf(template_name,{"objects": objects, })

def export_pdf(request,pk):
    '''
    if not "dtDate__range__gte" in request.GET.keys():
        start = datetime.today().replace(day=1,hour=0, minute=0)
    else:
        start = request.GET['dtDate__range__gte']
    if not "dtDate__range__lte" in request.GET.keys():
        end = datetime.now()
    else:
        end = request.GET['dtDate__range__lte']
    '''
    template_name = "studenteval/all_pdf_template.html"
    objects = []
    record = cl_Student_eval.objects.get(pk=pk)
    record.get_all_evals_for_student()
    record.get_mirror_eval(record.a_evals)
    objects.append(record)

    return render_to_pdf(template_name,{"objects": objects, })