;+
; PROJECT
;          SOLARB EIS
;
; NAME
;          EIS_CT_SUMMARY_GUI__DEFINE
;
; PURPOSE
;          This object
;
; NOTES
;          The
;
; CATEGORY
;          EIS timeline planning
;
; WRITTEN
;          John A Rainne RAL
;
; VERSION
;          v0.1 JAR 7-Aug-2007
;              Added facility to allow planner to select entries using
;              the summary tool.
;          v0.2 JAR 28-May-2008
;              Changed instructional label. That's all.
;          v0.3 JAR 29-May-2008
;               Made start and stop times longer to include milliseconds
;          v0.4 JAR 14-Jun-2009
;               Save set_list_select and set_list_top positions, and after
;               refreshing set them back. This saves the list selection
;               and position.
;          v0.5 HPW 20-Feb-2026
;               A minor update to the window to avoid automatic resizing.
;
;______________________________________________________________________________
FUNCTION eis_ct_summary_gui::Init , parentClass

Self.parentClass  = parentClass
Self.group_leader = parentClass->tlbID()
;-------------------create the main base---------------------------------------;
pt_version = eis_get_pt_version_number()
widTitle = 'EIS Make Plan Summary Tool' + pt_version
TLB_EVENT                   = {eis_ct_summary_gui_Event}
TLB_EVENT.eis_ct_summary_gui = Self
TLB_EVENT.Type              = "TLB"
Self.tlbID                  = WIDGET_BASE(                                  $
                                    TITLE       = widTitle,                 $
                                    COLUMN      = 1,                        $
                                    /FLOATING      ,                        $
                                    GROUP_LEADER = Self.group_leader ,      $
                                    UValue      = TLB_EVENT,                $
                                    xOffset     = 800)

;-------------------create the GUI------------------------------;
Self->createGUI

RETURN, 1

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_ct_summary_gui::createGUI

; Get eis_fonts
fonts = eis_fonts()

; ______________________________________________________________________
;
; Instruction Base
;
; ______________________________________________________________________
; instructionBase            = WIDGET_BASE(Self.tlbID ,                      $
;                                     ROW         = 1)

value = 'Load an entry on the timeline plot by selecting an item'    +     $
        ' from this list'
aLabel                     = WIDGET_TEXT(Self.tlbID                  ,     $
                                   value       = value               ,     $
                                   /WRAP)
; ______________________________________________________________________
;
; Database List Base
;
; ______________________________________________________________________
dbBase                      = WIDGET_BASE(Self.tlbID ,                      $
                                    COLUMN      = 1,/frame)

value = STRING(format='(A-8,A-10,A-23,A-12,A-1,A-13,A-13,A-12,A-10,A-10)',  $
                       'ST_ID:','SEQ_ID:','Study Acronym:','Date','',          $
                       'Starts:','Ends:','Duration:','xC','yC')

ll_listLabel                = WIDGET_LABEL(dbBase ,                         $
                                    value       = value ,                   $
                                    /ALIGN_LEFT,                            $
                                    font        = fonts.mono)


LIST_EVENT                  = {eis_ct_summary_gui_Event}
;Set it's values
LIST_EVENT.eis_ct_summary_gui    = Self
LIST_EVENT.Type             = "LIST"
Self.listID                 = WIDGET_LIST(dbBase ,                          $
                                    ySize       = 32  ,                     $
                                    xSize       = 120 ,                     $
                                    uValue      = LIST_EVENT,               $
                                    value       = '' ,                      $
                                    font        = fonts.mono)

; ______________________________________________________________________
;
; Button Base
;
; ______________________________________________________________________
buttonBase                  = WIDGET_BASE(Self.tlbID ,                      $
                                    ROW         = 1,grid_layout=0)
; ______________________________________________________________________
;
; Bottom Base
;
; ______________________________________________________________________
REFRESH_EVENT               = {eis_ct_summary_gui_Event}
REFRESH_EVENT.eis_ct_summary_gui = Self
REFRESH_EVENT.Type          = "REFRESH"
REFRESHbutton               = WIDGET_BUTTON(buttonBase ,                    $
                                    value       = ' Refresh ' ,             $
                                    Event_Pro   = 'eis_ct_summary_gui_EV',  $
                                    uValue      = REFRESH_EVENT)

; ______________________________________________________________________
;
; Number of lines Button
;
; ______________________________________________________________________
buttonBase = WIDGET_BASE(Self.tlbID ,                      $
                                    ROW         = 1)



QUIT_EVENT                  = {eis_ct_summary_gui_Event}
QUIT_EVENT.eis_ct_summary_gui = Self
QUIT_EVENT.Type             = "QUIT"
quitbutton                  = WIDGET_BUTTON(buttonBase ,                    $
                                    value       = ' Quit ' ,             $
                                    Event_Pro   = 'eis_ct_summary_gui_EV',  $
                                    uValue      = QUIT_EVENT)


END

;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_ct_summary_gui_Cleanup , wid

WIDGET_CONTROL , wid , get_uvalue = eis_ct_summary_gui_Event
OBJ_DESTROY    , eis_ct_summary_gui_Event.eis_ct_summary_gui

