/*
 * Copyright
 *   2009 axYus - www.axyus.com
 *   2009 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.dgfip.xemelios.batch.imports;

import fr.gouv.finances.dgfip.xemelios.batch.Batch;
import fr.gouv.finances.dgfip.xemelios.batch.BatchRunner;
import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOFull;
import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOPK;
import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOWrapper;
import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.XemImportsFilesVOFull;
import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.XemImportsFilesVOWrapper;
import fr.gouv.finances.dgfip.xemelios.common.Constants;
import fr.gouv.finances.dgfip.xemelios.common.PJRef;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentsModel;
import fr.gouv.finances.dgfip.xemelios.common.config.Loader;
import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import org.apache.log4j.Logger;

/**
 * Ce batch vérifie que toutes les conditions sont bien remplies pour réaliser un import
 * @author chm
 */
public class ImportChecker extends Batch {
    private final static Logger logger = Logger.getLogger(ImportChecker.class);
    public static final int MAX_CONCURRENT_THREADS = 3;
    private int maxConcurrentThreads = MAX_CONCURRENT_THREADS;
    
    private String importDirectory = null;
    private DocumentsModel documents;

    public ImportChecker(String[] args) {
        super(args);
    }

    @Override
    protected void initialize() throws Exception {
        super.initialize();
        String dirs = getProps().getProperty("documents-def.dir");
        File baseDir = BatchRunner.getBaseDir();
//        logger.debug("baseDir: "+baseDir);
        String canon = baseDir.getCanonicalPath().replace("\\", "/");
//        logger.debug("canon: "+canon);
//        logger.debug("dirs: "+dirs);
        String realDirs = dirs.replaceAll("%basedir%", canon);
//        logger.debug("realDirs: "+realDirs);
        logger.debug("reading document-def from "+realDirs);
        documents = Loader.getDocumentsInfos(realDirs);
        if(documents==null || documents.getDocuments().size()==0) {
            throw new RuntimeException("No documents configuration found into "+realDirs);
        }
        String sTmp = getProps().getProperty("max.concurrent.threads");
        if(sTmp!=null && sTmp.length()>0) {
            try {
                maxConcurrentThreads = Integer.parseInt(sTmp);
            } catch(Throwable t) {}
        }
    }

    @Override
    protected void doProcess() throws Exception {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        ArrayList<ImportCheckerImpl> threads = new ArrayList<ImportCheckerImpl>();
        try {
            con = PoolManager.getInstance().getConnection();
            String sqlParam = "SELECT PARAM_VALUE FROM PARAMETERS WHERE PARAM_NAME='import.directory'";
            st = con.createStatement();
            rs = st.executeQuery(sqlParam);
            if(rs.next()) {
                importDirectory = rs.getString(1);
                rs.close();
            } else {
                throw new Exception("parameter 'import.directory' not set !");
            }
            String sql = "SELECT IMPORT_ID FROM XEM_IMPORTS WHERE STATUS=0";
            rs = st.executeQuery(sql);
            while(rs.next()) {
                threads.add(new ImportCheckerImpl(rs.getString(1),importDirectory,documents));
            }
            ThreadPool<ImportCheckerImpl> pool = new ThreadPool<ImportCheckerImpl>(threads);
            pool.start(maxConcurrentThreads);
        } catch(SQLException sqlEx) {
            // TODO
        } finally {
            if(con!=null) PoolManager.getInstance().releaseConnection(con);
        }
    }

