from email.policy import default
from django.db import models
from django.utils import timezone
from django.template.defaultfilters import slugify

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

# SITE = [
# 	('GEN', 'Gentilly'),
# 	('CAP', 'Ambroise Paré'),
# 	('HPN', 'HPNL'),
# 	('EXT', 'Extéreieur'),
# ]

TYPE = [
	('PC', 'PC'),
	('PORTABLE', 'Portable'),
	('CL', 'Client Léger'),
	('PANEL-PC', 'Panel-PC'),
	('IMP', 'Imprimante'),
]

STATUT = [
	('RAS', '-'),
	('PREPROD','En préparation'),
	('HS','A réparer'),
	('ADEPROD','A retirer'),
	('EN STOCK', 'En stock'),
	('INAPTE', 'Inapte / HS'),
]

SWOS = [
	('CISCO', 'Cisco'),
	('ARUBA', 'Aruba'),
	('HP', 'HP'),
]
SWTYPE = [
	('F', 'Fast'),
	('G', 'Giga'),
]

class Accessory_Link(models.Model):
	Accessory_Nom = models.CharField("Nom de l\'accesoire", max_length=64, unique=True)
	Accessory_N_slugify = models.CharField("Nom de l\'accesoir slugifié", max_length=64, blank = True, editable = False)
	Accessory_icone = models.CharField("Code de l'icone", max_length = 64, blank = True)
	Accessory_on_list = models.BooleanField("Afficher sur le listing", default=False)

	def save(self, *args, **kwargs) :
		self.Accessory_N_slugify = slugify(self.Accessory_Nom)
		super(Accessory_Link, self).save(*args, **kwargs)

	def __str__(self):
		return str(self.Accessory_Nom)
	
	class Meta:
		verbose_name = "Accessoires lié un poste Informatique"
		verbose_name_plural = "Accessoires lié un poste Informatique"

# Create your models here.
class PIC(models.Model):
	PIC_NUnic = models.PositiveSmallIntegerField("Numéro uniq", unique=True, blank=True, null=True)
	PIC_L_Statut = models.CharField("Statut", max_length=16, choices=STATUT, default='RAS')
	PIC_Site = models.ForeignKey(Clinique, verbose_name="Clinique", on_delete=models.PROTECT, null=True)
	PIC_L_Chassi_Type = models.CharField("Type", max_length=8, choices=TYPE, default='PC')
	PIC_Deg = models.BooleanField("Poste avec procédure dégradé", default=False)

	PIC_is_for_Cadre = models.BooleanField("Poste utiliser pas des RUS ou Cadre", default=False)
	PIC_with_Snow = models.BooleanField("Sync avec Snow", default=False)
	PIC_with_Office = models.BooleanField("Office installé sur le poste", default=False)

	PIC_Nom_netbios = models.CharField("Nom NETBIOS", max_length=64, blank=True, null=True)
	PIC_Utilisateur = models.CharField("Utilisateur", max_length=64, blank=True, null=True)
	PIC_Utilisateur_Fq = models.CharField("Utilisateur Frequent", max_length=64, blank=True, null=True)

	PIC_L_Service = models.ForeignKey("Services", on_delete=models.CASCADE)
	PIC_Service = models.ForeignKey(Clin_Services, verbose_name="Service", on_delete=models.PROTECT, blank=True, null=True)
	PIC_Local = models.CharField("Localisation", max_length=64, blank=True, null=True)

	PIC_Adresse_IP = models.CharField("Adresse IP", max_length=24, default="DHCP" )
	PIC_Adresse_Mac = models.CharField("Adresse MAC", max_length=18, blank=True, null=True)

	PIC_OS = models.CharField("OS", max_length=64, blank=True, null=True)
	PIC_Marque = models.CharField("Marque", max_length=64, blank=True, null=True)
	PIC_Type = models.CharField("Modèle", max_length=64, blank=True, null=True)
	PIC_SN = models.CharField("SN", max_length=64, blank=True, null=True)
	PIC_Date_Achat = models.CharField("Date d'achat", max_length=64, blank=True, null=True)
	PIC_Fin_de_Garantie = models.CharField("Date de fin de garentie", max_length=64, blank=True, null=True)
	PIC_CPU = models.CharField("CPU", max_length=64, blank=True, null=True)
	PIC_RAM = models.PositiveSmallIntegerField("RAM", blank=True, null=True)
	PIC_Commentaires = models.TextField("Commentaires", blank=True, null=True)
	PIC_Precisions = models.TextField("Precisions", blank=True, null=True)
	PIC_Accessory = models.ManyToManyField(Accessory_Link, blank=True)

	def __str__(self):
		return self.PIC_Nom_netbios

	def save(self, *args, **kwargs):
		if self.PIC_Nom_netbios != None :
			if "GEN-" in self.PIC_Nom_netbios or "CAP-" in self.PIC_Nom_netbios or "HPN-" in self.PIC_Nom_netbios:
				tmp_PIC_NUnic = self.PIC_Nom_netbios[-4:]
				try:
					self.PIC_NUnic = int(tmp_PIC_NUnic)
				except:
					pass
			self.PIC_Nom_netbios = self.PIC_Nom_netbios.upper()
			
		if self.PIC_Adresse_IP != None :
			self.PIC_Adresse_IP = self.PIC_Adresse_IP.replace(',','.').replace(';','.')
		if self.PIC_Adresse_IP != None :
			self.PIC_Adresse_IP = self.PIC_Adresse_IP.replace(',','.').replace(';','.')

		super(PIC, self).save(*args, **kwargs)

	class Meta:
		verbose_name = "Poste client"
		verbose_name_plural = "Postes clients"