END
;______________________________________________________________________________
; Cleanup
;______________________________________________________________________________
PRO eis_ct_summary_gui::CleanUp

print,'Shutting down Timeline Summary Tool'

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_ct_summary_gui::setProperty, autoScaleStr  = autoScaleStr

IF (N_ELEMENTS(autoScaleStr) GT 0) THEN BEGIN
    stop
ENDIF

End
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_ct_summary_gui::GetProperty, tlbID   = tlbID

tlbID = Self.tlbID

End
;______________________________________________________________________________
; Return widget id
;______________________________________________________________________________
Function eis_ct_summary_gui::tlbID

    Return, Self.tlbID
End

;______________________________________________________________________________
; Widget_Control wrapper
;______________________________________________________________________________
PRO eis_ct_summary_gui::Control, _Ref_Extra=Widget_Control_Keywords

Widget_Control, Self.tlbID, _Extra=Widget_Control_Keywords

End

;______________________________________________________________________________
; Widget_Info wrapper
;______________________________________________________________________________
Function eis_ct_summary_gui::Info, _Ref_Extra=Widget_Info_Keywords

Return, Widget_Info(Self.tlbID, _Extra=Widget_Info_Keywords)

End

;______________________________________________________________________________
; Realize wrapper
;______________________________________________________________________________
PRO eis_ct_summary_gui::Realize

Widget_Control, Self.tlbID, /Realize

End

;______________________________________________________________________________
; XManager wrapper
;______________________________________________________________________________
PRO eis_ct_summary_gui::XManager

XMANAGER , "eis_ct_summary_gui" ,   Self.tlbID ,                                 $
    Event_Handler  = "eis_ct_summary_gui_EV",                                    $
    cleanup        = "eis_ct_summary_gui_Cleanup",                               $
    GROUP_LEADER   = Self.group_leader ,                                   $
    /NO_BLOCK

END

;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_ct_summary_gui_Event__Define

    struct = {  eis_ct_summary_gui_Event,                                         $
                Type                 : "",         $ ;Unique identifier for event
                eis_ct_summary_gui   : OBJ_NEW(),  $ ;Reference to object passed
                extra                : 0           $ ;to - often object associated
             }
END
;______________________________________________________________________________
; Event Handler wrapper
;______________________________________________________________________________
PRO eis_ct_summary_gui_EV , Event

;Get event handler structure containing eis_ct_summary_gui type object
Widget_CONTROL,Event.ID,get_uvalue = eis_ct_summary_gui_Event
;If a WIDGET_TAB event , just quit
IF (TAG_NAMES(event, /STRUCTURE_NAME) EQ 'WIDGET_TAB') THEN BEGIN
    ;print,'tab event'
    RETURN
ENDIF

;Call eis_ct_summary_gui's class member procedure "Handler"
eis_ct_summary_gui_Event.eis_ct_summary_gui->Handler,Event

END
;______________________________________________________________________________
;
; * THIS is the routine for putting in event handling NOT eis_ct_summary_gui_Ev above.
;
; Event Handler - handle events.  Derived classes must
; contain their own implementation in order to handle events or else pass in an
; procedure name/function name using the Event_Pro/Event_Func keywords to
; CWidget::Init()
;______________________________________________________________________________
PRO eis_ct_summary_gui::Handler, Event

WIDGET_CONTROL, Event.ID, Get_UValue = eis_ct_summary_gui_Event
CASE eis_ct_summary_gui_Event.Type OF
    "QUIT"      : Self->Control, /Destroy
    "REFRESH"   : Self->refreshSummary
    "LIST"      : Self->select_entry
    "NLINES"    : Self->refreshSummary
    ELSE        : print,eis_ct_summary_gui_Event.Type
ENDCASE

END
;______________________________________________________________________________
; Set selection in eis_mk_plan
;______________________________________________________________________________
PRO eis_ct_summary_gui::select_entry

; JAR 7-Aug-2007
; Added facility to select a timeline entry from the summary list
;

WIDGET_CONTROL , /HOURGLASS

; Unpack tlc - and get a list of the visible entries
Self.parentClass->getProperty , eis_timeline_container = eis_timeline_container
visible_list  = eis_timeline_container->get_visible_list()
visible_1     = WHERE(visible_list eq 1)

; Get summary widget list index
index         = WIDGET_INFO(Self.listID , /LIST_SELECT)
; So, the required entry is...
selected      = visible_1[index]

; Reset tlc.working_position to this entry
eis_timeline_container->setProperty , working_position = selected
; Call eis_mk_plan method to select this entry
Self.parentClass->select_entry , selected , /SUMMARY

END
;______________________________________________________________________________
; Reset Search Parameters
;______________________________________________________________________________
PRO eis_ct_summary_gui::set_list_select , index

WIDGET_CONTROL , Self.listID , set_list_select = index

END
;______________________________________________________________________________
; Reset Search Parameters
;______________________________________________________________________________
PRO eis_ct_summary_gui::refreshSummary

