from django.db import models
from django.utils.translation import ugettext_lazy as _
from clients.models import ClientOffice, ContactPerson
from core.models import WinoseBaseWithTrackingModel


class Project(WinoseBaseWithTrackingModel):
    class Meta:
        verbose_name = _('project')
        verbose_name_plural = _('projects')

    overview_url = 'projects_list'
    detail_url = 'project_detail'
    create_url = 'project_create'
    update_url = 'project_update'

    # general purpose name for this project
    name = models.CharField(
        max_length=127,
        verbose_name=_('name'),
        unique=True
    )

    # client office this project belongs to
    client = models.ForeignKey(to=ClientOffice,
                               related_name='projects',
                               null=False,
                               verbose_name=_('client'))

    # again optional reference to a user specific reference
    reference = models.CharField(
        max_length=255,
        verbose_name=_('internal reference'),
        blank=True
    )

    # you can never have enough comment fields.
    comments = models.TextField(
        blank=True,
        max_length=1023,
        verbose_name=_('comments')
    )

    # contact person responsible for this sample (optional) (client side)
    contact = models.ForeignKey(to=ContactPerson,
                                null=True,
                                related_name='projects',
                                blank=True,
                                verbose_name=_('contact person'))

    # project manager (lab side)
    project_manager = models.ForeignKey(to=ContactPerson,
                                        null=True,
                                        related_name='manages',
                                        blank=True,
                                        verbose_name=_('project manager'))

    @property
    def operator_list(self):
        # utility method, which returns a list of all operators taking part in all measurements
        # for this project's samples
        from measurements.models import Measurement

        measurements = Measurement.objects.prefetch_related('rounds'). \
            filter(sample__pk__in=self.samples.all().values('pk'))
        result = list()
        for m in measurements:
            for r in m.rounds.all():
                result.append('{0} {1}'.format(r.operator.first_name, r.operator.last_name))
        return set(result)  # make unique

    # why here? I don't know.
    @property
    def recent_lab_values(self):
        from panelists.qualification import lab_qualification_values
        # convert floats to strings, meh.as
        lab_perf_dict = lab_qualification_values()
        if lab_perf_dict:  # not None implies A and r
            return {'r': str(lab_perf_dict['r']),
                    'A': str(lab_perf_dict['A'])}
        else:
            return lab_perf_dict  # None

    def toggle_is_active(self, state=None):
        super(Project, self).toggle_is_active(state)
        for sample in self.samples.all():
            sample.toggle_is_active(self.is_active)

    def __str__(self):
        return self.name
