;+
; PROJECT
;          Solar-B EIS
;
; NAME
;          eis_internal_gui__DEFINE
;
; PURPOSE
;          GUI to view internal stuff
;
; INPUTS
;          parentClass - the eis_mk_plan object
;
; CATEGORY
;          EIS Timeline Planning
;
; WRITTEN
;          John A Rainnie RAL 22-May-2008
;
; HISTORY
;          v0.1 JAR 23-Apr-2008
;               Now use tlc method "create_dr_output" to return DR info
;          v0.2 JAR 26-Jun-2008
;               Added scroll bars to all text boxes
;
;-
;______________________________________________________________________________
FUNCTION eis_internal_gui::Init , parentClass

Self.parentClass  = parentClass
Self.group_leader = parentClass->tlbID()

Event_Pro = 'eis_internal_gui_EV'
;-------------------create the main base---------------------------------------;
pt_version = eis_get_pt_version_number()
widTitle = "Stuff" + pt_version
TLB_EVENT                  = {eis_internal_gui_Event}
TLB_EVENT.eis_internal_gui     = Self
TLB_EVENT.Type             = "TLB"
Self.tlbID                 = WIDGET_BASE(                                  $
                                   TITLE       = widTitle            ,     $
                                   COLUMN      = 1                   ,     $
                                   /FLOATING                         ,     $
                                   GROUP_LEADER = Self.group_leader  ,     $
                                   UValue      = TLB_EVENT )

value = STRARR(2)
value = "This is a bunch of diagnostic stuff that I use to keep"     +     $
        " track of some internal parameters - including timeline"    +     $
        " entries that aren't visible on the timeline plot."

TOP_EVENT                  = {eis_internal_gui_Event}
TOP_EVENT.eis_internal_gui = Self
TOP_EVENT.Type             = "LABEL"
TOPID                      = WIDGET_TEXT(Self.tlbID                  ,     $
                                   xSize      = 85 ,           $
                                   ySize      = 3  ,           $
                                   value      = value       ,  $
                                   /WRAP           ,    $
                                   Event_Pro   = 'eis_internal_gui_EV',          $
                                   uValue      = TOP_EVENT)
; ______________________________________________________________________
;
; Widget Tab
;
; ______________________________________________________________________
tabBase                    = WIDGET_TAB(Self.tlbID,/frame)
drBase                     = WIDGET_BASE(tabBase                     ,     $
                                   Title      = ' DR Output '        ,     $
                                   COLUMN     = 1)
tlcBase                    = WIDGET_BASE(tabBase                     ,     $
                                   Title      = ' TLC Summary '      ,     $
                                   COLUMN     = 1)
axesBase                   = WIDGET_BASE(tabBase                     ,     $
                                   Title      = ' Axes Summary '     ,     $
                                   COLUMN     = 1)
value = ' EIS Event Parameters '
eisEventBase               = WIDGET_BASE(tabBase                     ,     $
                                   Title      = value                ,     $
                                   COLUMN     = 1)
blockBase                  = WIDGET_BASE(tabBase                     ,     $
                                   Title      = ' Blocks '           ,     $
                                   COLUMN     = 1)

;
; DR Output
;

DR_EVENT                   = {eis_internal_gui_Event}
DR_EVENT.eis_internal_gui  = Self
DR_EVENT.Type              = "DR"
Self.dr_textID             = WIDGET_TEXT(drBase                      ,     $
                                   xSize       = 85                  ,     $
                                   ySize       = 30                  ,     $
                                   units       = 0                   ,     $
                                   /WRAP                             ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   /SCROLL                           ,     $
                                   uValue      = DR_EVENT)

;
; TLC Info
;

TEXT_EVENT                 = {eis_internal_gui_Event}
TEXT_EVENT.eis_internal_gui    = Self
TEXT_EVENT.Type            = "TEXT"
Self.tlc_textID            = WIDGET_TEXT(tlcBase                     ,     $
                                   xSize       = 85                  ,     $
                                   ySize       = 30                  ,     $
                                   units       = 0                   ,     $
                                   /WRAP                             ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   /SCROLL                           ,     $
                                   uValue      = TEXT_EVENT)

;
; AXES Info
;

AXES_EVENT                 = {eis_internal_gui_Event}
AXES_EVENT.eis_internal_gui    = Self
AXES_EVENT.Type            = "AXES"
Self.axes_textID           = WIDGET_TEXT(axesBase                ,     $
                                   xSize      = 85 ,           $
                                   ySize      = 30 ,           $
                                   units      = 0  ,    $
                                   /WRAP           ,    $
                                   Event_Pro   = 'eis_internal_gui_EV',          $
                                   /SCROLL                           ,     $
                                   uValue      = AXES_EVENT)

