# Copyright (C) 2012-2013 Science and Technology Facilities Council.
# Copyright (C) 2016 East Asian Observatory.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from smtplib import SMTP

from crab.report.text import report_to_text
from crab.report.html import report_to_html
from crab.report.summary import report_to_summary


class CrabNotifyEmail:
    """Class to send notification messages by email."""

    def __init__(self, crab_home, base_url, config_email):
        """Construct a nofication object.

        Stores relevant configuration information in the object."""

        self.home = crab_home
        self.base_url = base_url

        self.server = config_email['server']
        self.from_ = config_email['from']
        self.subject_ok = config_email['subject_ok']
        self.subject_warning = config_email['subject_warning']
        self.subject_error = config_email['subject_error']

    def __call__(self, report, to):
        """Sends a report by email to the given addresses."""

        if report.error:
            subject = self.subject_error
        elif report.warning:
            subject = self.subject_warning
        else:
            subject = self.subject_ok

        subject += ' (' + report_to_summary(report) + ')'

        message = MIMEMultipart('alternative')
        message['Subject'] = subject
        message['Date'] = formatdate(localtime=True)
        message['From'] = self.from_
        message['To'] = ', '.join(to)

        message.attach(MIMEText(report_to_text(report), 'plain'))
        message.attach(MIMEText(report_to_html(report,
                                self.home, self.base_url), 'html'))

        smtp = SMTP(self.server)
        smtp.sendmail(self.from_, to, message.as_string())
        smtp.quit()
