/*******************************************************************************
 * Copyright 2018 2Fi Business Solutions Ltd.
 *
 * This code is part of the KONE project.
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.tbms.mail.service;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import javax.mail.internet.InternetAddress;

import com.ffii.core.Settings;
import com.ffii.core.engine.MailEngine;
import com.ffii.core.setting.service.SettingsService;
import com.ffii.core.utils.SecurityUtils;
import com.ffii.core.web.AbstractService;
import com.ffii.tbms.user.service.SysGroupService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamSource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class MailService extends AbstractService {
	public static InternetAddress FIX_RECIPIENT = null;

    @Autowired
    private SettingsService settingsService;

	@Autowired
	private MailEngine mailEngine;

	@Autowired
    private SysGroupService sysGroupService;

	static {
		FIX_RECIPIENT = null; // new InternetAddress("netpluto@gmail.com", "Jason Chuang");
	}

	@Async
	public void sendSystemMail(int sysGroupId,InternetAddress recipient, String subject, String contentTemplate, Map<?, ?> contentModel) throws UnsupportedEncodingException {
		this.sendSystemMail(sysGroupId, getFromAddress(sysGroupId), null, FIX_RECIPIENT != null ? FIX_RECIPIENT : recipient, subject, contentTemplate, contentModel);
	}

	public InternetAddress getFromAddress(int sysGroupId) throws UnsupportedEncodingException{
		Map<String, Object> sysGroup = sysGroupService.find(sysGroupId);
		return new InternetAddress(sysGroup.get("smtp_username")+"", sysGroup.get("companyName")+"");
	}

	@Async
	public void sendSystemMail(int sysGroupId, InternetAddress sender, InternetAddress replyTo, InternetAddress recipient, String subject, String contentTemplate,
			Map<?, ?> contentModel) {
		if (FIX_RECIPIENT != null) 
			logger.info("sendSystemMail() start, from: " + sender + ", to: " + FIX_RECIPIENT.getAddress() + ",Subject: " + subject);
		else
			logger.info("sendSystemMail() start, from: " + sender + ", to: " + recipient.getAddress() + ",Subject: " + subject);
		mailEngine.sendFreeMarkerHtmlMail(sysGroupId, sender, replyTo, new InternetAddress[] { FIX_RECIPIENT != null ? FIX_RECIPIENT : recipient }, subject, contentTemplate,
				contentModel);
		logger.info("sendSystemMail() end");
	}

	@Async
	public void sendSystemMail(int sysGroupId,InternetAddress sender, InternetAddress replyTo, InternetAddress recipient, String subject, String contentTemplate,
			Map<?, ?> contentModel, String attachmentFilename, File file) {
		if (FIX_RECIPIENT != null) 
			logger.info("sendSystemMail() start, from: " + sender + ", to: " + FIX_RECIPIENT.getAddress() + ",Subject: " + subject);
		else
			logger.info("sendSystemMail() start, from: " + sender + ", to: " + recipient.getAddress() + ",Subject: " + subject);
		mailEngine.sendFreeMarkerHtmlMail(sysGroupId, sender, replyTo, new InternetAddress[] { FIX_RECIPIENT != null ? FIX_RECIPIENT : recipient }, subject, contentTemplate,
				contentModel, attachmentFilename, file);
		logger.info("sendSystemMail() end");
	}

	@Async
	public void sendSystemMail(int sysGroupId, InternetAddress sender, InternetAddress replyTo, InternetAddress recipient, String subject, String contentTemplate,
			Map<?, ?> contentModel, String attachmentFilename, InputStreamSource file) {
		if (FIX_RECIPIENT != null) 
			logger.info("sendSystemMail() start, from: " + sender + ", to: " + FIX_RECIPIENT.getAddress() + ",Subject: " + subject);
		else
			logger.info("sendSystemMail() start, from: " + sender + ", to: " + recipient.getAddress() + ",Subject: " + subject);
		mailEngine.sendFreeMarkerHtmlMail(sysGroupId, sender, replyTo, new InternetAddress[] { FIX_RECIPIENT != null ? FIX_RECIPIENT : recipient }, subject, contentTemplate,
				contentModel, attachmentFilename, file);
		logger.info("sendSystemMail() end");
	}

	@Async
	public void sendSystemMail(int sysGroupId, InternetAddress sender, InternetAddress replyTo, InternetAddress[] recipients, String subject, String contentTemplate,
			Map<?, ?> contentModel) {
		sendSystemMail(sysGroupId, sender, replyTo, recipients, subject, contentTemplate, contentModel, null, null);
	}

	@Async
	public void sendSystemMail(int sysGroupId, InternetAddress sender, InternetAddress replyTo, InternetAddress[] recipients, String subject, String contentTemplate,
			Map<?, ?> contentModel, String attachmentFilename, InputStreamSource file) {
		if (FIX_RECIPIENT != null) 
			logger.info("sendSystemMail() start, from: " + sender + ", to: " + FIX_RECIPIENT.getAddress() + ",Subject: " + subject);
		else
			logger.info("sendSystemMail() start, from: " + sender + ", to: " + recipients.toString() + ",Subject: " + subject);
		mailEngine.sendFreeMarkerHtmlMail(sysGroupId, sender, replyTo, FIX_RECIPIENT != null ? new InternetAddress[] { FIX_RECIPIENT } : recipients, subject, contentTemplate,
				contentModel, attachmentFilename, file);
		logger.info("sendSystemMail() end");
	}
	
}