package com.storlead.sales.mail.connection;

import com.storlead.sales.mail.entity.EmailsEntity;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.pop3.POP3Folder;
import com.sun.mail.pop3.POP3Store;
import lombok.extern.log4j.Log4j2;

import javax.mail.*;
import javax.mail.search.MessageIDTerm;
import javax.mail.search.RecipientTerm;
import javax.mail.search.SearchTerm;
import java.util.Objects;
import java.util.Properties;
/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-05-30 15:14
 */
@Log4j2
public class POP3Connection implements MailConnection {
    private  Store store;
    private  Session session;
    private  String host;
    private  String port;
    private  String user;
    private  String password;
    private  Boolean tls;
    private  Boolean ssl;
    private  POP3Folder folder;

    public POP3Connection(String host, String port, String user, String password) {
        this.host = host;
        this.port = port;
        this.user = user;
        this.password = password;
        this.tls = true;
        this.ssl = true;
    }

    public POP3Connection(String host, String port, String user, String password,Boolean tls,Boolean ssl) {
        this.host = host;
        this.port = port;
        this.user = user;
        this.password = password;
        this.tls = tls;
        this.ssl = ssl;
    }
    @Override
    public void connect() throws MessagingException {
        Properties properties = new Properties();
        properties.put("mail.store.protocol", "pop3");
        properties.put("mail.pop3.host", host);
        properties.put("mail.pop3.port", port);
        properties.put("mail.pop3.starttls.enable", "true");
        properties.put("mail.pop3.ssl.enable", "true");

        Authenticator auth = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password);
            }
        };
        this.session = Session.getInstance(properties,auth);
        this.store = session.getStore("pop3");
        this.store.connect();
    }

    @Override
    public void close() {
        try {
            if (this.store != null && this.store.isConnected()) {
                this.store.close();
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Store getStore() {
        return this.store;
    }

    @Override
    public POP3Folder getFolder(String folderName) {
        try {
            if (Objects.isNull(this.store)) {
                log.error("store is null --- address ---"+this.user);
                return null;
            }
            this.folder = (POP3Folder)this.store.getFolder(folderName);
            return this.folder;
        }catch (Exception e) {
            log.error("store is null --- Exception ---"+this.user,e);
            return null;
        }
    }

    @Override
    public Message getMessageByUid(String messageId,String folderName) {
        try {
            if (Objects.isNull(this.folder)) {
                this.folder = getFolder(folderName);
            }
            SearchTerm searchTerm = new MessageIDTerm(messageId);
            if (!this.folder.isOpen()) {
                this.folder.open(Folder.READ_ONLY);
            }
            Message [] messages = folder.search(searchTerm);
            if (messages.length > 0) {
                return messages[0];
            }
            return null;
        } catch (MessagingException e) {
            log.error("getMessageByUid -- error",e);
            return null;
        }
    }

    @Override
    public String getMessageUid(Message message) {
        try {
           return this.folder.getUID(message);
        } catch (Exception e) {
            return null;
        }
    }
}

