package com.bizzan.bitrade.config;

import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;

import javax.mail.internet.MimeMessage;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
@ConditionalOnProperty(name = "spring.mail.host")
@EnableConfigurationProperties(MailProperties.class)
public class MailConfiguration extends JavaMailSenderImpl {

	private ArrayList<String> usernameList;

	private ArrayList<String> passwordList;

	private int currentMailId = 0;

	private final MailProperties properties;

	public MailConfiguration(MailProperties properties) {
		this.properties = properties;

// 初始化账号

		if (usernameList == null)

			usernameList = new ArrayList<String>();

		String[] userNames = this.properties.getUsername().split(",");

		if (userNames != null) {
			for (String user : userNames) {
				usernameList.add(user);

			}

		}

// 初始化密码

		if (passwordList == null)

			passwordList = new ArrayList<String>();

		String[] passwords = this.properties.getPassword().split(",");

		if (passwords != null) {
			for (String pw : passwords) {
				passwordList.add(pw);

			}

		}
		
		log.info("username={}, password1={}", this.properties.getUsername(), this.properties.getPassword());

	}

	@Override

	protected void doSend(MimeMessage[] mimeMessage, Object[] object) throws MailException {

		super.setJavaMailProperties(asProperties(this.properties.getProperties()));
		
		super.setUsername(usernameList.get(currentMailId));

		super.setPassword(passwordList.get(currentMailId));
		log.info("username={}, password={}", super.getUsername(), super.getPassword());

// 设置编码和各种参数

		super.setHost(this.properties.getHost());
		super.setPort(this.properties.getPort());

		super.setDefaultEncoding(this.properties.getDefaultEncoding().name());


		super.doSend(mimeMessage, object);

// 轮询

		currentMailId = (currentMailId + 1) % usernameList.size();

	}

	private Properties asProperties(Map<String, String> source) {
		Properties properties = new Properties();

		properties.putAll(source);

		return properties;

	}

	@Override
	public String getUsername() {
		return usernameList.get(currentMailId);

	}
	@Override
	public String getPassword() {
		return passwordList.get(currentMailId);

	}
	
	public int getCurrentMailId() {
		return currentMailId;
	}

}
