from django import forms
from django.db import models
from django.utils import timezone

from gest_clin.models import Clinique as Clinique
from gest_clin.models import Services as Clin_Services


login_type = (
	(u'pnom', u'1er lettre du Prenom puis Nom'),
	(u'pnomMGAG', u'1er lettre du Prenom puis Nom en Mag'),
	(u'pre.nom', u'Prenom.Nom'),
	(u'preMAG.nomMAG', u'PRENOM.NOM'),
	(u'mixed', u'Au choix'),
	(u'pre.nom', u'Lié a AD'),
	(u'pre.nom@el', u'Lié au compte email'),
	(u'autre', u'Autre'),
)

def generate_password(length=9, chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"):
	import random
	password = ""
	for _ in range(length):
		password += random.choice(chars)
	return password

class GES_APP(models.Model):
	GESAP_Nom = models.CharField("Nom", max_length=16)
	GESAP_ltype = models.CharField("Type de Login",choices=login_type, max_length=48, default='pre.nom')
	GESAP_Sup_Nom = models.CharField("Champ complaimentaire", max_length=16, blank=True)
	#GESAP_Commentaire = models.TextField("Aide pour la création du compte", blank=True)
	GESAP_Procedure = models.CharField("Lien vers la Procédure de création de compte", max_length=248, blank=True)
	GESAP_Actif = models.BooleanField("Active ?", default = True)

	def __str__(self):
		return self.GESAP_Nom

	class Meta:
		verbose_name = "Les apllications métiers"
		verbose_name_plural = "Les apllications métiers"
		ordering = ['GESAP_Nom']

class GES_Fonction(models.Model):
	GESFC_Nom = models.CharField("Nom", max_length=16)
	
	def __str__(self):
		return self.GESFC_Nom

	class Meta:
		verbose_name = "Les fonctions"
		verbose_name_plural = "Les fonctions"
		ordering = ['GESFC_Nom']
		
class GES_User(models.Model):
	GES_Site =  models.ForeignKey(Clinique, verbose_name="Clinique", on_delete=models.PROTECT, null=True)
	GES_Nom = models.CharField("Nom", max_length=16)
	GES_Prenom = models.CharField("Prénom", max_length=16)
	GES_Motdepasse = models.CharField("Mot de passe", max_length=16, blank=True)
	GES_Service = models.ForeignKey(Clin_Services, verbose_name="Service", on_delete=models.PROTECT, blank=True, null=True)
	GES_Fonction = models.ForeignKey(GES_Fonction, verbose_name="Fonction", on_delete=models.PROTECT, blank=True, null=True)
	GES_APPs = models.ManyToManyField(GES_APP, verbose_name="Applications", limit_choices_to={'GESAP_Actif': True},)
	GES_Date = models.DateField("Date d'arrivée", blank=True, null=True)
	GES_OK = models.BooleanField("Création complété", default = False)
	ordering = ['GES_Service']

	def save(self, *args, **kwargs) :
		if self.GES_Motdepasse == "" :
			self.GES_Motdepasse = generate_password()
		super(GES_User, self).save(*args, **kwargs)

	def __str__(self):
		return self.GES_Nom + ' ' + self.GES_Prenom

	class Meta:
		verbose_name = "Les utilisateurs"
		verbose_name_plural = "Les utilisateurs"

class GES_APPLink(models.Model):
	GES_APPLink_User = models.ForeignKey(GES_User, on_delete=models.CASCADE)
	GES_APPLink_APP = models.ForeignKey(GES_APP, on_delete=models.CASCADE, verbose_name="Application", limit_choices_to={'GESAP_Actif': True},)
	GES_APPLink_Login = models.CharField("Login", max_length=48)
	GES_APPLink_MDP = models.CharField("Mot de passe", max_length=16)
	GES_APPLink_Sup = models.CharField("Info supplémentaire", max_length=16, blank=True)
	GES_APPLink_Create = models.BooleanField("Crée", default = False)

class GES_APPMat(models.Model):
	GES_Fonction = models.ForeignKey(GES_Fonction, verbose_name="Fonction", on_delete=models.PROTECT, blank=True, null=True)
	GES_Service = models.ManyToManyField(Clin_Services, verbose_name="Services")
	GES_APPs = models.ManyToManyField(GES_APP, verbose_name="Applications", limit_choices_to={'GESAP_Actif': True},)
	
	def __str__(self):
		return self.GES_Fonction.GESFC_Nom
	
	def Select_GES_Service(self):
		return "\n".join([p.SERVICE_Nom +', ' for p in self.GES_Service.all()])
	def Select_GES_APPs(self):
		return "\n".join([p.GESAP_Nom +', ' for p in self.GES_APPs.all()])

	class Meta:
		verbose_name = "Matrice Services - Fonctions - Applications"
		verbose_name_plural = "Matrice Services - Fonctions - Applications"
	
class GES_User_form(forms.ModelForm):
	class Meta:
		model = GES_User
		fields = '__all__'
		exclude = ['GES_APPs','GES_OK',]

class GES_APPLink_form(forms.ModelForm):
	class Meta:
		model = GES_APPLink
		fields = ['GES_APPLink_Login', 'GES_APPLink_MDP', 'GES_APPLink_Sup']

class GES_APPLink_add_form(forms.ModelForm):
	class Meta:
		model = GES_APPLink
		fields = ['GES_APPLink_APP',]

class PSearch_form(forms.Form):
	PSearch = forms.CharField(label='Recherche', max_length=128)