/*
 * Copyright
 *   2005 axYus - www.axyus.com
 *   2005 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.importers;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import fr.gouv.finances.dgfip.utils.Pair;
import fr.gouv.finances.dgfip.utils.StringUtilities;
import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
import fr.gouv.finances.dgfip.xemelios.common.Constants;
import fr.gouv.finances.dgfip.xemelios.common.DematTransform;
import fr.gouv.finances.dgfip.xemelios.common.FileInfo;
import fr.gouv.finances.dgfip.xemelios.common.ToolException;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
import fr.gouv.finances.dgfip.xemelios.common.config.EtatModel;
import fr.gouv.finances.dgfip.xemelios.common.config.ParameterModel;
import fr.gouv.finances.dgfip.xemelios.data.DataAccessException;
import fr.gouv.finances.dgfip.xemelios.data.DataConfigurationException;
import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
import fr.gouv.finances.dgfip.xemelios.data.impl.sqlconfig.TDocument;
import fr.gouv.finances.dgfip.xemelios.importers.transformers.EtatTransformeur;
import fr.gouv.finances.dgfip.xemelios.utils.FileUtils;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.dom.DOMResult;

/**
 * This class imports a document in the underlying data layer.
 * First it splits the document in small files that contains only one
 * importable element.
 * Then, if the "Etat" configuration defines an Xsl to apply before importing,
 * it applies it.
 * Finally, it delegates import to the data layer.
 *
 * This class has been fully-rewritten since Xemelios 3.0, replacing the old
 * <tt>DefaultImporteur</tt> one.
 *
 * @since 3.0
 *
 * @author chm
 */
public class ImporterWithXSLBeforeSplitFile extends EtatImporteur {
    private static Logger logger = Logger.getLogger(ImporterWithXSLBeforeSplitFile.class);
    
    //private Hashtable<DocumentModel,Pair> budgets=new Hashtable<DocumentModel,Pair>();
    //private Hashtable<DocumentModel,Pair> collectivites=new Hashtable<DocumentModel,Pair>();
    
    private File documentsConfigDir = null;
    
    public ImporterWithXSLBeforeSplitFile(XemeliosUser user) {
        super();
        this.user=user;
        documentsConfigDir = new File(System.getProperty(Constants.SYS_PROP_DOC_DEF_DIR));
    }
    
    @Override
    public void setDocument(DocumentModel dm) {
        this.dm = dm;
    }
    
    
    /**
     * Importe le fichier specifie.
     * @param f Le fichier a importer
     * @see fr.gouv.finances.cp.xemelios.importers.EtatImporteur#importe(java.io.File)
     */
    @Override
    protected FileInfo importFile(File f) throws Exception {
        getImpSvcProvider().startLongWait();
    	File fTmp = f;
    	for (ParameterModel pm:dm.getParameters().getParameters()) {
    		if ("pre-split-xsl-file".equals(pm.getName())) {
//                    String fileEncoding = getFileEncoding(f);
                    File tmpDir = new File(System.getProperty("java.io.tmpdir"));
                    tmpDir = new File(tmpDir,Constants.NOM_APP);
                    tmpDir.mkdirs();
                    File tmpDest = new File(tmpDir,fTmp.getName());
                
//                DematTransform.saxTransform(new FileInputStream(f), new FileInputStream(new File (new File(dm.getBaseDirectory()),pm.getValue())), fileEncoding, tmpDest);
//                fTmp = tmpDest;
//                break;  // on effectue une seule transformation
                
//                    InputStream is = DematTransform.transform(
//                            new FileInputStream(f),
//                            new FileInputStream(new File (new File(dm.getBaseDirectory()),pm.getValue())),fileEncoding);

//                    StringBuffer newText = new StringBuffer();
//                    int len = 0;
//                    byte[] buff = new byte[1024];
//                    do {
//                        len = is.read(buff);
//                        if(len>0) newText.append(new String(buff,0,len));
//                    } while (len > 0);

                    FileOutputStream fos = new FileOutputStream (tmpDest);
                    StreamResult sr = new StreamResult(fos);
                    Transformer trans = net.sf.saxon.TransformerFactoryImpl.newInstance().newTransformer(new StreamSource(new File (new File(dm.getBaseDirectory()),pm.getValue())));
//                    fos.write(newText.toString().getBytes(fileEncoding));
                    trans.transform(new StreamSource(f), sr);
//                    trans.transform(new StreamSource(f), new StreamResult(System.out));
                    fos.flush();
                    fos.close();

                    fTmp = tmpDest;
    		}
    	}
        getImpSvcProvider().endLongWait();
        // on redonne la main � la proc�dure standard, DefaultImporter
    	return oldImportFile(fTmp);
    }
    
