;+
; PROJECT
;           SolarB EIS
;
; NAME
;           EIS_WRITE_TIMELINE_DATABASE_TABLES
;
; PURPOSE
;           Function to save/write timeline entries to the "as-planned"
;           database.
;           The concept is to check whether a tlc entry object has
;           already been saved. After inserting a new timeline entry,
;           encapsulating it into a timeline_entry object, it is
;           initialized with SEQUENCE_ID = 0.
;           When this entry is saved to the database, a unique
;           SEQUENCE_ID value is allocated.
; CATEGORY
;           EIS planning
;
; WRITTEN
;           John A. Rainnie
;
; MODIFICATION HISTORY
;           V0.0 originally written by Chunkey- September 2005
;           V0.1 Completely re-designed JAR
;-
;************************************************************************
FUNCTION eis_write_timeline_database_tables , tlc

status      = -1

; Replace call to astrolib
defsysv,'!DEBUG', 0
defsysv,'!TEXTUNIT', 0
defsysv,'!PRIV', 2
defsysv,'!TEXTOUT', EXIST = EXIST

;
; Read tlc - get all entry seq_ids
;
tlc_count  = tlc->count()
IF (tlc_count EQ 0) THEN RETURN , -1

seq_id_list = tlc->get_seq_id_list()
save_list   = tlc->get_save_list()

;
; OK, bail out if there aren't any items to save/update
;
IF (TOTAL(save_list) EQ 0) THEN BEGIN
    ;print, 'Not updating databases!'
    print, 'No databases to update!'
    RETURN , status
ENDIF


; Get list of item indicies which are to be saved
save_tl_list = WHERE(save_list EQ 1 , save_count)

FOR i = 0, save_count -1 DO BEGIN

    IF (seq_id_list[save_tl_list[i]] EQ 0) THEN BEGIN

        ; New entries where seq_id = 0
        item_tlc_index = save_tl_list[i]
        ;
        ; Assign a UNIQUE sequence_id here
        ;
        ; How many entries are there? New entry is this value + 1
        status = eis_open_db(/TIME)
        n      = db_info('entries')
        seq_id = n[0] + 1
        dbclose

        IF (seq_id EQ 0) THEN BEGIN

            print,"Cripes - I'm trying to save a TL entry here with a seq_id"
            print,"         set to zero!"
            stop
        ENDIF


        temp   = tlc->get(POSITION = item_tlc_index)
        tl     = temp->get()     ; a eis_timeline_entry structure

        tl.time_component.sequence_id    = seq_id
        tl.science_component.sequence_id = seq_id

        ; Print message
        print,STRJOIN(['Saving new TL entry as seq_id = ',                     $
                                 STRCOMPRESS(seq_id,/REMOVE)],/SINGLE)
        ;stop

        status = eis_write_science_component_database_table(tl.science_component)
        status = eis_write_time_component_database_table(tl.time_component)

       ; Finally, update
       temp->set_seq_id , seq_id = seq_id


    ENDIF ELSE BEGIN

        ; Update existing entries where seq_id NE 0
        ; Sequence_ID is the database entry handle
        temp  = tlc->get( POSITION = save_tl_list[i])
        tl    = temp->get()   ; a eis_timeline_entry structure

        tc_seq_id = tl.time_component.sequence_id

        IF (tc_seq_id EQ 0) THEN BEGIN

            print,"Cripes - I'm trying to save a TL entry here with a seq_id"
            print,"         set to zero!"
            stop
        ENDIF

        sc_seq_id = tl.science_component.sequence_id
        IF (tc_seq_id NE sc_seq_id) THEN BEGIN
            print,"Problem here - tl.tc and tl.sc sequence ids are different!"
            stop
        ENDIF

        ; Print message
        print,STRJOIN(['Updating entry seq_id = ',                          $
                                STRCOMPRESS(tc_seq_id,/REMOVE)],/SINGLE)
        ;stop

        status = eis_update_timeline_entry(tc_seq_id , tl.time_component)
        status = eis_update_timeline_science_entry(tc_seq_id , tl.science_component)

    ENDELSE


ENDFOR

RETURN, status

END