;
; EIS Event
;

EISE_EVENT                 = {eis_internal_gui_Event}
EISE_EVENT.eis_internal_gui    = Self
EISE_EVENT.Type            = "EISE"
Self.eise_textID           = WIDGET_TEXT(eisEventBase                ,     $
                                   xSize       = 85                  ,     $
                                   ySize       = 30                  ,     $
                                   units       = 0                   ,     $
                                   /WRAP                             ,     $
                                   Event_Pro   = 'eis_internal_gui_EV',    $
                                   /SCROLL                           ,     $
                                   uValue      = EISE_EVENT)

;
; BLocks
;

BLOCKS_EVENT               = {eis_internal_gui_Event}
BLOCKS_EVENT.eis_internal_gui    = Self
BLOCKS_EVENT.Type          = "BLOCKS"
Self.blocks_textID         = WIDGET_TEXT(blockBase                   ,     $
                                   xSize       = 85                  ,     $
                                   ySize       = 30                  ,     $
                                   units       = 0                   ,     $
                                   /WRAP                             ,     $
                                   Event_Pro   = 'eis_internal_gui_EV',    $
                                   /SCROLL                           ,     $
                                   uValue      = BLOCKS_EVENT)
; ______________________________________________________________________
;
; Bottom base
;
; ______________________________________________________________________
bottomBase                  = WIDGET_BASE(Self.tlbID ,                      $
                                    ROW         = 1  ,                      $
                                    ;/FRAME           ,                      $
                                    /GRID_LAYOUT)

REFRESH_EVENT              = {eis_internal_gui_Event}
REFRESH_EVENT.eis_internal_gui   = Self
REFRESH_EVENT.Type         = "REFRESH"
REFRESHbutton              = WIDGET_BUTTON(bottomBase,                     $
                                    value       = ' Refresh ',                $
                                    Event_Pro   = 'eis_internal_gui_EV',     $
                                    uValue      = REFRESH_EVENT)

EXPORT_EVENT                = {eis_internal_gui_Event}
EXPORT_EVENT.eis_internal_gui     = Self
EXPORT_EVENT.Type           = "EXPORT"
EXPORTbutton                = WIDGET_BUTTON(bottomBase,                     $
                                    value       = ' Export Text... ',       $
                                    Event_Pro   = 'eis_internal_gui_EV',     $
                                    uValue      = EXPORT_EVENT)

QUIT_EVENT                 = {eis_internal_gui_Event}
QUIT_EVENT.eis_internal_gui    = Self
QUIT_EVENT.Type            = "QUIT"
quitbutton                 = WIDGET_BUTTON(bottomBase,                      $
                                    value       = ' Quit ',                  $
                                    Event_Pro   = 'eis_internal_gui_EV',     $
                                    uValue      = QUIT_EVENT)

RETURN, 1

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_internal_gui_Cleanup , wid

WIDGET_CONTROL , wid , get_uvalue = eis_internal_gui_Event
OBJ_DESTROY    , eis_internal_gui_Event.eis_internal_gui

END
;______________________________________________________________________________
; Cleanup
;______________________________________________________________________________
PRO eis_internal_gui::CleanUp


END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_internal_gui::setProperty, start_time = start_time

IF (N_ELEMENTS(start_time) GT 0) THEN BEGIN

    stop

ENDIF

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_internal_gui::GetProperty, tlbID      = tlbID

tlbID      = Self.tlc_textID

END
;______________________________________________________________________________
; Return widget id
;______________________________________________________________________________
FUNCTION eis_internal_gui::tlbID

    RETURN, Self.tlbID
END
;______________________________________________________________________________
; Widget_Control wrapper
;______________________________________________________________________________
PRO eis_internal_gui::Control, _Ref_Extra=Widget_Control_Keywords

Widget_Control, Self.tlbID, _Extra=Widget_Control_Keywords

End
;______________________________________________________________________________
; Widget_Info wrapper
;______________________________________________________________________________
FUNCTION eis_internal_gui::Info, _Ref_Extra=Widget_Info_Keywords

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

End

;______________________________________________________________________________
; Realize wrapper
;______________________________________________________________________________
PRO eis_internal_gui::Realize

Widget_Control, Self.tlbID, /Realize

End

;______________________________________________________________________________
; XManager wrapper
;______________________________________________________________________________
PRO eis_internal_gui::XManager;,group=group

