# coding=utf-8
import datetime
import logging

import timeout_decorator
from django_cron import CronJobBase, Schedule

from web.models import ConfigurationItem
from web.models.constants import VIRUS_EMAIL_HOUR_CONFIGURATION_TYPE, \
    CRON_JOB_TIMEOUT
from ..models import StudySubject, Worker
from ..smash_email import EmailSender

logger = logging.getLogger(__name__)


def get_subject_statistics():
    date_from = datetime.datetime.now() - datetime.timedelta(days=1)

    subjects_positive_count = StudySubject.objects.filter(virus_test_1_updated__gt=date_from, virus_test_1=True).count()
    subjects_positive_count += StudySubject.objects.filter(virus_test_2_updated__gt=date_from,
                                                           virus_test_2=True).count()
    subjects_positive_count += StudySubject.objects.filter(virus_test_3_updated__gt=date_from,
                                                           virus_test_3=True).count()
    subjects_positive_count += StudySubject.objects.filter(virus_test_4_updated__gt=date_from,
                                                           virus_test_4=True).count()
    subjects_positive_count += StudySubject.objects.filter(virus_test_5_updated__gt=date_from,
                                                           virus_test_5=True).count()

    subjects_negative_count = StudySubject.objects.filter(virus_test_1_updated__gt=date_from,
                                                          virus_test_1=False).count()
    subjects_negative_count += StudySubject.objects.filter(virus_test_2_updated__gt=date_from,
                                                           virus_test_2=False).count()
    subjects_negative_count += StudySubject.objects.filter(virus_test_3_updated__gt=date_from,
                                                           virus_test_3=False).count()
    subjects_negative_count += StudySubject.objects.filter(virus_test_4_updated__gt=date_from,
                                                           virus_test_4=False).count()
    subjects_negative_count += StudySubject.objects.filter(virus_test_5_updated__gt=date_from,
                                                           virus_test_5=False).count()

    subjects_inconclusive_count = StudySubject.objects.filter(virus_test_1_updated__gt=date_from,
                                                              virus_test_1__isnull=True).count()
    subjects_inconclusive_count += StudySubject.objects.filter(virus_test_2_updated__gt=date_from,
                                                               virus_test_2__isnull=True).count()
    subjects_inconclusive_count += StudySubject.objects.filter(virus_test_3_updated__gt=date_from,
                                                               virus_test_3__isnull=True).count()
    subjects_inconclusive_count += StudySubject.objects.filter(virus_test_4_updated__gt=date_from,
                                                               virus_test_4__isnull=True).count()
    subjects_inconclusive_count += StudySubject.objects.filter(virus_test_5_updated__gt=date_from,
                                                               virus_test_5__isnull=True).count()

    return {"Positive": subjects_positive_count, "Negative": subjects_negative_count,
            "Inconclusive": subjects_inconclusive_count,
            "total": subjects_positive_count + subjects_negative_count + subjects_inconclusive_count}


def create_statistic_email_content(data, title):
    email_body = "<h1>" + title + "</h1>"

    email_body += "In the past 24 hours " + str(data["total"]) + " donors were tested</br></br>"
    for status in data:
        if status != "total":
            email_body += str(data[status]) + ' number of donors were tested ' + status + "</br>"

    return email_body


def send_mail(data):
    title = "Virus test statistics"
    email_body = create_statistic_email_content(data, title)

    recipients = []
    workers = Worker.objects.all()
    for worker in workers:
        if worker.user is not None:
            if worker.user.is_active:
                if worker.email is not None and worker.email != "":
                    recipients.append(worker.email)
    cc_recipients = []
    logger.warn('Calling to send email')
    EmailSender().send_email(title, email_body, ";".join(recipients), cc_recipients)


class KitRequestEmailSendJob(CronJobBase):
    RUN_AT = []

    try:
        times = ConfigurationItem.objects.get(
            type=VIRUS_EMAIL_HOUR_CONFIGURATION_TYPE).value.split(";")
        for entry in times:
            # TODO it's a hack assuming that we are in CEST
            text = str((int(entry.split(":")[0]) + 22) % 24) + ":" + entry.split(":")[1]
            RUN_AT.append(text)
    except:
        logger.error("Cannot fetch data about email hour")
    schedule = Schedule(run_at_times=RUN_AT)
    code = 'web.virus_daily_email'  # a unique code

    @timeout_decorator.timeout(CRON_JOB_TIMEOUT)
    def do(self):
        data = get_subject_statistics()
        send_mail(data)
        return "mail sent"