from django.shortcuts import render
from django.template import loader
from django.urls import reverse
from django.utils import timezone
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib import messages
from django.db.models import Q


from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth.models import User

from core.views import gen_page_base, gen_page_sys, get_get_value
from .forms import *
from .models import *

def text_to_icon(text):
	icon = "Error"
	if text == "0":
		icon = "<i class=\"far fa-star\"></i><i class=\"far fa-star\"></i>"
	elif text == "1":
		icon = "<i class=\"fas fa-star\"></i><i class=\"far fa-star\"></i>"
	elif text == "2":
		icon = "<i class=\"fas fa-star\"></i><i class=\"fas fa-star-half-alt\"></i>"
	elif text == "3":
		icon = "<i class=\"fas fa-star\"></i><i class=\"fas fa-star\"></i>"
	elif text == "NOUVELLE":
		icon = "<i class=\"far fa-calendar-plus\"></i>"
	elif text == "PROGRAMEE":
		icon = "<i class=\"far fa-calendar-alt\"></i>"
	elif text == "ENCOUR":
		icon = "<i class=\"far fa-calendar\"></i>"
	elif text == "FINI":
		icon = "<i class=\"fas fa-calendar-check\"></i>"
	
	return icon


def taches_get_list(request):
	template = loader.get_template('parc_info_api_taches_get_list.html')

	data_query = Taches.objects.exclude(Tache_Statut = 'FINI').order_by('-Tache_Priorite','-Tache_Date_Cree')
	for item in data_query:
		item.Tache_Priorite = text_to_icon(item.Tache_Priorite)
		item.Tache_Statut = text_to_icon(item.Tache_Statut)

	html = template.render({
			'data_query': data_query,
		}, request)
		
	return HttpResponse(html)

def taches_get_info(request, id_tache):
	template = loader.get_template('parc_info_api_taches_get.html')
	
	item = Taches.objects.get(id = id_tache)
	item.Tache_Priorite = text_to_icon(item.Tache_Priorite)
	item.Tache_Statut = text_to_icon(item.Tache_Statut)

	html = template.render({
			'item': item,
		}, request)
		
	return HttpResponse(html)

def taches_edit_info(request, id_tache):
	template = loader.get_template('parc_info_api_taches_edit.html')
	if id_tache == None or id_tache == '0':
		item = Taches()
		item.Tache_Date_Programee = timezone.now()
		item.id = 0
	else :
		item = Taches.objects.get(id = id_tache)

	form = Edit_Tache_form(instance=item)

	html = template.render({
			'form': form,
			'item': item,
		}, request)
		
	return HttpResponse(html)