from ast import mod
from django.db import models
from django.utils import timezone
from numpy import mask_indices, maximum

# Create your models here.

class device(models.Model):
    device_type = models.CharField(max_length=100, default='ADC')
    brend = models.CharField(max_length=100, default='Picoscope')
    serial_model = models.CharField(max_length=100, default='PS400A')
    proto = models.CharField(max_length=100, default='TCP')
    proto_interface = models.CharField(max_length=100, default='adc_default')
    time_creation = models.DateTimeField(default=timezone.now)
    time_publication = models.DateTimeField(blank=True, null=True)
    
    # Потом можно будет добавить ещё что-нибудь

    def set_defaults(self):
        self.time_publication = timezone.now()
        self.save()
        
    def __str__(self):
        return self.serial_model  

class adc_params(models.Model):
    device = models.ForeignKey(device, on_delete=models.PROTECT, related_name='adc_params', null=True)
    srate = models.PositiveIntegerField()
    points = models.JSONField(blank=True,
        null=True,
        default=list,
        help_text='Массив целых чисел в формате JSON')
    n_channels = models.PositiveIntegerField()
    channel_ranges = models.JSONField(blank=True,
        null=True,
        default=list,
        help_text='Массив целых чисел в формате JSON')
    n_triggers = models.PositiveIntegerField()
    trigger_channel = models.PositiveIntegerField()
    threshold = models.IntegerField()
    trig_direction = models.IntegerField()
    auto_measure_time = models.PositiveIntegerField()
    averaging = models.PositiveIntegerField(default=1)
    enabled = models.BooleanField(default=True)

    def __str__(self):
        return self.device.serial_model

class sync_params(models.Model):
    device = models.ForeignKey(device, on_delete=models.PROTECT, related_name='sync_params', null=True)
    file = models.CharField(max_length=100, default='Sync_params.xml')
    port = models.IntegerField(default=7)

    def __str__(self):
        return self.device.serial_model


class sdr_params(models.Model):
    device = models.ForeignKey(device, on_delete=models.PROTECT, related_name='sdr_params', null=True)
    srate = models.PositiveIntegerField(default=2000000)
    freq = models.PositiveIntegerField(default=3000000)
    gain = models.PositiveIntegerField(default=35)
    ampl = models.BooleanField(default=True)
    file = models.CharField(editable=True)

    def __str__(self):
        return self.device.serial_model

class gra_params(models.Model):
    device = models.ForeignKey(device, on_delete=models.PROTECT, related_name='gra_params', null=True)
    ip = models.GenericIPAddressField(default='127.0.0.1')
    file = models.CharField(editable=True)
    enabled = models.BooleanField(default=True)

    def __str__(self):
        return self.device.serial_model
    
class measurement_info(models.Model):
    infostr = models.CharField(max_length=100, default='str')
    time = models.DateTimeField(default=timezone.now)
    iadc = models.ForeignKey(adc_params, on_delete=models.CASCADE, related_name='measurement_info6')
    isync = models.ForeignKey(sync_params, on_delete=models.CASCADE, related_name='measurement_info5')
    isdr = models.ForeignKey(sdr_params, on_delete=models.CASCADE, related_name='measurement_info4')
    igrax = models.ForeignKey(gra_params, on_delete=models.CASCADE, related_name='measurement_info3')
    igray = models.ForeignKey(gra_params, on_delete=models.CASCADE, related_name='measurement_info2')
    igraz = models.ForeignKey(gra_params, on_delete=models.CASCADE, related_name='measurement_info1')
    engine = models.CharField(max_length=100, default='DefaultEngine')

    def __str__(self):
        return self.infostr

class device_state(models.Model):
    status = models.CharField(max_length=100, default='')
    code = models.BigIntegerField(default=0)

    def __str__(self):
        return self.status

class state(models.Model):
    id_measurement = models.IntegerField(default=0)
    status = models.CharField(max_length=100, default='default')
    code = models.BigIntegerField(default=0)
    adc = models.ForeignKey(device_state, on_delete=models.CASCADE, related_name='state6')
    sync = models.ForeignKey(device_state, on_delete=models.CASCADE, related_name='state5')
    sdr = models.ForeignKey(device_state, on_delete=models.CASCADE, related_name='state4')
    grax = models.ForeignKey(device_state, on_delete=models.CASCADE, related_name='state3')
    gray = models.ForeignKey(device_state, on_delete=models.CASCADE, related_name='state2')
    graz = models.ForeignKey(device_state, on_delete=models.CASCADE, related_name='state1')
    data_ready = models.BooleanField(default=False)

    def __str__(self):
        return self.status

class measurement(models.Model):
    info = models.ForeignKey(measurement_info, on_delete=models.CASCADE, related_name='measurement2')
    state = models.ForeignKey(state, on_delete=models.CASCADE, related_name='measurement1', null=True)

    def __str__(self):
        return self.info.infostr
    
class measurement_data(models.Model):
    measurement = models.ForeignKey(measurement, on_delete=models.CASCADE, related_name='measurement_data1', null=True)
    channel_data = models.ManyToManyField('channel_data', related_name='measurement_data2')
    data_num = models.IntegerField(default=0)
    averaging_num = models.IntegerField(default=0)

    def __str__(self):
        return str(self.id)
    
class channel_data(models.Model):
    channel_num = models.IntegerField(default=0)
    channel_data = models.TextField(editable=True, default='')
    measurement_data = models.ForeignKey(measurement_data, on_delete=models.CASCADE, related_name='channel_data1', null=True)

    def __str__(self):
        return str(self.id)