    @Override
    public String getResumeTraitement() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return "import.checker resume";
    }

    @Override
    public String typeTraitementRefCode() {
        return "import.checker";
    }

    protected class ImportCheckerImpl extends RunnableBatch {
        private final Logger logger = Logger.getLogger(ImportCheckerImpl.class);
        private String importId;
        private ImportVOFull importFull;
        private List<XemImportsFilesVOFull> files;
        // utilities
        private File importDirectory;
        private DocumentsModel documents;
        public ImportCheckerImpl(String importId,String importDirectory, DocumentsModel documents) {
            setName("importChecker<"+importId+">");
            this.importId=importId;
            this.importDirectory = new File(importDirectory);
            this.documents=documents;
        }

        @Override
        public void run() {
            Connection con = null;
            try {
                con = PoolManager.getInstance().getConnection();
                loadImportInfos(con);
                importFull.setStatus(1);
                ImportVOWrapper.updateImportVO(con, importFull);
//                if(checkFile()) {
//                    importFull.setStatus(6);
//                } else {
//                    importFull.setStatus(2);
//                }
                checkFile();
                ImportVOWrapper.updateImportVO(con, importFull);
            } catch(SQLException sqlEx) {
                logger.error("run<"+importId+">()", sqlEx);
            } finally {
                if(con!=null) PoolManager.getInstance().releaseConnection(con);
                getNotifier().notifyThreadStop(this);
            }
        }

        protected boolean checkFile() {
            File tmpDir = new File(System.getProperty("java.io.tmpdir"));
            File[] ret = new File[1];
            ret[0] = new File(new File(importDirectory,importId),importFull.getFileName());
            ImportContent ic = new ImportContent();
            for (int i = 0; i < ret.length; i++) {
                if (ret[i].getName().toLowerCase().endsWith(".zip")) {
                    if (ret[i].exists()) {
                        ZipFile zf = null;
                        try {
                            zf = new ZipFile(ret[i]);
                            for (Enumeration<? extends ZipEntry> enumer = zf.entries(); enumer.hasMoreElements();) {
                                ZipEntry ze = enumer.nextElement();
                                if (!ze.isDirectory()) {
                                    String fileName = ze.getName();
                                    String entryName = fileName.toLowerCase();
                                    fileName = fileName.replace(
                                            File.pathSeparatorChar, '_').replace(
                                            File.separatorChar, '_').replace(':',
                                            '|').replace('\'', '_').replace('/',
                                            '_');
                                    logger.debug(entryName);
                                    if(PJRef.isPJ(ze)) {
                                        PJRef pj = new PJRef(ze);
                                        pj.writeTmpFile(tmpDir,zf);
                                        ic.pjs.add(pj);
                                    } else if ((entryName.endsWith(documents.getDocumentById(importFull.getFileType()).getExtension().toLowerCase()) || entryName.endsWith(".xml")) && !fileName.startsWith("_")) {
                                        // on decompresse le fichier dans le
                                        // repertoire temporaire, comme ca il sera
                                        // supprime en quittant
                                        InputStream is = zf.getInputStream(ze);
                                        BufferedInputStream bis = new BufferedInputStream(is);
                                        File output = new File(tmpDir, fileName);
                                        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output));
                                        byte[] buffer = new byte[1024];
                                        int read = bis.read(buffer);
                                        while (read > 0) {
                                            bos.write(buffer, 0, read);
                                            read = bis.read(buffer);
                                        }
                                        bos.flush();
                                        bos.close();
                                        bis.close();
                                        ic.filesToImport.add(output);
                                    }
                                }
                            }
                            zf.close();
                        } catch (ZipException zEx) {
                            String errorMessage = "Le fichier "+ret[i].getName()+ " n'est pas une archive ZIP valide";
                            importFull.setErrorMessage(errorMessage);
                        } catch (IOException ioEx) {
                            String errorMessage = "Le fichier "+ret[i].getName()+" est illisible. Vérifiez que les noms des fichiers\ncontenus dans l'archive ne comportent pas de caractères spéciaux ou accentués.";
                            importFull.setErrorMessage(errorMessage);
                        } finally {
                            if(zf!=null) {
                                try { zf.close(); } catch(Throwable t) {}
                            }
                        }
                    }
                } else if(ret[i].getName().toLowerCase().endsWith(".gz")) {
                    try {
                        String fileName = ret[i].getName();
                        fileName = fileName.substring(0, fileName.length()-3);
                        File output = new File(tmpDir, fileName);
                        GZIPInputStream gis = new GZIPInputStream(new FileInputStream(ret[i]));
                        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output));
                        byte[] buffer = new byte[1024];
                        int read = gis.read(buffer);
                        while (read > 0) {
                            bos.write(buffer, 0, read);
                            read = gis.read(buffer);
                        }
                        bos.flush();
                        bos.close();
                        gis.close();
                        ic.filesToImport.add(output);
                    } catch(IOException ioEx) {
                        // nothing to do
                    }
                } else {
                    ic.filesToImport.add(ret[i]);
                }
            }
            if(ic.filesToImport.size()==1) {
                if(Constants.XEMELIOS_ARCHIVE_SIGN.equals(importFull.getFileType())) {
                    // Traitement des archives
                    importFull.setStatus(6);
                } else {
                    // Traitement des autres fichiers
                    boolean needToLookForCollBudg = false;
                    StringBuffer errors = new StringBuffer();
                    if(importFull.getCollectivite()==null || importFull.getCollectivite().length()==0) {
                        needToLookForCollBudg = true;
                    }
                    if(importFull.getCollectiviteLib()==null || importFull.getCollectiviteLib().length()==0) {
                        needToLookForCollBudg = true;
                    }
                    if(importFull.getBudget()==null || importFull.getBudget().length()==0) {
                        needToLookForCollBudg = true;
                    }
                    if(importFull.getBudgetLib()==null || importFull.getBudgetLib().length()==0) {
                        needToLookForCollBudg = true;
                    }
                    if(needToLookForCollBudg) {
                        BudgCollParser bcp = new BudgCollParser(ic.filesToImport.get(0), documents.getDocumentById(importFull.getFileType()));
                        bcp.investigate();
                        if(bcp.isInError()) {
                            importFull.setErrorMessage(bcp.getErrorMessage());
                            importFull.setStatus(5);
                        } else {
                            if(importFull.getCollectivite()==null || importFull.getCollectivite().length()==0) {
                                importFull.setCollectivite(bcp.getCollectivite());
                            }
                            if(importFull.getCollectiviteLib()==null || importFull.getCollectiviteLib().length()==0) {
                                importFull.setCollectiviteLib(bcp.getCollectiviteLib());
                            }
                            if(importFull.getBudget()==null || importFull.getBudget().length()==0) {
                                importFull.setBudget(bcp.getBudget());
                            }
                            if(importFull.getBudgetLib()==null || importFull.getBudgetLib().length()==0) {
                                importFull.setBudgetLib(bcp.getBudgetLib());
                            }
                        }
                    }
                }
            } else {
                // TODO: multi sub files
            }
            // TODO
            return false;
        }
        @SuppressWarnings("unchecked")
        public void loadImportInfos(Connection con) throws SQLException {
            ImportVOPK pk = new ImportVOPK(importId);
            importFull = ImportVOWrapper.getImportVOFullByPk(con, pk);
            files = XemImportsFilesVOWrapper.getAllXemImportsFilesVOFullBy(con, XemImportsFilesVOFull.class, "SELECT "+XemImportsFilesVOWrapper.getSelectFieldsClause()+" FROM XEM_IMPORTS_FILES WHERE IMPORT_ID='"+importId+"'");
        }
    }
    public static class ImportContent {
        ArrayList<File> filesToImport;
        ArrayList<PJRef> pjs;
        public ImportContent() {
            super();
            filesToImport = new ArrayList<File>();
            pjs = new ArrayList<PJRef>();
        }
        public void setFilesToImport(ArrayList<File> array){
            this.filesToImport=array;
        }
    }
}
