/*
 * Copyright (c) 2007-2010 by The Broad Institute, Inc. and the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * This software is licensed under the terms of the GNU Lesser General Public License (LGPL), Version 2.1 which
 * is available at http://www.opensource.org/licenses/lgpl-2.1.php.
 *
 * THE SOFTWARE IS PROVIDED "AS IS." THE BROAD AND MIT MAKE NO REPRESENTATIONS OR WARRANTIES OF
 * ANY KIND CONCERNING THE SOFTWARE, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT
 * OR OTHER DEFECTS, WHETHER OR NOT DISCOVERABLE.  IN NO EVENT SHALL THE BROAD OR MIT, OR THEIR
 * RESPECTIVE TRUSTEES, DIRECTORS, OFFICERS, EMPLOYEES, AND AFFILIATES BE LIABLE FOR ANY DAMAGES OF
 * ANY KIND, INCLUDING, WITHOUT LIMITATION, INCIDENTAL OR CONSEQUENTIAL DAMAGES, ECONOMIC
 * DAMAGES OR INJURY TO PROPERTY AND LOST PROFITS, REGARDLESS OF WHETHER THE BROAD OR MIT SHALL
 * BE ADVISED, SHALL HAVE OTHER REASON TO KNOW, OR IN FACT SHALL KNOW OF THE POSSIBILITY OF THE
 * FOREGOING.
 */

package org.broad.igv.sam.reader;

import net.sf.samtools.SAMFileHeader;
import net.sf.samtools.SAMFileReader;
import net.sf.samtools.SAMRecord;
import net.sf.samtools.SAMSequenceRecord;
import net.sf.samtools.util.CloseableIterator;
import org.apache.log4j.Logger;
import org.broad.igv.exceptions.DataLoadException;
import org.broad.igv.sam.Alignment;
import org.broad.igv.ui.UIConstants;
import org.broad.igv.ui.util.MessageUtils;
import org.broad.igv.util.IGVHttpUtils;
import org.broad.igv.util.SeekableStreamFactory;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: jrobinso
 * Date: Sep 22, 2009
 * Time: 2:21:04 PM
 * To change this template use File | Settings | File Templates.
 */
public class BAMHttpQueryReader implements AlignmentQueryReader {

    static Logger log = Logger.getLogger(BAMHttpQueryReader.class);

    static Properties cachedEtags;

    URL url;
    SAMFileHeader header;
    File indexFile;
    SAMFileReader reader;

    public BAMHttpQueryReader(URL url) {
        this.url = url;
        indexFile = getIndexFile(url);
    }

    public void close() throws IOException {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public SAMFileHeader getHeader() {
        if (header == null) {
            loadHeader();
        }
        return header;
    }

    public boolean hasIndex() {
        return indexFile != null && indexFile.exists();
    }

    public Set<String> getSequenceNames() {
        SAMFileHeader header = getHeader();
        if (header == null) {
            return null;
        }
        Set<String> seqNames = new HashSet();
        List<SAMSequenceRecord> records = header.getSequenceDictionary().getSequences();
        if (records.size() > 0) {
            for (SAMSequenceRecord rec : header.getSequenceDictionary().getSequences()) {
                String chr = rec.getSequenceName();
                seqNames.add(chr);
            }
        }
        return seqNames;
    }

    public CloseableIterator<Alignment> iterator() {
        try {
            if (reader == null) {
                InputStream is = url.openStream();
                reader = new SAMFileReader(new BufferedInputStream(is));
            }
            return new WrappedIterator(reader.iterator());
        }
        catch (IOException e) {
            log.error("Error creating iterator", e);
            throw new RuntimeException(e);
        }

    }

    public CloseableIterator<Alignment> query(String sequence, int start, int end, boolean contained) {
        if (reader == null) {
            reader = new SAMFileReader(url, indexFile, false);
        }
        CloseableIterator<SAMRecord> iter = reader.query(sequence, start, end, contained);
        return new WrappedIterator(iter);
    }


    private void loadHeader() {

        InputStream is = null;
        SAMFileReader reader = null;
        try {
            Map<String, String> props = new HashMap();
            String byteRange = "bytes=0-1000000";
            props.put("Range", byteRange);

            is = SeekableStreamFactory.getStreamFor(url.toString()); //  IGVHttpUtils.openConnectionStream(url);
            BufferedInputStream bis = new BufferedInputStream(is);
            reader = new SAMFileReader(bis);
            header = reader.getFileHeader();

        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (reader != null) {
                    reader.close();
                }

            }
            catch (Exception e) {

            }
        }
    }


    // TODO -- revisit caching scehme,  do something for ftp loads

