package com.storlead.sales.mail.connection.client;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-05-30 16:26
 */
import com.storlead.sales.mail.connection.*;
import com.storlead.sales.mail.connection.config.MailProperties;
import lombok.extern.log4j.Log4j2;

import javax.mail.*;
import java.util.Arrays;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

@Log4j2
public class MailConnectionUtil {

    public static ConcurrentHashMap<String, MailConnection> chatMailConnectionMap = new ConcurrentHashMap<>();
    // Method to receive emails using IMAP

    public static MailConnection receiveEmailsConnection(MailProperties properties) {
        try {
//            if (Objects.nonNull(chatMailConnectionMap.get(properties.getEmailAddress()+"_"+properties.get))) {
//                return chatMailConnectionMap.get(properties.getEmailAddress());
//            }
            MailConnectionFactory factory = MailConnectionFactory.getFactory(properties.getProtocol(),properties.getHost(),properties.getPort(),properties.getEmailAddress(),properties.getEmailPassword());
            MailConnection imapConnection  = factory.createConnection();
            log.error("mailConnection -- testImap == "+properties.getProtocol()+"------"+imapConnection);
            imapConnection.connect();
//            chatMailConnectionMap.put(properties.getEmailAddress(),imapConnection);
            return imapConnection;
        } catch (MessagingException e) {
//            e.printStackTrace();
            log.error("connection error mail --"+properties.getEmailAddress(),e);
//            chatMailConnectionMap.remove(properties.getEmailAddress());
            return null;
        }
    }

    public static MailConnection testReceiveEmailsConnection(MailProperties properties) {
        try {
            MailConnectionFactory factory = MailConnectionFactory.getFactory(properties.getProtocol(),properties.getHost(),properties.getPort(),properties.getEmailAddress(),properties.getEmailPassword());
            MailConnection imapConnection  = factory.createConnection();
            log.error("mailConnection -- testImap == "+properties.getProtocol()+"------"+imapConnection);
            imapConnection.connect();
            return imapConnection;
        } catch (MessagingException e) {
//            e.printStackTrace();
            log.error("connection error mail --"+properties.getEmailAddress(),e);
            return null;
        }
    }

    public static MailConnection deleteEmailsConnection(MailProperties properties) {
        try {
            MailConnectionFactory factory = MailConnectionFactory.getFactory(properties.getProtocol(),properties.getHost(),properties.getPort(),properties.getEmailAddress(),properties.getEmailPassword());
            MailConnection imapConnection  = factory.createConnection();
            imapConnection.connect();
            return imapConnection;
        } catch (MessagingException e) {
            e.printStackTrace();
            return null;
        }
    }
//    public static void main(String[] args) {
//        String imapHost = "imap.exmail.qq.com";
//        String pop3Host = "pop.exmail.qq.com";
//        String smtpHost = "smtp.exmail.qq.com";
//        String port = "993";
//        String smtpPort = "465";
//        String user = "your_email@example.com"; // Replace with your email
//        String password = "your_password"; // Replace with your password
//
//        try {
//            System.out.println("Receiving emails using IMAP:");
//            receiveEmailsIMAP(imapHost, port, user, password);
//
//            System.out.println("Receiving emails using POP3:");
//            receiveEmailsPOP3(pop3Host, port, user, password);
//
//            System.out.println("Sending email using SMTP:");
//            sendEmailSMTP(smtpHost, smtpPort, user, password,
//                    "recipient@example.com", "Test Subject", "Test Message");
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}

