/*
 * The MIT License
 *
 * Copyright (c) 2009 The Broad Institute
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package net.sf.picard.metrics;

import net.sf.picard.PicardException;
import net.sf.picard.util.FormatUtil;
import net.sf.picard.util.Histogram;
import net.sf.samtools.util.StringUtil;

import java.io.*;
import java.lang.reflect.Field;
import java.util.*;

/**
 * Contains a set of metrics that can be written to a file and parsed back
 * again. The set of metrics is composed of zero or more instances of a class,
 * BEAN, that extends {@link MetricBase} (all instances must be of the same type)
 * and may optionally include one or more histograms that share the same key set.
 *
 * @author Tim Fennell
 */
public class MetricsFile<BEAN extends MetricBase, HKEY extends Comparable> {
    public static final String MAJOR_HEADER_PREFIX = "## ";
    public static final String MINOR_HEADER_PREFIX = "# ";
    public static final String SEPARATOR = "\t";
    public static final String HISTO_HEADER = "## HISTOGRAM\t";
    public static final String METRIC_HEADER = "## METRICS CLASS\t";

    private final Set<String> columnLabels = new HashSet<String>();
    private final List<Header> headers = new ArrayList<Header>();
    private final List<BEAN> metrics = new ArrayList<BEAN>();
    private final List<Histogram<HKEY>> histograms = new ArrayList<Histogram<HKEY>>();

    /** Adds a header to the collection of metrics. */
    public void addHeader(Header h) { this.headers.add(h); }

    /** Returns the list of headers. */
    public List<Header> getHeaders() { return Collections.unmodifiableList(this.headers); }

    /** Adds a bean to the collection of metrics. */
    public void addMetric(final BEAN bean) { this.metrics.add(bean); }

    /** Returns the list of headers. */
    public List<BEAN> getMetrics() { return Collections.unmodifiableList(this.metrics); }

    public Set<String> getMetricsColumnLabels() { return Collections.unmodifiableSet(this.columnLabels); }

    /** Returns the histogram contained in the metrics file if any. */
    public Histogram<HKEY> getHistogram() {
        if (histograms.size() > 0) return this.histograms.get(0);
        else return null;
    }

    /** Sets the histogram contained in the metrics file. */
    public void setHistogram(final Histogram<HKEY> histogram) {
        if (this.histograms.isEmpty()) {
            if (histogram != null) this.histograms.add(histogram);
        }
        else {
            this.histograms.set(0, histogram);
        }
    }

    /** Adds a histogram to the list of histograms in the metrics file. */
    public void addHistogram(final Histogram<HKEY> histogram) {
        this.histograms.add(histogram);
    }

    //** Returns an unmodifiable version of the histogram list */
    public List<Histogram<HKEY>> getAllHistograms() {
        return Collections.unmodifiableList(histograms);
    }

    /** Returns the number of histograms added to the metrics file. */
    public int getNumHistograms() 
    {
    	return this.histograms.size();
    }
    
    /** Returns the list of headers with the specified type. */
    public List<Header> getHeaders(final Class<? extends Header> type) {
        List<Header> tmp = new ArrayList<Header>();
        for (final Header h : this.headers) {
            if (h.getClass().equals(type)) {
                tmp.add(h);
            }
        }

        return tmp;
    }

    /**
     * Writes out the metrics file to the supplied file. The file is written out
     * headers first, metrics second and histogram third.
     *
     * @param f a File into which to write the metrics
     */
    public void write(final File f) {
        FileWriter w = null;
        try {
            w = new FileWriter(f);
            write(w);
        }
        catch (IOException ioe) {
            throw new PicardException("Could not write metrics to file: " + f.getAbsolutePath(), ioe);
        }
        finally {
            if (w != null) {
                try {
                    w.close();
                } catch (IOException e) {
                }
            }
        }
    }

