using System;
using System.Web.Mail;
using System.Web;
using System.Web.Services;
using edu.jhu.Cas.Jobs;
using edu.jhu.Cas.Users;

namespace edu.jhu.Cas.Util {
	/// <summary>
	/// Summary description for Mailer.
	/// </summary>
	public class Mailer {

        public static void Mail(string email, string message, long jobid) {
            string strFrom = ID.MAIL_FROM;
            string strSubject = ID.MAIL_SUBJECT;
            SmtpMail.SmtpServer = ID.SMTP;
            try {
                SmtpMail.Send(strFrom, email, strSubject + " job#" + jobid, message);
            }
            catch { }//don't break the job just because the smtp server is screwey
        }
		public static void MaybeMail(long wsid,string message, int status,long jobid) {
			Job j = (new CasJobs()).FindJob(wsid," jobid = "+jobid)[0];
			if(j.TaskName == null || j.TaskName.IndexOf(SYSTEM.TASKNAME) == -1){
				User u = (new CasUsers()).FindUser(ID.SERVICE_ID," webservicesid = "+wsid)[0];
				if(u.EmailLevel == EMAIL.ON_BATCH_FAILURE_OR_COMPLETION)
					Mail(u.Email,message,jobid);
				else//this is switched, i don't know where
					switch(status) {
						case QUERY_STATUS.FAILED:
							if(u.EmailLevel == EMAIL.ON_BATCH_FAILURE)
								Mail(u.Email,message,jobid);
							break;
						case QUERY_STATUS.FINISHED:
							if(u.EmailLevel == EMAIL.ON_BATCH_COMPLETION)
								Mail(u.Email,message,jobid);
							break;
					}
			}
		}
	}
}
