package nxm.sys.test.junit.prim;

import java.util.ArrayList;

import nxm.sys.inc.MapKBT;
import nxm.sys.lib.Args;
import nxm.sys.lib.Command;
import nxm.sys.lib.Data;
import nxm.sys.lib.DataFile;
import nxm.sys.lib.Midas;
import nxm.sys.lib.Shell;
import nxm.sys.test.junit.NMTestCase;

/**
 * Test for convertPrimitive
 * @author Kevin Esterheld
 * @since NeXtMidas 3.9.1
 *
 */
public class convertTest extends NMTestCase{

  public convertTest(String methodName) {
    super(methodName, null);
  }

  /** Creates a new instance of the class.
      @param methodName The name of the method being called.
      @param midas      The midas context testTo use when running the tests.
   */
  public convertTest(String methodName, Midas midas) {
    super(methodName, midas);
  }

  /**
   *  This test will make sure that when converting a type 2000 file
   *  the size of the buffer is done correctly and does not
   *  throw an indexOutOfBoundsException
   *  @since 3.9.1
   */
  public void testConvertType () {
    String command1 = "waveform out=wave.tmp{aux=ram}";
    String command2 = "FFT/PSD in1=wave.tmp{aux=ram} out1=wavepsd.tmp{aux=ram}";
    String command3 = "convert in=wavepsd.tmp{aux=ram} conv=INT out=psdbyte.tmp{aux=ram}";
    // Command 1
    Args args = Args.parseCommand(Shell.getSharedMidasContext(), command1);
    Command cmd = Shell.loadCommand(getMidas(),args);
    Shell.execCommand(getMidas(), cmd);
    // Command 2
    args = Args.parseCommand(Shell.getSharedMidasContext(), command2);
    cmd = Shell.loadCommand(getMidas(),args);
    Shell.execCommand(getMidas(), cmd);
    // Command 3
    args = Args.parseCommand(getMidas(), command3);
    cmd = Shell.loadCommand(getMidas(),args);
    Shell.execCommand(getMidas(), cmd);
    DataFile dfwave = new DataFile(getMidas(), "wavepsd.tmp{aux=ram}");
    DataFile dfpsd = new DataFile(getMidas(), "psdbyte.tmp{aux=ram}");
    dfpsd.open();
    Data dfdata = dfpsd.getDataBuffer((int)dfpsd.size);
    dfwave.open();
    Data dfdatawave = dfwave.getDataBuffer((int)(dfwave.size/2));
    dfwave.read(dfdatawave);
    dfpsd.read(dfdata);
    for(int i = 0;i<dfdata.buf.length;i++) {
      if (i%1024==18) assertEquals(7, dfdata.buf[i]);
      else if (i%1024==20) assertEquals(59, dfdata.buf[i]);
      else if (i%1024==22) assertEquals(29, dfdata.buf[i]);
      else assertEquals("The index is "+i, 0,dfdata.buf[i]);
    }

  }

  /** test XML2MAPKBT */
  public void testXmlToMapKBT () {
    // convert command
    String command = "convert nxm.sys.test.test_convert_dot_in_key.xml xml2mapkbt mapkbt";
    Shell.runCommand(getMidas(), command);

    // Get mapkbt from results table
    Object obj = getMidas().getResults().get("MAPKBT");

    // Check if mapkbt is an instance of MapKBT
    boolean isMapKBT = obj instanceof MapKBT ? true : false;
    assertEquals("Object is MapKBT", true, isMapKBT);

    // If mapkbt is an instance of MapKBT, parse the mapkbt
    if (isMapKBT) {
      MapKBT mapkbt = (MapKBT)obj;
      Object entry = ((ArrayList<?>)((MapKBT)mapkbt.get("XML")).get("RELEASE.SERIES")).get(1);
      assertEquals("MapKBT with '.' in Keys values correct", "beta series", ((MapKBT)entry).get("TYPE.OF.RELEASE"));
      entry = ((ArrayList<?>)((MapKBT)mapkbt.get("XML")).get("RELEASE.SERIES")).get(0);
      assertEquals("MapKBT with '.' in Keys values correct", "4.0.X", ((MapKBT)entry).get("Version.Number"));
    }
  }
}