    /**
     * Writes out the metrics file to the supplied writer. The file is written out
     * headers first, metrics second and histogram third.
     *
     * @param w a Writer into which to write the metrics
     */
    public void write(final Writer w) {
        try {
            final FormatUtil formatter = new FormatUtil();
            final BufferedWriter out = new BufferedWriter(w);
            printHeaders(out);
            out.newLine();

            printBeanMetrics(out, formatter);
            out.newLine();

            printHistogram(out, formatter);
            out.newLine();
            out.flush();
        }
        catch (IOException ioe) {
            throw new PicardException("Could not write metrics file.", ioe);
        }
    }

    /** Prints the headers into the provided PrintWriter. */
    private void printHeaders(final BufferedWriter out) throws IOException {
        for (final Header h : this.headers) {
            out.append(MAJOR_HEADER_PREFIX);
            out.append(h.getClass().getName());
            out.newLine();
            out.append(MINOR_HEADER_PREFIX);
            out.append(h.toString());
            out.newLine();
        }
    }

    /** Prints each of the metrics entries into the provided PrintWriter. */
    private void printBeanMetrics(final BufferedWriter out, final FormatUtil formatter) throws IOException {
        if (this.metrics.isEmpty()) {
            return;
        }

        // Write out a header row with the type of the metric class
        out.append(METRIC_HEADER + getBeanType().getName());
        out.newLine();

        // Write out the column headers
        final Field[] fields = getBeanType().getFields();
        final int fieldCount = fields.length;

        for (int i=0; i<fieldCount; ++i) {
            out.append(fields[i].getName());
            if (i < fieldCount - 1) {
                out.append(MetricsFile.SEPARATOR);
            }
            else {
                out.newLine();
            }
        }

        // Write out each of the data rows
        for (final BEAN bean : this.metrics) {
            for (int i=0; i<fieldCount; ++i) {
                try {
                    final Object value = fields[i].get(bean);
                    out.append(StringUtil.assertCharactersNotInString(formatter.format(value), '\t', '\n'));

                    if (i < fieldCount - 1) {
                        out.append(MetricsFile.SEPARATOR);
                    }
                    else {
                        out.newLine();
                    }
                }
                catch (IllegalAccessException iae) {
                    throw new PicardException("Could not read property " + fields[i].getName()
                            + " from class of type " + bean.getClass());
                }
            }
        }

        out.flush();
    }

    /** Prints the histogram if one is present. */
    private void printHistogram(final BufferedWriter out, final FormatUtil formatter) throws IOException {
        final List<Histogram<HKEY>> nonEmptyHistograms = new ArrayList<Histogram<HKEY>>();
        for (final Histogram<HKEY> histo : this.histograms) {
            if (!histo.isEmpty()) nonEmptyHistograms.add(histo);
        }

        if (nonEmptyHistograms.isEmpty()) {
            return;
        }

        // Build a combined key set.  Assume comparator is the same for all Histograms
        final java.util.Set<HKEY> keys = new TreeSet<HKEY>(nonEmptyHistograms.get(0).comparator());
        for (final Histogram<HKEY> histo : nonEmptyHistograms) {
            if (histo != null) keys.addAll(histo.keySet());
        }

        // Add a header for the histogram key type
        out.append(HISTO_HEADER + nonEmptyHistograms.get(0).keySet().iterator().next().getClass().getName());
        out.newLine();

        // Output a header row
        out.append(StringUtil.assertCharactersNotInString(nonEmptyHistograms.get(0).getBinLabel(), '\t', '\n'));
        for (final Histogram<HKEY> histo : nonEmptyHistograms) {
            out.append(SEPARATOR);
            out.append(StringUtil.assertCharactersNotInString(histo.getValueLabel(), '\t', '\n'));
        }
        out.newLine();

        for (final HKEY key : keys) {
            out.append(key.toString());

            for (final Histogram<HKEY> histo : nonEmptyHistograms) {
                final Histogram<HKEY>.Bin bin = histo.get(key);
                final double value = (bin == null ? 0 : bin.getValue());

                out.append(SEPARATOR);
                out.append(formatter.format(value));
            }

            out.newLine();
        }
    }

