/*
 * 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.NmailUser;
import hu.nir.nmail.spring.beans.NmailErrors;
import hu.nir.nmail.spring.beans.NmailInfo;
import hu.nir.nmail.threads.ImporterThread;
import hu.nir.nmail.utils.AddressStoreHelper;
import hu.nir.nmail.utils.NmailUtility;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author attila
 * 
 * A feltöltött csv fájlból importálja az email címeket.
 */
public class ImportService implements NmailService {
    private DataManager dataManager;
    private Logger logger = Logger.getLogger(ImportService.class.getName());
    
    public Map<String, Object> processCommand(Object... commands) {
        NmailUser nmailUser = null;
        MultipartFile multipartFile = null;
        NmailErrors nmailErrors = new NmailErrors();
        ApplicationContext applicationContext = null;
        Locale locale = null;
        
        for(Object command : commands) {
            if (command instanceof NmailUser) {
                nmailUser = (NmailUser) command;
                continue;
            }
            
            if (command instanceof MultipartFile) {
                multipartFile = (MultipartFile) command;
                continue;
            }
            
            if(command instanceof ApplicationContext) {
                applicationContext = (ApplicationContext) command;
                continue;
            }
            
            if (command instanceof Locale) {
                locale = (Locale) command;
                continue;
            }
        }
        
        if (nmailUser == null) {
            return null;
        }
        
        if (multipartFile == null) {
            nmailErrors.addErrorCode("import.nofile");
            return NmailUtility.toMap("nmailErrors", nmailErrors);
        }

        String contentType = multipartFile.getContentType();
        
        if (!contentType.equals("text/plain")) {
            nmailErrors.addErrorCode("import.incorrectfiletype");
            return NmailUtility.toMap("nmailErrors", nmailErrors);
        }
        
        try {
            InputStream inputStream = multipartFile.getInputStream();
            String message = null;
            NmailInfo nmailInfo = new NmailInfo();
            
            if (multipartFile.getSize() > 100*1024) {
                ImporterThread importerThread = new ImporterThread(dataManager, inputStream, nmailUser);
                importerThread.start();
                nmailInfo.addInfoCode("import.started");
            }
            else {
                AddressStoreHelper addressStoreHelper = new AddressStoreHelper(dataManager);
                int storedAddress = addressStoreHelper.storeAddresses(inputStream, nmailUser);
                Integer[] param = {storedAddress};
                message = applicationContext.getMessage("import.success", param, locale);
                nmailInfo.addInfoMessage(message);
            }
            
            return NmailUtility.toMap("nmailInfo", nmailInfo);
        } catch (IOException ex) {
            nmailErrors.addErrorCode("import.fileproceserror");
            logger.log(Level.SEVERE, ex.getMessage(), ex);
            return NmailUtility.toMap("nmailErrors", nmailErrors);
        }
    }

    public DataManager getDataManager() {
        return dataManager;
    }

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

}
