from django.db import models
from django.utils.translation import ugettext_lazy as _

from core.models import WinoseBaseWithTrackingModel


class Calibration(WinoseBaseWithTrackingModel):
    class Meta:
        verbose_name = _('calibration')
        verbose_name_plural = _('calibrations')

    overview_url = 'calibrations_list'
    create_url = 'calibration_create'
    detail_url = 'calibration_detail'
    update_url = 'calibration_update'

    json_data = models.TextField(
        blank=False,
        editable=False  # from calibration file
    )

    valid_to = models.DateField(
        blank=False,
        editable=False,  # set from calibration file
        verbose_name=_('expires')
    )

    # the device this calibration set was
    # originally created for (set from calibration file_
    created_for = models.CharField(
        editable=False,
        blank=False,
        max_length=20,
        verbose_name=_('serial')
    )

    certificate = models.FileField(
        upload_to='calib_certs',
        verbose_name=_('certificate'),
        null=True,
        blank=True)

    def __str__(self):
        return '{0} {1} {2}'.format(self._meta.verbose_name, self.created_for, self.valid_to)


class Olfactometer(WinoseBaseWithTrackingModel):
    class Meta:
        verbose_name = _('olfactometer')
        verbose_name_plural = _('olfactometers')

    overview_url = 'olfactometers_list'
    detail_url = 'olfactometer_detail'
    create_url = 'olfactometer_create'
    update_url = 'olfactometer_update'

    name = models.CharField(
        max_length=63,
        blank=False,
        verbose_name=_('name')
    )

    inventory_number = models.CharField(
        max_length=127,
        blank=True,
        verbose_name=_('inventory number')
    )

    serial_no = models.CharField(
        max_length=10,
        blank=False,
        unique=True,
        verbose_name=_('serial')
    )

    calibration = models.ForeignKey(to=Calibration,
                                    related_name='used_by',
                                    verbose_name=_('calibration'))

    comment = models.TextField(
        blank=True,
        max_length=1023,
        verbose_name=_('comments')
    )

    def __str__(self):
        return '{0} {1}'.format(self.name, self.serial_no)