; Save 1. set_list_top
;      2. set_list_select
; and at the end set them back
set_list_top    = WIDGET_INFO(Self.listID , /LIST_TOP)
set_list_select = WIDGET_INFO(Self.listID , /LIST_SELECT)


Self.parentClass->getProperty , eis_timeline_container = eis_timeline_container
count = eis_timeline_container->count()
; IF there are NO entries, then set w_p = -1, and bail out
IF (count EQ 0) THEN BEGIN
    eis_timeline_container->setProperty , working_position = -1
;    mText = 'There are NO enties in the plan!
;    BEEP
;    d = DIALOG_MESSAGE(mText , /INFORMATION , DIALOG_PARENT = Self.tlbID)

    WIDGET_CONTROL , Self.listID , set_value = ''

    RETURN
ENDIF

; Get display start and stop time
Self.parentClass->getProperty , tl_graphics = tl_graphics
disp_start = tl_graphics.axes.x_axis_day_tai
duration   = tl_graphics.axes.x_axis_length
disp_stop  = disp_start + (duration * 3600.)

; If there are entries
strArray = STRARR(count)
FOR i = 0, count - 1 DO BEGIN

    temp = eis_timeline_container->get(POSITION = i)
    temp->getproperty , visible = visible

    IF (visible EQ 1) THEN BEGIN

        tl   = temp->get()
        tc   = tl.time_component
        ; Convert t1 & t2 from TAI to hours since display start
        ;
        t1   = anytim2utc(tc.start_time , /STIME ,             /TIME)
        d1   = anytim2utc(tc.start_time , /STIME , /TRUNCATE , /DATE)
        t2   = anytim2utc(tc.end_time   , /STIME ,             /TIME)

        ; Time-Tagged ?
        IF (tc.time_tagged EQ 1) THEN tt = '*' ELSE tt = ''
        ; Acronym
        id   = tc.st_id
        ; ID needs to be in 000001 format
        idValue = STRING(format='(I06)',id)
        ; seq_ID needs to be in 000001 format
        seq_idValue = STRING(format='(I06)',tc.sequence_id)

        ; Get pointing coordinates - of first raster only
        temp->getproperty , read_all = read_all
        IF (read_all EQ 1) THEN BEGIN
            xc = tl.science_component.studyRasters[0].x_point
            yc = tl.science_component.studyRasters[0].y_point
        ENDIF ELSE BEGIN
            status = eis_open_db(/SCI_2)
            IF (status[0] EQ -1) THEN stop
            seq_id_string = STRCOMPRESS(tc.sequence_id,/REMOVE)
            c_1 = STRJOIN(['SN_ID = ', seq_id_string,','],/SINGLE)
            c_2 = STRJOIN([c_1,'RA_INDEX = 0'],/SINGLE)
            entry = dbfind(c_2 , /SILENT , count = count)

            xc = dbval(entry[0] , 'X_POINT')
            yc = dbval(entry[0] , 'Y_POINT')
        ENDELSE

        ; Get study duration - in xxDxxHxxMxxS format
        dur = sec2dhms(tc.end_time-tc.start_time)


        format = '(A-8,A-10,A-22,A-12,A-2,A-13,A-13,A-12,F-10.2,F-10.2)'
        strArray[i] = STRING(format=format,                                 $
                     idValue,seq_idValue,tc.acronym,d1,tt,t1,t2,dur,xC,yC)

    ENDIF

ENDFOR

string_lengths = STRLEN(strArray)
indicies       = WHERE(string_lengths NE 0 , count)
IF (count EQ 0) THEN result_arr = '' ELSE result_arr = strArray[indicies]


WIDGET_CONTROL , Self.listID , set_value = result_arr , update = 0

WIDGET_CONTROL , Self.listID , set_list_top    = set_list_top        ,     $
                               set_list_select = set_list_select


pt_version = eis_get_pt_version_number()
widTitle = 'EIS Make Plan Summary Tool ' + pt_version + ':  '    +          $
           STRCOMPRESS(N_ELEMENTS(result_arr),/REMOVE_ALL)       +          $
           ' entries'
WIDGET_CONTROL , Self.tlbID , TLB_Set_Title = widTitle
END
;______________________________________________________________________________
; Set selection in eis_mk_plan
;______________________________________________________________________________
PRO eis_ct_summary_gui::nlines

WIDGET_CONTROL, Self.listID, UPDATE=0 

nLines = WIDGET_INFO(Self.nlinesDroplist , /DROPLIST_SELECT)
WIDGET_CONTROL, Self.listID, ySize=32

WIDGET_CONTROL, Self.listID, UPDATE=1

test = WIDGET_INFO(Self.listID , /LIST_NUM_VISIBLE)
Self->refreshSummary
;print,test

END
;______________________________________________________________________________
; Define Data members
;______________________________________________________________________________
PRO eis_ct_summary_gui__define

    struct          =   { eis_ct_summary_gui                          ,     $
                                    tlbID           : 0L              ,     $
                                    group_leader    : 0L              ,     $
                                    listID          : 0L              ,     $
                                    nlinesDroplist  : 0L              ,     $
                                    parentClass     : OBJ_NEW()             $
                      }

END