    protected FileInfo oldImportFile (File f) throws Exception {
        // various inits
        getImpSvcProvider().startLongWait();
        String fileEncoding = getFileEncoding(f);
        String xmlVersion = getFileXmlVersion(f);
        File tmpDir = new File(System.getProperty("java.io.tmpdir"));
        tmpDir = new File(tmpDir,Constants.NOM_APP);
        tmpDir.mkdirs();
//        logger.debug("tmpDir="+tmpDir.getPath());
        
        File fHeader = File.createTempFile("header-","",tmpDir);
        File fRef = File.createTempFile("ref-","",tmpDir);
        File fFooter = File.createTempFile("footer-","",tmpDir);
        XmlSplitter xs = splitFile(fHeader,fRef, fFooter, tmpDir,f,fileEncoding);
        getImpSvcProvider().endLongWait();
        Pair collectivite = new Pair();Pair codeBudget = new Pair();
        
        if(col == null){
        	collectivite = xs.getCollectivite();
        } else {
        	collectivite = col;
        }
        
        if(bdg == null){
        	codeBudget = xs.getBudget();
        } else {
        	codeBudget = bdg;
        }
        
        Vector<File> dataFiles = xs.getDataFiles();
        String header = FileUtils.readTextFile(fHeader,fileEncoding);
        String footer = FileUtils.readTextFile(fFooter,fileEncoding);
        String ref = FileUtils.readTextFile(fRef,fileEncoding);
        
        int docCount=1;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        
// load documents
        TDocument pc = DataLayerManager.getImplementation().getPersistenceConfig(dm,getUser()).getLayer(DataLayerManager.getImplementation().getLayerName()).getDocument(dm.getId());
        if(collectivite==null || collectivite.key==null || collectivite.libelle==null) {
            try {
                collectivite = getCollectivite(dm,f.getName(),collectivite);
            } catch(ToolException tEx) {
                if(tEx.getErrorCode()==ToolException.ERROR_INTERRUPTED)
                    getImpSvcProvider().displayMessage(tEx.getMessage(),ImportServiceProvider.MSG_TYPE_INFORMATION);
                removeTempDir();
                return null;
            }
        }
        if(codeBudget==null  || codeBudget.key==null || codeBudget.libelle==null || codeBudget.libelle.equals("")) {
            try {
                codeBudget = getBudget(dm,collectivite,f.getName(),codeBudget);
            } catch(ToolException tEx) {
                if(tEx.getErrorCode()==ToolException.ERROR_INTERRUPTED)
                    getImpSvcProvider().displayMessage(tEx.getMessage(),ImportServiceProvider.MSG_TYPE_INFORMATION);
                removeTempDir();
                return null;
            }
        }
        
        
        
        // check if document exists, and if so, if we must remove it
        if(DataLayerManager.getImplementation().isDocumentExists(dm,codeBudget,collectivite,f.getName(),getUser())) {
            boolean shouldImport = false;
            try {
                shouldImport = getOverwrite(f.getName());
                if(shouldImport) {
                    // this can be very long, display the infinite waiter
                    getImpSvcProvider().startLongWait();
                    DataLayerManager.getImplementation().removeDocument(dm,codeBudget,collectivite,f.getName(),getUser());
                    getImpSvcProvider().endLongWait();
                } else {
                    return new FileInfo();
                }
            } catch(DataAccessException daEx) {
                logger.error("DataAcessException while removing:",daEx.getCause());
                daEx.getCause().printStackTrace();
            } catch(DataConfigurationException dcEx) {
                logger.error("DataConfigurationException while removing:",dcEx.getCause());
                dcEx.getCause().printStackTrace();
            } catch(Exception ex) {
                // si exception ici, l'utilisateur veut annuler
                logger.error(ex);
                ex.printStackTrace();
                removeTempDir();
                return null;
            }
        }
        
        DataLayerManager.getImplementation().registerBudgetCollectivite(dm,codeBudget,collectivite,xs.getParentcollectivites(),f.getName(),getUser());
        DataLayerManager.getImplementation().saveSpecialKeys(dm,codeBudget,collectivite,f.getName(),xs.getSpecialKey1(),xs.getSpecialKey2(),xs.getSpecialKey3(),getUser());
        
        for(int fileCount=0;fileCount<dataFiles.size();fileCount++) {
            if(isCancelled()) break;
            File df=dataFiles.get(fileCount);
            final int progress = (int)((fileCount+1)*100.0/dataFiles.size());
            this.getImpSvcProvider().pushCurrentProgress(progress);
            String data = FileUtils.readTextFile(df,fileEncoding);
            StringBuffer fullText = new StringBuffer();
            fullText.append("<?xml version=\"").append(xmlVersion).append("\" encoding=\"").append(fileEncoding).append("\"?>");
            fullText.append(header).append(data).append(footer);
            String sFullText = fullText.toString();
            byte[] bData = sFullText.getBytes(fileEncoding);
            
            Document doc = db.parse(new ByteArrayInputStream(bData));
            
            // il faut retrouver de quel etat est ce document
            // on cherche si la balise root contient un dm.getEtatxxx().getBalise()
            EtatModel currentEtat = null;
            for(EtatModel em:dm.getEtats()) {
                String balise = em.getBalise();
                NodeList nl = doc.getElementsByTagName(balise);
                if(nl.getLength()>0) {
                    currentEtat = em;
                    break;
                } else {
                    nl = doc.getElementsByTagNameNS(dm.getNamespaces().getNamespaceURI(""),balise);
                    if(nl.getLength()>0) {
                        currentEtat = em;
                        break;
                    }
                }
            }
//logger.debug("EtatModel="+currentEtat.getId());
            // apply before-import xslt
            if(pc.getEtat(currentEtat.getId()).getImportXsltBuffer()!=null) {
            	/**
            	 * Un etat peut avoir un attribut indiquant que l'on doit appeler une classe Java afin de transformer une partie du flux.
            	 * Par exemple, la classe qui prends en compte les PJ du PES_ALLER.
            	 */
            	if (currentEtat.getTransformClass() != null) {
            		Class clazz = Class.forName(currentEtat.getTransformClass());
            		Object o = clazz.newInstance();
            		EtatTransformeur et = (EtatTransformeur)o;
            		et.transform(collectivite, fileEncoding, xmlVersion, dm.getBaseDirectory(), sFullText,user);
            		if (et.getReturnCode()!=EtatTransformeur.RETURN_CODE_OK) throw new Exception ("Erreur lors de la transformation du flux");
            	}

//                InputStream is = DematTransform.transform(new ByteArrayInputStream(sFullText.getBytes(fileEncoding)),new ByteArrayInputStream(pc.getEtat(currentEtat.getId()).getImportXsltBuffer().replaceAll("##output-encoding##",fileEncoding).replaceAll("##xml-version##",xmlVersion).getBytes()),fileEncoding,parameters);
                TransformerFactory tf = new net.sf.saxon.TransformerFactoryImpl();
                File directory = new File (currentEtat.getParent().getBaseDirectory());
                File xslFile = new File(directory,pc.getEtat(currentEtat.getId()).getImportXsltFile());
                Transformer trans = tf.newTransformer(new StreamSource(xslFile));
                if(codeBudget!=null) {
                    trans.setParameter("CodeBudget",codeBudget.key);
                    trans.setParameter("LibelleBudget",codeBudget.libelle);
                }
                if(collectivite!=null) {
                    trans.setParameter("CodeCollectivite",collectivite.key);
                    trans.setParameter("LibelleCollectivite",collectivite.libelle);
                }
                trans.setOutputProperty(OutputKeys.ENCODING, fileEncoding);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                trans.transform(new StreamSource(new ByteArrayInputStream(sFullText.getBytes(fileEncoding))), new StreamResult(baos));
                bData = baos.toByteArray();
            }
            
            String docName = StringUtilities.removeFileNameSuffix(f.getName())+"-"+docCount+"."+dm.getExtension();
            if(!isCancelled()) {
                try {
                    if(!DataLayerManager.getImplementation().importElement(dm,currentEtat,codeBudget,collectivite,f.getName(),docName,bData,fileEncoding,getUser())) {
                        logger.warn(DataLayerManager.getImplementation().getLastWarnings());
                        warningCount++;
                    }
                } catch(Throwable t) {
                    logger.error("importing element:",t);
                }
            }
            df.delete();
            docCount++;
        }
        logger.debug("importing repository");
        if(!isCancelled()) {
            StringBuffer fullText = new StringBuffer();
            fullText.append("<?xml version=\"").append(xmlVersion).append("\" encoding=\"").append(fileEncoding).append("\" ?>");
            fullText.append(header).append(ref).append(footer);
            Document doc = db.parse(new ByteArrayInputStream(fullText.toString().getBytes(fileEncoding)));
            if(pc.getRepositoryImportXsltFile()!=null && pc.getRepositoryImportXsltFile().length()>0) {
                Document doc2 = null;
                File xslNomencl = new File(documentsConfigDir,pc.getRepositoryImportXsltFile());
                TransformerFactory tf = new net.sf.saxon.TransformerFactoryImpl();
                Transformer trans = tf.newTransformer(new StreamSource(xslNomencl));
                DOMResult result = new DOMResult();
                DOMSource source = new DOMSource(doc);
                trans.transform(source, result);
                doc2 = (Document)result.getNode();
                if(doc2!=null) doc=doc2;
            }
            fullText = null;
            // String docName = "nomenclature";
            DataLayerManager.getImplementation().saveRepository(dm,collectivite,doc,xs.getReferenceNomenclature(),getUser());
        } else {
            logger.debug("cancelled");
        }
        fHeader.delete();
        fRef.delete();
        fFooter.delete();
        tmpDir.delete();
        f.deleteOnExit();
        return xs.getFileInfo();
    }
    