XMANAGER , "eis_internal_gui" ,   Self.tlbID ,                                 $
    Event_Handler  = "eis_internal_gui_EV",                                    $
    cleanup        = "eis_internal_gui_Cleanup",                               $
    GROUP_LEADER   = Self.group_leader ,                                    $
    /NO_BLOCK

end

;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_internal_gui_Event__Define

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

;Get event handler structure containing eis_internal_gui type object
Widget_CONTROL,Event.ID,get_uvalue = eis_internal_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_internal_gui's class member procedure "Handler"
eis_internal_gui_Event.eis_internal_gui->Handler,Event

END
;______________________________________________________________________________
;
; * THIS is the routine for putting in event handling NOT eis_internal_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_internal_gui::Handler, Event

WIDGET_CONTROL, Event.ID, Get_UValue = eis_internal_gui_Event
CASE eis_internal_gui_Event.Type OF
   "QUIT"    : Self->Control, /Destroy
   "REFRESH" : Self->refresh
   "EXPORT"  : Self->export
   ELSE      : print,eis_internal_gui_Event.Type
ENDCASE
END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_internal_gui::update_tlc_info , tlc , wp ,ss

CASE ss OF
    1    : ss_string = 'Yes'
    0    : ss_string = 'No'
    ELSE : stop
ENDCASE
line_div ='=================================================================='

line = 'Summary of Timeline Container (TLC)'
WIDGET_CONTROL , Self.tlc_textID , set_value = line
WIDGET_CONTROL , Self.tlc_textID , set_value = '' , /APPEND
line = STRJOIN(['count          = ',STRCOMPRESS(tlc->count(),/REMOVE)],/SINGLE)
WIDGET_CONTROL , Self.tlc_textID , set_value = line , /APPEND
line = STRJOIN(['wp             = ',STRCOMPRESS(wp,/REMOVE)],/SINGLE)
WIDGET_CONTROL , Self.tlc_textID , set_value = line , /APPEND
line = STRJOIN(['study_selected = ',ss_string              ],/SINGLE)
WIDGET_CONTROL , Self.tlc_textID , set_value = line , /APPEND
WIDGET_CONTROL , Self.tlc_textID , set_value = '' , /APPEND

info = tlc->get_tlc_info()
IF (N_TAGS(info) GT 0) THEN BEGIN

    line = 'i  seq_id  TT  V  Start_time               Stop_time'
    WIDGET_CONTROL , Self.tlc_textID , set_value = line , /APPEND
    WIDGET_CONTROL , Self.tlc_textID , set_value = line_div , /APPEND
    tlc->getProperty , working_position = wp
    FOR i = 0, tlc->count() - 1 DO BEGIN

        start_time = anytim2utc(info.start_time_list[i],/stime)
        stop_time  = anytim2utc(info.end_time_list[i]  ,/stime)

        IF (info.tt_list[i] EQ 1) THEN tt = 'TT' ELSE tt = ''

        IF (i EQ wp) THEN wp_item = '*WP*' ELSE wp_item = ''
        line = string(format='(I-3,I-8,A-4,I-3,A-25,A-25,A-4)'       ,     $
                            i,info.seq_id_list[i],tt,info.visible_list[i],     $
                            start_time,stop_time,wp_item)
        WIDGET_CONTROL , Self.tlc_textID , set_value = line , /APPEND
    ENDFOR

ENDIF

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_internal_gui::update_axes_tab , tl_graphics

axes = tl_graphics.axes
line = 'Summary of Timeline Axes Properties'
WIDGET_CONTROL , Self.axes_textID , set_value = line
WIDGET_CONTROL , Self.axes_textID , set_value = '' , /APPEND
; X_AXIS_DAY_TAI
line = STRING(format='(A-16,A-3,A-30)','X_AXIS_DAY_TAI'              ,     $
                           ':',anytim2utc(axes.x_axis_day_tai,/stime))
WIDGET_CONTROL , Self.axes_textID , set_value = line , /APPEND
; X_AXIS_START
line = STRING(format='(A-16,A-3,F-10.4)','X_AXIS_START'                ,     $
                           ':',axes.x_axis_start)
WIDGET_CONTROL , Self.axes_textID , set_value = line , /APPEND
; X_AXIS_LENGTH
line = STRING(format='(A-16,A-3,F-10.4)','X_AXIS_LENGTH'               ,     $
                           ':',axes.x_axis_length)
WIDGET_CONTROL , Self.axes_textID , set_value = line , /APPEND
; X_AXIS_STOP
line = STRING(format='(A-16,A-3,F-10.4)','X_AXIS_STOP'                 ,     $
                           ':',axes.x_axis_stop)