    /** Gets the type of the metrics bean being used. */
    private Class<?> getBeanType() {
        if (this.metrics == null || this.metrics.isEmpty()) {
            return null;
        } else {
            return this.metrics.get(0).getClass();
        }
    }

    /** Reads the Metrics in from the given reader. */
    public void read(final Reader r) {
        final BufferedReader in = new BufferedReader(r);
        final FormatUtil formatter = new FormatUtil();
        String line = null;

        try {
            // First read the headers
            Header header = null;
            boolean inHeader = true;
            while ((line = in.readLine()) != null && inHeader) {
                line = line.trim();
                // A blank line signals the end of the headers, otherwise parse out
                // the header types and values and build the headers.
                if ("".equals(line)) {
                    inHeader = false;
                }
                else if (line.startsWith(MAJOR_HEADER_PREFIX)) {
                    if (header != null) {
                        throw new IllegalStateException("Consecutive header class lines encountered.");
                    }
                    
                    final String className = line.substring(MAJOR_HEADER_PREFIX.length()).trim();
                    try {
                        header = (Header) loadClass(className, true).newInstance();
                    }
                    catch (Exception e) {
                        throw new PicardException("Error load and/or instantiating an instance of " + className, e);
                    }
                }
                else if (line.startsWith(MINOR_HEADER_PREFIX)) {
                    if (header == null) {
                        throw new IllegalStateException("Header class must precede header value:" + line);
                    }
                    header.parse(line.substring(MINOR_HEADER_PREFIX.length()));
                    this.headers.add(header);
                    header = null;
                }
                else {
                    throw new PicardException("Illegal state. Found following string in metrics file header: " + line);
                }
            }

            //read space between starting headers and metrics
            while (line != null && !line.startsWith(MAJOR_HEADER_PREFIX)) {
                line = in.readLine();
            }


            if(line != null) {
                line = line.trim();
            
                // Then read the metrics if there are any
                if (line.startsWith(METRIC_HEADER)) {
                    // Get the metric class from the header
                    final String className = line.split(SEPARATOR)[1];
                    Class<?> type = null;
                    try {
                        type = loadClass(className, true);
                    }
                    catch (ClassNotFoundException cnfe) {
                        throw new PicardException("Could not locate class with name " + className, cnfe);
                    }

                    // Read the next line with the column headers
                    final String[] fieldNames = in.readLine().split(SEPARATOR);
                    Collections.addAll(columnLabels, fieldNames);
                    final Field[] fields = new Field[fieldNames.length];
                    for (int i=0; i<fieldNames.length; ++i) {
                        try {
                            fields[i] = type.getField(fieldNames[i]);
                        }
                        catch (Exception e) {
                            throw new PicardException("Could not get field with name " + fieldNames[i] +
                                " from class " + type.getName());
                        }
                    }

                    // Now read the values
                    while ((line = in.readLine()) != null) {
                        if ("".equals(line.trim())) {
                            break;
                        }
                        else {
                            String[] values = line.split(SEPARATOR, -1);
                            BEAN bean = null;

                            try { bean = (BEAN) type.newInstance(); }
                            catch (Exception e) { throw new PicardException("Error instantiating a " + type.getName(), e); }

                            for (int i=0; i<fields.length; ++i) {
                                Object value = null;
                                if (values[i] != null && values[i].length() > 0) {
                                    value = formatter.parseObject(values[i], fields[i].getType());
                                }

                                try { fields[i].set(bean, value); }
                                catch (Exception e) {
                                    throw new PicardException("Error setting field " + fields[i].getName() +
                                            " on class of type " + type.getName(), e);
                                }
                            }

                            this.metrics.add(bean);
                        }
                    }
                }
            }

            // Then read the histograms if any are present
            while (line != null && !line.startsWith(MAJOR_HEADER_PREFIX)) {
                line = in.readLine();
            }
            if (line != null && line.startsWith(HISTO_HEADER)) {
                // Get the key type of the histogram
                final String keyClassName = line.split(SEPARATOR)[1].trim();
                Class<?> keyClass = null;

                try { keyClass = loadClass(keyClassName, true); }
                catch (ClassNotFoundException cnfe) { throw new PicardException("Could not load class with name " + keyClassName); }

                // Read the next line with the bin and value labels
                final String[] labels = in.readLine().split(SEPARATOR);
                for (int i=1; i<labels.length; ++i) {
                    this.histograms.add(new Histogram<HKEY>(labels[0], labels[i]));
                }

                // Read the entries in the histograms
                while ((line = in.readLine()) != null && !"".equals(line)) {
                    final String[] fields = line.trim().split(SEPARATOR);
                    final HKEY key = (HKEY) formatter.parseObject(fields[0], keyClass);

                    for (int i=1; i<fields.length; ++i) {
                        final double value = formatter.parseDouble(fields[i]);
                        this.histograms.get(i-1).increment(key, value);
                    }
                }
            }
        }
        catch (IOException ioe) {
            throw new PicardException("Could not read metrics from reader.", ioe);
        }
    }