    protected XmlSplitter splitFile(File fHeader, File fRef, File fFooter, File tmpDir, File fToImport, String fileEncoding) throws FileNotFoundException, UnsupportedEncodingException, SAXException, ParserConfigurationException {
        XmlSplitter xs = new XmlSplitter(
                new FileOutputStream(fHeader),
                new FileOutputStream(fRef),
                new FileOutputStream(fFooter),
                tmpDir,dm,fileEncoding);
        SAXParserFactory sf = SAXParserFactory.newInstance();
        sf.setNamespaceAware(true);
        SAXParser parser = sf.newSAXParser();
        try {
            parser.parse(fToImport,xs);
        } catch(Exception ex) {
            ex.printStackTrace();
            String position = "line "+xs.getLocator().getLineNumber()+": ";
            throw new SAXException(position+ex.getMessage(),ex);
        }
        return xs;
    }
    protected void removeTempDir() {
        File tmpDir = new File(System.getProperty("java.io.tmpdir"));
        tmpDir = new File(tmpDir,Constants.NOM_APP);
        if(tmpDir.exists()) {
            File[] files = tmpDir.listFiles();
            for(File df:files) df.delete();
        }
    }
    public boolean getOverwrite(String docName) throws Exception {
        return getImpSvcProvider().getOverwrite(docName);
    }
    
