from django.contrib import admin
from django.db import transaction
from .models import *
from core.models import Professor, MateriaCorrente
from .forms import *
from cfar.utils import *
from django.contrib import messages

class JustificativaAdmin(admin.ModelAdmin):
	form = JustificativaForm
	list_display = ('user', 'status', 'url')
	actions = [aceitar_justificativa, negar_justificativa]
	
	def get_actions(self, request):
		actions = super().get_actions(request)
		user = Professor.objects.get(pk=get_user_id(request))
		
		if user.funcao_docente == '0':
			if 'aceitar_justificativa' in actions:
				del actions['aceitar_justificativa']
			if 'negar_justificativa' in actions:
				del actions['negar_justificativa']
		return actions

	def formfield_for_manytomany(self, db_field, request, **kwargs):
		if db_field.name == "faltas":
			temp_faltas = Falta.objects.filter(materia_corrente__professor_id=get_user_id(request), justificativas__isnull=True).exclude(status=0).exclude(situacao=0)
			#final_faltas = []
			#.exclude(justificativas=True)
			#for i in temp_faltas:
			#	if i.justificativas.count() != 0:
			#		temp_faltas.pop(i)
			kwargs["queryset"] = temp_faltas
			print(kwargs["queryset"])
		return super(JustificativaAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)

	class Meta:
		model = Justificativa

	def save_model(self, request, obj, form, change):
		if not change:
			obj.user_id = get_user_id(request)
		obj.save()

class AntecipacaoAdmin(admin.ModelAdmin):
	form = AntecipacaoForm
	list_display = ('agendamento', 'url', 'status')
	actions = [aceitar_antecipacao, negar_antecipacao]

	class Meta:
		model = Antecipacao

	def get_actions(self, request):
		actions = super().get_actions(request)
        
		user = Professor.objects.get(pk=get_user_id(request))

		if user.funcao_docente == '0':
			if 'aceitar_antecipacao' in actions:
				del actions['aceitar_antecipacao']
			if 'negar_antecipacao' in actions:
				del actions['negar_antecipacao']
		return actions

	def formfield_for_foreignkey(self, db_field, request, **kwargs):
		user = Professor.objects.get(pk=get_user_id(request))
		if db_field.name == "agendamento":
			kwargs["queryset"] = Agendamento.objects.filter(materia_corrente__professor=user, status=True, tipo=1, antecipacao=None)
		return super(AntecipacaoAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

	def get_queryset(self, request):
		qs = super(AntecipacaoAdmin, self).get_queryset(request)
		user = Professor.objects.get(pk=get_user_id(request))
		current_qs = qs.filter(agendamento__user_id=user.pk)
		
		if user.funcao_docente == '2':
			current_qs = qs.filter(agendamento__materia_corrente__professor__departamento__chefe__id=user.pk)
			print(current_qs)
		elif user.funcao_docente == '1':
			current_qs = qs.filter(agendamento__materia_corrente__materia__turma__curso__coordenador__id=user.pk)
			print(current_qs)
		
		return current_qs

class ReposicaoAdmin(admin.ModelAdmin):
	form = ReposicaoForm
	list_display = ('agendamento', 'url', 'status')
	actions = [aceitar_reposicao, negar_reposicao]

	class Meta:
		model = Reposicao

	def get_actions(self, request):
		actions = super().get_actions(request)
        
		user = Professor.objects.get(pk=get_user_id(request))

		if user.funcao_docente == '0':
			if 'aceitar_reposicao' in actions:
				del actions['aceitar_reposicao']
			if 'negar_reposicao' in actions:
				del actions['negar_reposicao']
		return actions

	def formfield_for_foreignkey(self, db_field, request, **kwargs):
		user = Professor.objects.get(pk=get_user_id(request))
		if db_field.name == "agendamento":
			kwargs["queryset"] = Agendamento.objects.filter(materia_corrente__professor=user, status=True, tipo=0, reposicao=None)
		return super(ReposicaoAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

	def get_queryset(self, request):
		qs = super(ReposicaoAdmin, self).get_queryset(request)
		user = Professor.objects.get(pk=get_user_id(request))
		current_qs = qs.filter(agendamento__user_id=user.pk)
		
		if user.funcao_docente == '2':
			current_qs = qs.filter(agendamento__materia_corrente__professor__departamento__chefe__id=user.pk)
			print(current_qs)
		elif user.funcao_docente == '1':
			current_qs = qs.filter(agendamento__materia_corrente__materia__turma__curso__coordenador__id=user.pk)
			print(current_qs)
		
		return current_qs

class AgendamentoAdmin(admin.ModelAdmin):
	form = AgendamentoForm
	list_display = ('user', 'dia', 'status', 'tipo', 'materia_corrente')
	actions = [aceitar_agendamento, negar_agendamento]

	class Meta:
		model = Agendamento

	def get_actions(self, request):
		actions = super().get_actions(request)
        
		user = Professor.objects.get(pk=get_user_id(request))

		if user.funcao_docente == '0':
			if 'aceitar_agendamento' in actions:
				del actions['aceitar_agendamento']
			if 'negar_agendamento' in actions:
				del actions['negar_agendamento']
		return actions

	def formfield_for_foreignkey(self, db_field, request, **kwargs):
		user = Professor.objects.get(pk=get_user_id(request))
		if db_field.name == "materia_corrente":
			kwargs["queryset"] = MateriaCorrente.objects.filter(professor=user)
		return super(AgendamentoAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

	def get_queryset(self, request):
		qs = super(AgendamentoAdmin, self).get_queryset(request)
		user = Professor.objects.get(pk=get_user_id(request))
		current_qs = qs.filter(user_id=user.pk)
		
		if user.funcao_docente == '2':
			current_qs = qs.filter(materia_corrente__professor__departamento__chefe__id=user.pk)
			print(current_qs)
		elif user.funcao_docente == '1':
			current_qs = qs.filter(materia_corrente__materia__turma__curso__coordenador__id=user.pk)
			print(current_qs)
		
		return current_qs

	def save_model(self, request, obj, form, change):
		if not change:
			obj.user_id = get_user_id(request)
		obj.save()

admin.site.register(Justificativa,  JustificativaAdmin)
admin.site.register(Antecipacao,  AntecipacaoAdmin)
admin.site.register(Reposicao,  ReposicaoAdmin)
admin.site.register(Agendamento,  AgendamentoAdmin)