package nxm.sys.test;

import nxm.sys.lib.DataFile;
import nxm.sys.lib.Shell;
import nxm.sys.lib.Table;

/** Class designed to provide users a means of evaluating future functionality
 *  @author Jeanette McCall
 *  @since NeXtMidas 3.8.1
 */
public class EvaluationUtil {

  private EvaluationUtil () {}

  /** Designed to generate trimmers needed to get the portion of a DataFile
   *  that is within the specified time boundaries
   *
   *  @param fileName name of file that needs trimmers
   *  @param tmin     min time as double
   *  @param  tmax     max time as double
   *  @return trimmers as String
   */
  public static String trimmersFromTimes (String fileName, double tmin, double tmax) {
    DataFile df1000 = new DataFile(Shell.getMidasContext(), fileName);
    df1000.open();
    double firstElement = df1000.getIndexAt(tmin);
    double secondElement = df1000.getIndexAt(tmax);
    df1000.close();
    String fileTrimmers = "("+firstElement+":"+secondElement+")";
    return fileTrimmers;
  }

  /** Get the qualifiers for a file based on the msg.data string that
   *  contains a table
   *
   *  @param dataTable string containing table that has qualifier information
   * @param  qualCSV   comma separated list of items that should appear in file qualifier
   * @return String representation of qualifiers to set on a file
   */
  public static String qualifiersFromData (String dataTable, String qualCSV) {
    String[] qualNames = qualCSV.split(",");
    int startTable = dataTable.indexOf('{');
    dataTable = dataTable.substring(startTable);
    Table data = new Table(dataTable);
    StringBuilder sb = new StringBuilder(200);
    sb.append("{");
    for (String qual : qualNames) {
      if (data.containsKey(qual)) {
        sb.append(qual+"="+data.getD(qual)+",");
      }
    }
    sb.replace(sb.length() -1 , sb.length(),"}");
    String qualifier = sb.toString();
    return qualifier;
  }

}