    protected Pair getBudget(DocumentModel dm, Pair collectivite, String fileName,Pair budget) throws ToolException {
        return getImpSvcProvider().getBudget(dm,collectivite,fileName,budget);
    }
    protected Pair getCollectivite(DocumentModel dm, String fileName,Pair collectivite) throws ToolException {
        return getImpSvcProvider().getCollectivite(dm,fileName,collectivite);
    }
    
    private static String getFileEncoding(File f) throws IOException {
        String ret = null;
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
        byte[] bb = new byte[255];
        bis.read(bb);
        String buff = new String(bb);
        buff = buff.substring(buff.indexOf("<?"));
        buff = buff.substring(0,buff.indexOf("?>")+2);
        String header = buff;
        int encodingPos = header.indexOf("encoding=");
        if(encodingPos>=0) {
            String s = header.substring(encodingPos+9);
            if(s.startsWith("\"")) {
                ret=s.substring(1,s.indexOf('"',1));
            } else {
                s = s.substring(0,s.length()-2).trim();
                if(s.indexOf(' ')>0) ret = s.substring(0,s.indexOf(' '));
                else ret = s;
            }
        } else {
            ret = (new InputStreamReader(new FileInputStream(f))).getEncoding();
        }
        bis.close();
        logger.debug("encoding="+ret);
        return ret;
    }
    private static String getFileXmlVersion(File f) throws IOException {
        String ret = null;
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
        byte[] bb = new byte[255];
        bis.read(bb);
        String buff = new String(bb);
        buff = buff.substring(buff.indexOf("<?"));
        buff = buff.substring(0,buff.indexOf("?>")+2);
        String header = buff;
        int versionPos = header.indexOf("version=");
        if(versionPos>=0) {
            String s = header.substring(versionPos+8);
            if(s.startsWith("\"")) {
                ret=s.substring(1,s.indexOf('"',1));
            } else {
                s = s.substring(0,s.length()-2).trim();
                if(s.indexOf(' ')>0) ret = s.substring(0,s.indexOf(' '));
                else ret = s;
            }
        } else {
            ret = (new InputStreamReader(new FileInputStream(f))).getEncoding();
        }
        bis.close();
        logger.debug("version="+ret);
        return ret;
    }
    
    public static InputStream transform(Node xmlNode, InputStream xslStream, String encoding) throws IOException {
        TransformerFactory tFactory = net.sf.saxon.TransformerFactoryImpl.newInstance();
        Transformer transformer;
        try {
            transformer = tFactory.newTransformer(new StreamSource(xslStream));
        } catch (TransformerConfigurationException e) {
            logger.error(e);
            return null;
        }
        ByteArrayOutputStream outArray = new ByteArrayOutputStream();
        try {
            transformer.transform(new DOMSource(xmlNode), new StreamResult(outArray));
        } catch (TransformerException e) {
            logger.error(e);
            return null;
        }
        
        return new ByteArrayInputStream(outArray.toString(encoding).getBytes());
    }
    @Override
    public String getAdditionalMessage() {
        return "";
    }
    @Override
    protected void postProcess(boolean isErrorOccured) throws Exception {
        logger.info("L'import a prit "+((System.currentTimeMillis()-start)/1000/60.0)+" mins");
    }
}