class PIC_History(models.Model):
	H_PIC = models.ForeignKey("PIC", on_delete=models.CASCADE)
	H_Date = models.DateTimeField("Date", auto_now_add=True)
	H_Commentaire = models.TextField("Historique")
	
	def __str__(self):
		return str(self.H_PIC)
	
	class Meta:
		verbose_name = "Historique un poste Informatique"
		verbose_name_plural = "Historique lié un poste Informatique"

class PIS(models.Model):
	PIC_NUnic = models.PositiveSmallIntegerField("Numéro uniq", unique=True, blank=True, null=True)
	PIS_Site = models.ForeignKey(Clinique, verbose_name="Clinique", on_delete=models.PROTECT, null=True)
	PIS_Nom_netbios = models.CharField("Nom NETBIOS", max_length=64, blank=True, null=True)
	PIS_Friendly_Name = models.CharField("Friendly Name", max_length=64, blank=True, null=True)
	PIS_OS = models.CharField("OS", max_length=64, blank=True, null=True)
	PIS_with_Snow = models.BooleanField("Sync avec Snow", default=False)
	PIS_with_Web = models.BooleanField("Portail web", default=False)
	PIS_Adresse_IP = models.CharField("Adresse IP", max_length=64, blank=True, null=True)
	PIS_Adresse_Mac = models.CharField("Adresse MAC", max_length=64, blank=True, null=True)
	PIS_Role = models.CharField("Application", max_length=64, blank=True, null=True)
	PIS_Type = models.CharField("Type", max_length=64, blank=True, null=True)
	PIS_SN = models.CharField("SN", max_length=64, blank=True, null=True)
	PIS_CPU = models.CharField("CPU", max_length=64, blank=True, null=True)
	PIS_RAM = models.PositiveSmallIntegerField("RAM", blank=True, null=True)
	PIS_Localisation = models.CharField("Localisation", max_length=64, blank=True, null=True)
	PIS_Fonctionnalite = models.TextField("Fonctionnalité", blank=True, null=True)
	PIS_Commentaires = models.TextField("Commentaires", blank=True, null=True)
	PIS_Archive = models.BooleanField("Serveur plus en Production", default=False)
	
	def __str__(self):
		return self.PIS_Nom_netbios

	def save(self, *args, **kwargs):
		self.PIS_Nom_netbios = self.PIS_Nom_netbios.upper()
		if self.PIS_Type != None :
			self.PIS_Type = self.PIS_Type.upper()
		if self.PIS_Role != None :
			self.PIS_Role = self.PIS_Role.upper()
		if self.PIS_Adresse_IP != None :
			self.PIS_Adresse_IP = self.PIS_Adresse_IP.replace(',','.').replace(';','.')
		if self.PIS_Commentaires != None :
			self.PIS_Commentaires = self.PIS_Commentaires.replace(',','.').replace(';','.')
		
		super(PIS, self).save(*args, **kwargs)

	class Meta:
		verbose_name = "Serveur"
		verbose_name_plural = "Serveurs"

class Swich(models.Model):
	SW_Baie = models.ForeignKey("Baie", on_delete=models.CASCADE)
	SW_Nom = models.CharField("Nom", max_length=64)
	SW_IP = models.CharField("Adresse IP", max_length=64, blank=True, null=True)
	SW_OS = models.CharField("OS", max_length=5, choices=SWOS, default='HP')
	SW_TYPE = models.CharField("Type", max_length=1, choices=SWTYPE, default='G')
	SW_Commentaires = models.TextField("Commentaires", blank=True, null=True)
	SW_Update_Port = models.TextField("Last Auto update port of switch", blank=True, null=True)
	SW_Archive = models.BooleanField("Switch plus en production", default=False)

	class Meta:
		verbose_name = "Switch"
		verbose_name_plural = "Switchs"
	def __str__(self):
		return self.SW_Nom

class Port_Link(models.Model):
	PL_Swich = models.ForeignKey("Swich", on_delete=models.CASCADE)
	PL_Port_NG = models.CharField("Port sur le Switch", max_length=10)
	PL_VLAN = models.PositiveSmallIntegerField("VLAN sur le Port")
	PL_Headbang_plug = models.CharField("Prise sur le bandeau", max_length=8, blank=True)
	PL_Commentaires = models.TextField("Commentaires", blank=True, null=True)

	def __str__(self):
		return str(self.PL_Swich)+' - '+str(self.PL_Port)
	
	class Meta:
		verbose_name = "Lien des prises vers les Switchs"
		verbose_name_plural = "Lien des prises vers les Switchs"

class Services(models.Model):
	SERVICE_Anag = models.CharField("Anagrame du Service", max_length=2, unique=True)
	SERVICE_Nom = models.CharField("Nom du Service", max_length=64, unique=True)
	
	def __str__(self):
		return self.SERVICE_Nom

	class Meta:
		verbose_name = "Service"
		verbose_name_plural = "Services"

class Baie(models.Model):
	BAIE_Site_NG = models.ForeignKey(Clinique, verbose_name="Clinique", on_delete=models.PROTECT, null=True)
	BAIE_Nom =  models.CharField("Nom de la Baie", max_length=32, unique=True)
	BAIE_Local = models.CharField("Localisation", max_length=64, blank=True, null=True)
	BAIE_Commentaire = models.TextField("Commentaires", blank=True, null=True)

	def __str__(self):
		return self.BAIE_Nom

	class Meta:
		verbose_name = "Baie"
		verbose_name_plural = "Baies"