from django.shortcuts import render
from django.template import loader
from django.template.defaultfilters import slugify
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 .models import *

def genloginapp(user_id, app_id):
	item = GES_User.objects.get( id = user_id)
	app = GES_APP.objects.get( id = app_id)
	
	app_login =""
	if app.GESAP_ltype == "pnom":
		app_login = slugify(item.GES_Prenom[:1]) + slugify(item.GES_Nom)
		app_login = app_login.lower()
	if app.GESAP_ltype == "pnomMGAG":
		app_login = slugify(item.GES_Prenom[:1]) + slugify(item.GES_Nom)
		app_login = app_login.upper()
	elif app.GESAP_ltype == "pre.nom":
		app_login = slugify(item.GES_Prenom) + "." + slugify(item.GES_Nom)
		app_login = app_login.lower()
	elif app.GESAP_ltype == "preMAG.nomMAG":
		app_login = slugify(item.GES_Prenom) + "." + slugify(item.GES_Nom)
		app_login = app_login.upper()
	elif app.GESAP_ltype == "pre.nom@el":
		app_login = slugify(item.GES_Prenom) + "." + slugify(item.GES_Nom) + "@elsan.care"
		app_login = app_login.lower()
	return app_login

def link_user_to_app(user_id, action):
	item = GES_User.objects.get( id = user_id)
	for apps in item.GES_APPs.all():
		app_login = genloginapp(item.id, apps.id)
		print(app_login)

		obj, created = GES_APPLink.objects.update_or_create(GES_APPLink_User= item, GES_APPLink_APP= apps)
		print(created)
		if created == True or action == "check":
			obj.GES_APPLink_Login = app_login
			obj.GES_APPLink_MDP=item.GES_Motdepasse
			obj.GES_OK = False
			obj.save()
	return

def user_get_list(request):
	template = loader.get_template('gest_user_api_get_list.html')

	get_search = request.GET.get('get_search', '')
	if get_search :
		#print(get_search)
		data_query = GES_User.objects.filter(Q(GES_Nom__icontains=get_search)
										|Q(GES_Prenom__icontains=get_search)
										|Q(GES_Service__SERVICE_Nom__icontains=get_search)
										|Q(GES_Fonction__GESFC_Nom__icontains=get_search)
										).order_by('GES_L_Site','GES_Service','GES_Fonction','GES_Nom','GES_Prenom')
	else :
		#print("not get_search")
		data_query = GES_User.objects.filter(GES_OK = False).order_by('GES_L_Site','GES_Service','GES_Fonction','GES_Nom','GES_Prenom')

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

def user_get(request, id):
	template = loader.get_template('gest_user_api_get.html')
	item = GES_User.objects.get(id = id)
	link_user_to_app(item.id, 'update')

	item.apps = GES_APPLink.objects.filter(GES_APPLink_User = item.id)

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

def user_get_for_send(request, id):
	template = loader.get_template('gest_user_api_get_for_send.html')
	item = GES_User.objects.get(id = id)
	link_user_to_app(item.id, 'update')

	item.apps = GES_APPLink.objects.filter(GES_APPLink_User = item.id)

	html = template.render({
			'item': item,
		}, request)
		
	return HttpResponse(html)
def user_edit(request, id):
	get_search = request.GET.get('get_search', '')
	print(get_search)
	template = loader.get_template('gest_user_api_edit.html')
	if id == None or id == '0':
		item = GES_User()
		item.id = 0
	else :
		item = GES_User.objects.get(id = id)

	form = GES_User_form(instance=item)
	form.url = "gest_user_edit"

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

def app_edit(request, id):
	get_search = request.GET.get('get_search', '')
	print(get_search)
	template = loader.get_template('gest_user_api_edit.html')
	if id == None or id == '0':
		item = GES_APPLink()
		item.id = 0
	else :
		item = GES_APPLink.objects.get(id = id)

	form = GES_APPLink_form(instance=item)
	form.url = "gest_app_edit"

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

def switch_app_status(request, id):
	try:
		item = GES_APPLink.objects.get(id = id)
	except:
		return HttpResponse('Error')

	if item.GES_APPLink_Create == False :
		item.GES_APPLink_Create = True
	else :
		item.GES_APPLink_Create = False
	item.save()
	
	return HttpResponse(item.GES_APPLink_User.id)
