package com.alexanderlogistics;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Store;

import org.imixs.archive.importer.DocumentImportService;
import org.imixs.archive.importer.mail.IMAPBasicAuthenticator;
import org.imixs.archive.importer.mail.IMAPOutlookAuthenticator;
import org.imixs.workflow.ItemCollection;
import org.junit.Ignore;
import org.junit.Test;

import com.sun.mail.imap.IMAPFolder;

import junit.framework.Assert;

/**
 * This test class test the Imixs IMAPAuthenticator classes
 * 
 * @author rsoika
 * 
 */
public class TestIMAPAuhtenticators {

    private final static Logger logger = Logger.getLogger(TestIMAPAuhtenticators.class.getName());

    /**
     * Test IMAP connect via OAuth for email address imixs@alexander-logistics.com
     * 
     * https://techcommunity.microsoft.com/t5/exchange/javamail-connecting-to-office-365-xoauth2-for-imap/m-p/1505026
     */
    @Test
    public void testOutlookAuthenticator_ImixsAGL() {

        IMAPOutlookAuthenticator auth = new IMAPOutlookAuthenticator();

        ItemCollection source = new ItemCollection();
        source.setItemValue(DocumentImportService.SOURCE_ITEM_SERVER, "outlook.office365.com");
        source.setItemValue(DocumentImportService.SOURCE_ITEM_PORT, "993");
        source.setItemValue(DocumentImportService.SOURCE_ITEM_USER, "imixs@alexander-logistics.com");
        source.setItemValue(DocumentImportService.SOURCE_ITEM_PASSWORD, "A6x8Q~EWVUyt~gfyTHtIJf4ig.F9tajUpEEXMaCL");

        Properties sourceOptions = new Properties();
        sourceOptions.put("microsoft.tenantid", "51e2f038-0a96-41be-801d-bb4118aa018e");
        sourceOptions.put("microsoft.clientid", "39e9eec5-6939-47dc-9729-3438a9898e63");
        logger.info("Connect started....");
        try {
            Store store = auth.openMessageStore(source, sourceOptions);
            Assert.assertNotNull(store);
            Assert.assertTrue(store.isConnected());
            logger.info("Connect OK");

            // test open INBOX
            IMAPFolder inbox = (IMAPFolder) store.getFolder("INBOX");
            inbox.open(Folder.READ_WRITE);
            // test messages
            Message[] messages = inbox.getMessages();
            logger.log(Level.INFO, "... {0} new messages found", messages.length);

        } catch (MessagingException e) {
            e.printStackTrace();
            Assert.fail();

        }

    }

    /**
     * Test IMAP connect via OAuth for email address imixs@alexander-logistics.com
     * 
     * https://techcommunity.microsoft.com/t5/exchange/javamail-connecting-to-office-365-xoauth2-for-imap/m-p/1505026
     */
    @Test
    public void testOutlookAuthenticator_Imixs_bhvAGL() {

        IMAPOutlookAuthenticator auth = new IMAPOutlookAuthenticator();

        ItemCollection source = new ItemCollection();
        source.setItemValue(DocumentImportService.SOURCE_ITEM_SERVER, "outlook.office365.com");
        source.setItemValue(DocumentImportService.SOURCE_ITEM_PORT, "993");

        source.setItemValue(DocumentImportService.SOURCE_ITEM_USER, "Imixs_bhv@alexander-logistics.com");
        source.setItemValue(DocumentImportService.SOURCE_ITEM_PASSWORD, "A6x8Q~EWVUyt~gfyTHtIJf4ig.F9tajUpEEXMaCL");

        Properties sourceOptions = new Properties();
        sourceOptions.put("microsoft.tenantid", "51e2f038-0a96-41be-801d-bb4118aa018e");
        sourceOptions.put("microsoft.clientid", "39e9eec5-6939-47dc-9729-3438a9898e63");
        logger.info("Connect started....");

        try {
            Store store = auth.openMessageStore(source, sourceOptions);
            Assert.assertNotNull(store);
            Assert.assertTrue(store.isConnected());
            logger.info("Connect OK");

            // test open INBOX
            IMAPFolder inbox = (IMAPFolder) store.getFolder("INBOX");
            inbox.open(Folder.READ_WRITE);
            // test messages
            Message[] messages = inbox.getMessages();
            logger.log(Level.INFO, "... {0} new messages found", messages.length);
        } catch (MessagingException e) {
            e.printStackTrace();
            Assert.fail();

        }

    }

    /**
     * Test IMAP connect via Basic Authentication
     * <p>
     * NOTE: This test should fail in December 2022 as Basic Authentication is no
     * longer supported by Microsoft Outlook.
     * 
     */
    @Test
    @Ignore
    public void testBasicAuthenticator() {

        IMAPBasicAuthenticator auth = new IMAPBasicAuthenticator();

        ItemCollection source = new ItemCollection();
        source.setItemValue(DocumentImportService.SOURCE_ITEM_SERVER, "outlook.office365.com");
        source.setItemValue(DocumentImportService.SOURCE_ITEM_PORT, "993");
        source.setItemValue(DocumentImportService.SOURCE_ITEM_USER, "imixs@alexander-logistics.com");

        source.setItemValue(DocumentImportService.SOURCE_ITEM_PASSWORD, "i^i$oe-!+o1o0io(");

        Properties sourceOptions = new Properties();

        try {
            Store store = auth.openMessageStore(source, sourceOptions);
            Assert.assertNotNull(store);
        } catch (MessagingException e) {
            e.printStackTrace();
            Assert.fail();

        }

    }

}
