!******************************************************************************!
! Gets basic timeline information from the file and puts it in an "actual" table
!
! Arguments: s:file_name s:result_name
!
! @bug      NXM-3637 Wrapped BLUE TC timelines only writing entries since wrap
!                    Wrapped usec TIMELINE writing entries out in buffer order
!                      even when wrap is in the middle
! @since    NeXtMidas 3.9.4
! @author : Jeanette McCall

!******************************************************************************!
startmacro s:file_name s:result_name

  env get IOOptions origIOOptions
  env set IOOptions +SuppressInvalidTimeLineEntryErrors

  global ^result_name
  res file_name

  key ^file_name GET timeline usec_timeline TCSAMPLE_1 tc_first_offset TIMETAG.OFFSET plat_timeline

  res format ""
  res first_offset 0
  table ^result_name CREATE

  if usec_timeline NEQS NULL then
    res format MICROSECOND_PRECISION
    res ^{result_name}.FORMAT format
    invoke length usec_timeline.size
    calc last_index length 2 -
    calc l:num_entries last_index 2 /
    res ^{result_name}.NUM_ENTRIES num_entries
    res first_offset usec_timeline.2
    res ^{result_name}.FIRST_OFFSET first_offset
    res last_offset usec_timeline.^last_index
    res ^{result_name}.LAST_OFFSET last_offset

    if tc_first_offset NEQS NULL then
      ! have both keyword types!
      call maketctable "2"
    endif

    if plat_timeline NEQS NULL then
      ! have both keyword types!
      call makeplattable "2"
    endif

  elseif tc_first_offset NEQS NULL then
    call maketctable ""

  elseif plat_timeline NEQS NULL then
    call makeplattable ""
  endif

  env set IOOptions origIOOptions

endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return

!******************************************************************************!
! make the TC timeline table entries
!******************************************************************************!
procedure maketctable s:tnum
  res ^{result_name}.FORMAT^tnum BLUE_HIGH_PRECISION

  invoke globalM reg.shell.getMidasContext()
  new nxm.sys.lib.DataFile(globalM,^file_name) df
  new nxm.sys.lib.DataFileHeader(df) dfh
  invoke kwds dfh.getExtendedHeaderKeywords()
  invoke l:num_entries nxm.sys.lib.TimeLine.getNumTCEntries(kwds)

  res ^{result_name}.NUM_ENTRIES num_entries
  KEY ^file_name GET TCSAMPLE_^num_entries last_offset
  res ^{result_name}.FIRST_OFFSET^tnum tc_first_offset
  res ^{result_name}.LAST_OFFSET last_offset
return

!******************************************************************************!
! make the PLATINUM timeline table entries
!******************************************************************************!
procedure makeplattable s:tnum
  res format PLATINUM_HIGH_PRECISION
  res num_entries plat_timeline.size
  res d:plat_first_offset plat_timeline.0
  calc last_index num_entries 1 -
  res d:plat_last_offset plat_timeline.^last_index

  res ^{result_name}.FORMAT^tnum ^format
  res ^{result_name}.NUM_ENTRIES ^num_entries
  res ^{result_name}.FIRST_OFFSET^tnum ^plat_first_offset
  res ^{result_name}.LAST_OFFSET ^plat_last_offset
return