    File getIndexFile(URL bamURL) {

        try {
            String bamURLString = bamURL.toString();
            String urlString = bamURLString + ".bai";
            URL indexURL = new URL(urlString);
            String eTag = null;
            Properties eTagCache = getTagCache();

            if (urlString.toLowerCase().startsWith("http")) {
                eTag = IGVHttpUtils.getETag(indexURL);


                // Try other naming convention
                if (eTag == null) {
                    // Try other index convention
                    String baseName = bamURLString.substring(0, bamURLString.length() - 4);
                    indexURL = new URL(baseName + ".bai");

                    urlString = bamURL.toString().replace(".bam", ".bai");
                    indexURL = new URL(urlString);
                    eTag = IGVHttpUtils.getETag(indexURL);
                }
            }


            // Create a filename unique for this url;
            String idxFilename = getCachedIndexFilename(bamURLString);

            if (eTag != null) {
                File indexFile = new File(this.getCacheDirectory(), idxFilename);
                if (indexFile.exists()) {
                    String cachedETag = eTagCache.getProperty(urlString);
                    if (eTag == null || cachedETag == null || !eTag.equals(cachedETag)) {
                        indexFile.delete();
                        eTagCache.remove(urlString);
                        writeCachedProperties(eTagCache, "etags");
                    }
                }
            }

            indexFile = new File(this.getCacheDirectory(), idxFilename);
            if (!indexFile.exists()) {
                loadIndexFile(bamURL.toString(), indexFile);
                if (eTag != null) {
                    eTagCache.put(urlString, eTag);
                    writeCachedProperties(eTagCache, "etags");
                } else {
                    indexFile.deleteOnExit();
                }
            }
            return indexFile;
        } catch (MalformedURLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return null;
        }
    }

    private String getCachedIndexFilename(String bamURL) {

        Properties lookup = getIndexCache();
        String indexName = lookup.getProperty(bamURL);
        if (indexName == null) {
            int tmp = bamURL.lastIndexOf("/");
            String prefix = tmp > 0 ? bamURL.substring(tmp + 1) : "index_";
            indexName = prefix + System.currentTimeMillis() + ".bai";
            lookup.put(bamURL, indexName);
            writeCachedProperties(lookup, "index");
        }

        return indexName;
    }

    private void loadIndexFile(String bamURL, File indexFile) {
        InputStream is = null;
        OutputStream os = null;

        try {
            URL indexURL = new URL(bamURL + ".bai");
            os = new FileOutputStream(indexFile);
            try {
                is = indexURL.openStream();

            }
            catch (FileNotFoundException e) {
                // Try other index convention
                String baseName = bamURL.substring(0, bamURL.length() - 4);
                indexURL = new URL(baseName + ".bai");

                try {
                    is = indexURL.openStream();
                }
                catch (FileNotFoundException e1) {
                    MessageUtils.showMessage("Index file not found for file: " + bamURL);
                    throw new DataLoadException("Index file not found for file: " + bamURL, bamURL);
                }
            }
            byte[] buf = new byte[4 * 1024]; // 4K buffer
            int bytesRead;
            while ((bytesRead = is.read(buf)) != -1) {
                os.write(buf, 0, bytesRead);
            }

        }
        catch (IOException e) {
            MessageUtils.showMessage("Error loading index file: " + indexFile.getAbsolutePath());
            log.error("Error downloading index file ", e);

        }
        finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }

        }
    }


    // TODO -- move everything below to a utility class
    static File cacheDirectory = null;

    static synchronized File getCacheDirectory() {
        if (cacheDirectory == null) {
            File defaultDir = new File(UIConstants.DEFAULT_IGV_DIRECTORY);
            if (defaultDir.exists()) {
                cacheDirectory = new File(defaultDir, "bam");
                if (!cacheDirectory.exists()) {
                    cacheDirectory.mkdir();
                }
            }
        }
        return cacheDirectory;
    }

    static Properties tagCache = null;
    static Properties indexLookup = null;

    static synchronized Properties getTagCache() {
        if (tagCache == null) {
            tagCache = loadCachedProperties("etags");
        }
        return tagCache;
    }

    static synchronized Properties getIndexCache() {
        if (indexLookup == null) {
            indexLookup = loadCachedProperties("index");
        }
        return indexLookup;
    }

    private static Properties loadCachedProperties(String name) {
        File propFile = new File(getCacheDirectory(), name);
        Properties props = new Properties();
        if (propFile.exists()) {
            InputStream reader = null;
            try {
                reader = new FileInputStream(propFile);
                props.load(reader);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return props;
    }


    private static void writeCachedProperties(Properties props, String name) {
        OutputStream os = null;
        try {
            File propFile = new File(getCacheDirectory(), name);
            os = new FileOutputStream(propFile);
            props.store(os, null);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        }
    }
}
