package org.getopt.luke;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.lucene.index.IndexGate;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.index.IndexGate.FormatDetails;
import org.apache.lucene.index.IndexReader.FieldOption;

public class IndexInfo {
  private IndexReader reader;
  private String indexPath;
  private long totalFileSize;
  private int numTerms;
  private int indexFormat;
  private FormatDetails formatDetails;
  private TermInfo[] topTerms;
  private List<String> fieldNames;
  private String lastModified;
  
  public IndexInfo(IndexReader reader, String indexPath) throws Exception {
    this.reader = reader;
    this.indexPath = indexPath;
    lastModified = new Date(IndexReader.lastModified(reader.directory())).toString();
    totalFileSize = Util.calcTotalFileSize(indexPath, reader.directory());
    numTerms = 0;
    TermEnum te = reader.terms();
    while (te.next())
      numTerms++;
    te.close();
    fieldNames = new ArrayList<String>();
    fieldNames.addAll(reader.getFieldNames(FieldOption.ALL));
    Collections.sort(fieldNames);
    indexFormat = IndexGate.getIndexFormat(reader.directory());
    formatDetails = IndexGate.getFormatDetails(indexFormat);
    topTerms = HighFreqTerms.getHighFreqTerms(reader.directory(), null, 51, (String[])fieldNames.toArray(new String[fieldNames.size()]));
  }

  /**
   * @return the reader
   */
  public IndexReader getReader() {
    return reader;
  }

  /**
   * @return the indexPath
   */
  public String getIndexPath() {
    return indexPath;
  }

  /**
   * @return the totalFileSize
   */
  public long getTotalFileSize() {
    return totalFileSize;
  }

  /**
   * @return the numTerms
   */
  public int getNumTerms() {
    return numTerms;
  }

  /**
   * @return the indexFormat
   */
  public int getIndexFormat() {
    return indexFormat;
  }

  /**
   * @return the formatDetails
   */
  public FormatDetails getFormatDetails() {
    return formatDetails;
  }

  /**
   * @return the topTerms
   */
  public TermInfo[] getTopTerms() {
    return topTerms;
  }

  /**
   * @return the fieldNames
   */
  public List<String> getFieldNames() {
    return fieldNames;
  }

  /**
   * @return the lastModified
   */
  public String getLastModified() {
    return lastModified;
  }
  
  

}
