/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hu.nir.nmail.services;

import hu.nir.nmail.dbm.DataManager;
import hu.nir.nmail.ejb.entity.MailFolder;
import hu.nir.nmail.ejb.entity.NmailUser;
import hu.nir.nmail.ejb.stateless.NmailUserManager;
import hu.nir.nmail.spring.beans.NmailInfo;
import hu.nir.nmail.spring.beans.POP3Data;
import hu.nir.nmail.utils.NmailUtility;
import java.util.Map;

/**
 *
 * @author attila
 */
public class POP3Service implements NmailService {
    private DataManager dataManager;
    
    public Map<String, Object> processCommand(Object... commands) {
        NmailUser nmailUser = null;
        POP3Data pop3Data = null;
        
        for (Object command : commands) {
            if (command instanceof NmailUser) {
                nmailUser = (NmailUser) command;
                continue;
            }
            
            if (command instanceof POP3Data) {
                pop3Data = (POP3Data) command;
                continue;
            }
        }
        
        if (nmailUser == null || pop3Data == null) {
            return null;
        }
        
        MailFolder mailFolder = nmailUser.getMailFolder();
        mailFolder.setPop3Host(pop3Data.getPop3Host());
        mailFolder.setPop3Password(pop3Data.getPop3Password());
        mailFolder.setPop3UserName(pop3Data.getPop3UserName());
        mailFolder.setPort(pop3Data.getPort());
        mailFolder.setSsl(pop3Data.isSsl());
        
        NmailUserManager nmailUserManager = dataManager.getNmailUserManager();
        nmailUser.setMailFolder(mailFolder);
        nmailUserManager.update(nmailUser);
        
        NmailInfo nmailInfo = new NmailInfo();
        nmailInfo.addInfoCode("pop3.changed");
        
        return NmailUtility.toMap("pop3settings", pop3Data, "nmailInfo", nmailInfo);
    }

    public DataManager getDataManager() {
        return dataManager;
    }

    public void setDataManager(DataManager dataManager) {
        this.dataManager = dataManager;
    }

}
