#include "handler.ih"

namespace 
{
    char const pdfInfo[] =  R"(
A pdf document was constructed from the files in your submitted .zip file.
You find the zip containing the pdf document in the attached .zip file

Note that requesting a pdf does not imply that your exercises are also rated.

You're advised to check the layout details of the pdf document before 
submitting your zip for the purpose of having your exercises rated.

)";

}

void Handler::mail(MailType type)
{
    if (not g_options.sendMail())
    {
        string cc = g_mailer.cc();

        if (not cc.empty())
            cc = " (CC: " + cc + ')';

        throw Exception{NO_MAIL} << "Confirmation e-mail to " << d_from << 
                cc << " not sent";
    }

    ostringstream text;
    string subject;

    switch (type)
    {
        case EXERCISES:
            text << "Exercises of " << d_set <<
                    " (submitted " << d_date << ")\n"
                    "were received in good order";
            if (not d_rater.empty())
                text << " and will be rated by " << d_rater;
            text << ".\n"
                "Expect to receive the rated exercises within 3 "
                                                        "working days\n"
                "(unless the set's deadline is reached in between).\n";

            subject = "exercises received";
            log() << "exercises of " << d_set << 
                        " received from " << d_from << " are in " << 
                        d_spoolZip.substr(d_spoolZip.find_last_of('/') + 1) <<
                        '\n';

            d_exec.execute("/bin/cp " + d_spoolZip + 
                           " /var/lib/cpp/spool-saved");

            log() << "/bin/cp " << d_spoolZip <<
                           " /var/lib/cpp/spool-saved" << '\n';
        break;

        case PUBLIC_KEY:
            subject = "public key received";
            text << "A public PGP key (submitted " << d_date << ")\n"
                    "was received in good order\n";
        break;

        case PDF:
            g_mailer.attach(d_spoolZip);
            subject = "pdf available";
            text << pdfInfo;
        break;
    }

    g_mailer.sendmail(subject, text.str());

    if (type == PDF)
        d_exec.execute("/bin/rm " + d_spoolZip);
}


