/*
 *   Copyright 2012 Brian Rosenberger (Brutex Network)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package net.brutex.xservices.ws.impl;

import javax.jws.WebService;

import net.brutex.xservices.types.HostConnection;
import net.brutex.xservices.types.MailMimeType;
import net.brutex.xservices.types.ReturnCode;
import net.brutex.xservices.types.ant.FileSetResource;
import net.brutex.xservices.util.BrutexNamespaces;
import net.brutex.xservices.util.RunTask;
import net.brutex.xservices.ws.MailService;

import org.apache.tools.ant.taskdefs.email.EmailTask;

/**
 * Implements MailService
 * 
 * @author Brian Rosenberger, bru@brutex.de
 */
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, 
			endpointInterface = "net.brutex.xservices.ws.MailService", 
			serviceName = "MailService")
public class MailServiceImpl implements MailService {


	public ReturnCode sendMailSimple(HostConnection mailhost, String from,
			String tolist, String subject, String message) {
		return sendMail(from, from, tolist, "", "", subject, message,
				"text/plain", null, mailhost.hostname, mailhost.port,
				mailhost.user, mailhost.password, "utf-8", false, false);
	}

	public ReturnCode sendMailSimpleWithAttachment(HostConnection mailhost,
			String from, String tolist, String subject, String message,
			FileSetResource res) {
		return sendMail(from, from, tolist, "", "", subject, message,
				"text/plain", res, mailhost.hostname, mailhost.port,
				mailhost.user, mailhost.password, "utf-8", false, false);
	}

	public ReturnCode sendMail(HostConnection mailhost, String from,
			String tolist, String cclist, String bcclist, String subject,
			MailMimeType mimetype, String charset, String message,
			FileSetResource res, boolean ssl, boolean tls) {
		return sendMail(from, from, tolist, cclist, bcclist, subject, message,
				mimetype.value(), res, mailhost.hostname, mailhost.port,
				mailhost.user, mailhost.password, charset, tls, ssl);
	}

	private ReturnCode sendMail(String from, String replyto, String tolist,
			String cclist, String bcclist, String subject, String message,
			String messagemimetype, FileSetResource attachments,
			String mailhost, int mailport, String user, String password,
			String charset, boolean tls, boolean ssl) {
		EmailTask mail = new EmailTask();
		mail.setTaskName("Mail");
		RunTask runner = new RunTask(mail);
		mail.setFrom(from);
		mail.setReplyTo(replyto);
		mail.setToList(tolist);
		mail.setCcList(cclist);
		mail.setBccList(bcclist);
		mail.setSubject(subject);
		mail.setMessage(message);
		//mail.setMessageMimeType(messagemimetype);
		if (attachments != null) {
			mail.addFileset(attachments.getAntResource(mail.getProject()));
		}
		mail.setMailhost(mailhost);
		mail.setMailport(mailport);
		mail.setUser(user);
		mail.setPassword(password);
		mail.setCharset(charset);
		mail.setSSL(ssl);
		mail.setEnableStartTLS(tls);
		return runner.postTask();
	}
}
