import datetime

from django.db import models
from django.conf import settings
from django.utils import timezone

BASES_CHOICES = [
    ('1', 'Monthey'),
    ('2', 'Uvrier'),
]
TEAM_NUMBER = [
    ('1', 'Équipe 1'),
    ('2', 'Equipe 2'),
    ('3', 'Equipe 3'),
    ('4', 'Equipe 4'),
]
STATUS_CHOICES = [
    ('1', 'Opérationnel'),
    ('2', 'Réserve'),
    ('3', 'Utilisation altérée'),
    ('4', 'Hors Service'),
]
TYPES_CHOICES = [
    ('1', 'Technique'),
    ('2', 'Matériel'),
    ('3', 'Péremption'),
]
VHC_CHOICES = [
    ('1', 'Ambulance Urgence'),
    ('2', 'Ambulance Transfert'),
    ('3', 'Véhicule service'),
]


class Vehicles(models.Model):
    sName = models.CharField("Indicatif", max_length=250)
    nBases = models.CharField('Basé à ', max_length=1, choices=BASES_CHOICES, default=1)
    nStatus = models.CharField('Statut', max_length=1, choices=STATUS_CHOICES, default=1)
    nType = models.CharField('Type', max_length=1, choices=VHC_CHOICES, default=1)
    bLucas = models.BooleanField("Lucas", default=0)
    bDrap = models.BooleanField("Drap HRC", default=0)
    nOrder = models.IntegerField("Ordre", default=0)
    nTeam = models.CharField('Équipe', max_length=1, choices=TEAM_NUMBER, default=1)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.a_infos = None


    def get_all_infos(self,nType):
        if self.a_infos is not None:
            return self.a_infos[f"{nType}"]

        self.a_infos = {"1": [], "2": [], "3": []}
        infos = Vehicles_infos.objects.filter(Vehicle__id=self.id,bEnabled=1)
        for info in infos:
            self.a_infos[f"{info.nType}"].append(info)
        print(self.a_infos[f"{nType}"])
        return self.a_infos[f"{nType}"]

    def get_all_infos_tech(self):
        return self.get_all_infos(1)

    def get_all_infos_mat(self):
        return self.get_all_infos(2)

    def get_all_infos_perempt(self):
        return self.get_all_infos(3)[3::]

    def get_all_infos_perempt_perma(self):
        return self.get_all_infos(3)[:3]





    def __str__(self):
        return self.sName

    class Meta:
        verbose_name = "véhicule"
        verbose_name_plural = "véhicules"


class Vehicles_infos(models.Model):
    Vehicle = models.ForeignKey(Vehicles, on_delete=models.CASCADE, verbose_name="Véhicule" , related_name='vehicles_infos')
    nType = models.CharField('Type', max_length=1, choices=TYPES_CHOICES, default=1)
    sDesc = models.TextField("Description")
    bEnabled = models.BooleanField("Actif",default=1)

    dtStart = models.DateField("Date de début")
    dtEnd = models.DateField("Date de fin", blank=True, null=True)
    sAuthor = models.CharField("auteur string",max_length=120)
    Author = models.ForeignKey(settings.AUTH_USER_MODEL,verbose_name="Auteur", on_delete=models.DO_NOTHING)
    dtUpdated = models.DateTimeField('date updated', auto_now=True)
    dtCreated = models.DateTimeField('date published', auto_now_add=True)

    def get_Start_formated(self):
        return self.dtStart.strftime("%d.%m")
    def get_Author_formated(self):
        return self.Author.username.replace("@clerc.ch",'')


    def __str__(self):
        return self.sDesc

    class Meta:
        verbose_name = "véhicule info"
        verbose_name_plural = "véhicule infos"