WIDGET_CONTROL , Self.axes_textID , set_value = line , /APPEND

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_internal_gui::update_eis_event , tlc

line = 'EIS Event/Bright Point Trigger Parameters'
WIDGET_CONTROL , Self.eise_textID , set_value = line
WIDGET_CONTROL , Self.eise_textID , set_value = '' , /APPEND
line = "seq_id   respond"
WIDGET_CONTROL , Self.eise_textID , set_value = line , /APPEND
line_div ='=================================================================='
WIDGET_CONTROL , Self.eise_textID , set_value = line_div , /APPEND
; Get working_position (index of selected study)
n_tlc = tlc->count()
;
FOR i = 0, n_tlc -1 DO BEGIN
    temp   = tlc->get( position = i)
    tl     = temp->get()
    tc     = tl.time_component
    ;sc     = tl.science_component
    seq_id = tc.sequence_id
    flag   = tc.respond_to_eis_event

    line = string(format='(I-9,I-7)',seq_id,flag)
    WIDGET_CONTROL , Self.eise_textID , set_value = line , /APPEND

ENDFOR

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_internal_gui::update_blocks_tab , tlc , cp_tai

line = 'Block Parameters'
WIDGET_CONTROL , Self.blocks_textID , set_value = line
WIDGET_CONTROL , Self.blocks_textID , set_value = '' , /APPEND
line_div ='=================================================================='
WIDGET_CONTROL , Self.blocks_textID , set_value = line_div , /APPEND
Self->block_prev , tlc , cp_tai
WIDGET_CONTROL , Self.blocks_textID , set_value = '' , /APPEND
WIDGET_CONTROL , Self.blocks_textID , set_value = line_div , /APPEND
Self->block_next , tlc , cp_tai
WIDGET_CONTROL , Self.blocks_textID , set_value = line_div , /APPEND

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_internal_gui::block_next , tlc , cp_tai

; Get NEXT block info
next_start_time = tlc->get_next_block_start_time(cp_tai)
IF (next_start_time EQ 0) THEN BEGIN
    line = 'No entries detected within time period'
    WIDGET_CONTROL , Self.blocks_textID , set_value = line , /APPEND
ENDIF ELSE BEGIN
    line = 'Next block starts    : ' + anytim2utc(next_start_time,/stime)
    WIDGET_CONTROL , Self.blocks_textID , set_value = line , /APPEND
ENDELSE

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_internal_gui::block_prev , tlc , cp_tai

prev_block = tlc->get_prev_block_info(cp_tai)
IF (N_TAGS(prev_block[0]) EQ 0) THEN BEGIN
    line = 'No entries detected within time period'
    WIDGET_CONTROL , Self.blocks_textID , set_value = line , /APPEND
ENDIF ELSE BEGIN
    line = 'Previous block starts: ' + anytim2utc(prev_block.prev_start,/stime)
    WIDGET_CONTROL , Self.blocks_textID , set_value = line , /APPEND
    line = 'Previous block ends  : ' + anytim2utc(prev_block.prev_end,/stime)
    WIDGET_CONTROL , Self.blocks_textID , set_value = line , /APPEND
ENDELSE

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_internal_gui::update_dr_tab ,tlc

dr = tlc->create_dr_output()
WIDGET_CONTROL , Self.dr_textID , set_value = dr

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_internal_gui::refresh

Self.parentClass->getProperty , eis_timeline_container = tlc         ,     $
                                tl_graphics            = tl_graphics ,     $
                                cursor_time_gui        = cursor_time_gui
cursor_time_gui->getProperty  , datetime = cp_tai


tlc->getProperty , working_position = wp , study_selected = ss
; DR Output
Self->update_dr_tab , tlc
; TLC Info
Self->update_tlc_info , tlc , wp , ss
; Axes
Self->update_axes_tab , tl_graphics
; Eis Event/ Bright Point Trigger
Self->update_eis_event ,tlc
; Blocks
Self->update_blocks_tab , tlc , cp_tai
END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_internal_gui::export

print,'Coming soon....'

END
;______________________________________________________________________________
; Define Data members
;______________________________________________________________________________
PRO eis_internal_gui__define

    struct   = { eis_internal_gui                                    ,     $
                   tlbID                : 0L                         ,     $
                   group_leader         : 0L                         ,     $
                   parentClass          : OBJ_NEW()                  ,     $
                   dr_textID            : 0L                         ,     $
                   axes_textID          : 0L                         ,     $
                   eise_textID          : 0L                         ,     $
                   blocks_textID        : 0L                         ,     $
                   tlc_textID           : 0L                               $
               }


END
