package com.storlead.sales.mail.connection;

import com.sun.mail.imap.IMAPFolder;
import lombok.extern.log4j.Log4j2;

import javax.mail.*;
import javax.mail.search.MessageIDTerm;
import javax.mail.search.SearchTerm;
import java.util.Objects;
import java.util.Properties;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-05-30 14:55
 */
@Log4j2
public class IMAPConnection 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  IMAPFolder folder;

    public IMAPConnection(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;
    }

    public IMAPConnection(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;
    }

    @Override
    public void connect() throws MessagingException {
        Properties properties = new Properties();
        properties.put("mail.store.protocol", "imap");
        properties.put("mail.imap.host", host);
        properties.put("mail.imap.port", port);
        properties.put("mail.imap.starttls.enable",tls);
        properties.put("mail.imap.ssl.enable", ssl);

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

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

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

    @Override
    public IMAPFolder getFolder(String folderName) {
        try {
            if (Objects.isNull(this.store)) {
                return null;
            }
            this.folder = (IMAPFolder) this.store.getFolder(folderName);
            return  this.folder;
        }catch (Exception e) {
            return null;
        }
    }

    @Override
    public Message getMessageByUid(String uid,String folderName) {
        try {
            if (Objects.isNull(this.folder)) {
                this.folder = getFolder(folderName);
            }
            if (!this.folder.isOpen()) {
                this.folder.open(Folder.READ_ONLY);
            }
            Message message = this.folder.getMessageByUID(Long.parseLong(uid));
            return message;
        } catch (MessagingException e) {
            log.error("getMessageByUid -- error --",e);
            return null;
        }
    }

    @Override
    public String getMessageUid(Message message) {
        try {
            Long uid = this.folder.getUID(message);
            if (Objects.isNull(uid)) {
                return null;
            }
            return String.valueOf(uid);
        } catch (Exception e) {
            return null;
        }
    }
}