    /** Attempts to load a class, taking into account that some classes have "migrated" from the broad to sf. */
    private Class<?> loadClass(final String className, final boolean tryOtherPackages) throws ClassNotFoundException {
        // List of alternative packages to check in case classes moved around
        final String[] packages = new String[] {
                "edu.mit.broad.picard.genotype.concordance",
                "edu.mit.broad.picard.genotype.fingerprint",
                "edu.mit.broad.picard.ic",
                "edu.mit.broad.picard.illumina",
                "edu.mit.broad.picard.jumping",
                "edu.mit.broad.picard.quality",
                "edu.mit.broad.picard.samplevalidation",
                "net.sf.picard.analysis",
                "net.sf.picard.analysis.directed",
                "net.sf.picard.sam",
                "net.sf.picard.metrics"
        };

        try { return Class.forName(className); }
        catch (ClassNotFoundException cnfe) {
            if (tryOtherPackages) {
                for (final String p : packages) {
                    try {
                        return loadClass(p + className.substring(className.lastIndexOf(".")), false);
                    }
                    catch (ClassNotFoundException cnf2) {/* do nothing */}
                    // If it ws an inner class, try and see if it's a stand-alone class now
                    if (className.indexOf("$") > -1) {
                        try {
                            return loadClass(p + "." + className.substring(className.lastIndexOf("$") + 1), false);
                        }
                        catch (ClassNotFoundException cnf2) {/* do nothing */}
                    }
                }
            }

            throw cnfe;
        }
    }

    /** Checks that the headers, metrics and histogram are all equal. */
    @Override
    public boolean equals(final Object o) {
        if (o == null) {
            return false;
        }
        if (getClass() != o.getClass()) {
            return false;
        }
        final MetricsFile that = (MetricsFile) o;

        if (!areHeadersEqual(that)) {
            return false;
        }
        if (!areMetricsEqual(that)) {
            return false;
        }
        if (!areHistogramsEqual(that)) {
            return false;
        }

        return true;
    }

    public boolean areHeadersEqual(final MetricsFile that) {
        return this.headers.equals(that.headers);
    }

    public boolean areMetricsEqual(final MetricsFile that) {
        return this.metrics.equals(that.metrics);
    }

    public boolean areHistogramsEqual(final MetricsFile that) {
        return this.histograms.equals(that.histograms);
    }

    @Override
    public int hashCode() {
        int result = headers.hashCode();
        result = 31 * result + metrics.hashCode();
        return result;
    }

    /**
     * Convenience method to read all the Metric beans from a metrics file.
     * @param file to be read.
     * @return list of beans from the file.
     */
    public static List<? extends MetricBase> readBeans(final File file) {
        try {
            final MetricsFile<MetricBase, Comparable<?>> metricsFile = new MetricsFile<MetricBase, Comparable<?>>();
            metricsFile.read(new FileReader(file));
            return metricsFile.getMetrics();
        } catch (FileNotFoundException e) {
            throw new PicardException(e.getMessage(), e);
        }
    }
}
