;+
; NAME
;          EIS_FLARE_GUI
;
; PURPOSE
;          This (object) GUI allows the user/planner to edit the EIS Flare
;          trigger control parameters.
;
; INPUTS
;          parentClass - object reference of parent object gui
;          ll_id       - linelist ID. User must select one window
;
; WRITTEN
;          John Rainnie, RAL, some time ago now
;
; HISTORY
;          v0.1 JAR 2-Apr-2008
;               Included new input keywords "ll_id" and "eisf_flag" to
;               simplify calling from eis_mk_plan.
;          v0.2 JAR 3-Apr-2008
;               Made this GUI modal.
;          v0.3 JAR 7-Apr-2008
;               Check threshold values are in range.
;          v0.4 JAR 9-Jan-2010
;               Big rebuild. Lose "ll_id" and "eisf_flag" keywords.
;          v0.5 JAR 25-Oct-2010
;               Include locate_x/y droplists
;          v0.6 JAR 03-Nov-2010
;               Replace X/Y Min_limit droplists (values 1-7) to sliders
;               to accomodate range 1-255.
;               Also swapped X/Y Flare Position droplist widget values
;               (flag 0/1) to account for erroneous allocation
;          v0.7 JAR 22-Jun-2011
;               Made this gui modal
;          v0.8 JAR 22-Sep-2011
;               Updated gui title
;          v0.9 JAR 02-Dec-2014
;               Completely reworked. Reads a response DB entry (always
;               created when making a study a hunter study). Then use that
;               structure during the lifetime of this gui. When finished,
;               update the response DB with this structure. Lovely stuff!
;-
;______________________________________________________________________________
FUNCTION eis_flare_gui::Init , parentClass , resp_id , ra_index

; Check resp_id is valid. If not, bail out
resp = db_read_response_entry(id=resp_id)
IF (N_TAGS(resp) EQ 0) THEN BEGIN
   BEEP
   mText = "This Response Entry does not exist. Bailing out!"
   complain = DIALOG_MESSAGE( mText ,                        $
                              DIALOG_PARENT = Self.tlbID,                $
                              /ERROR )
   RETURN , 0
ENDIF

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

;-------------------create the main base---------------------------------------;
pt_version = eis_get_pt_version_number()
widTitle = 'EIS Flare Trigger Control Parameters' + pt_version
TLB_EVENT                  = {eis_flare_gui_Event}
TLB_EVENT.eis_flare_gui    = Self
TLB_EVENT.Type             = "TLB"
Self.tlbID                 = WIDGET_BASE(                                   $
                                    TITLE       = widTitle,                 $
                                    COLUMN      = 1,                        $
                                    /FLOATING      ,                        $
                                    ;/MODAL         ,                        $
                                    GROUP_LEADER = Self.group_leader ,      $
                                    UValue      = TLB_EVENT,                $
                                    xOffset     = 800)

;-------------------create the GUI------------------------------;
Self->create_hunter_study_gui
Self->create_response_study_main_gui
Self->create_ra1_widgets
Self->create_ra2_widgets

;______________________________________________________________________________
; Bottom Base
;______________________________________________________________________________
bottomBaseID               = WIDGET_BASE(Self.tlbID                    ,   $
                                   ROW         = 1                     ,   $
                                   FRAME       = 1                     ,   $
                                   /GRID_LAYOUT)
;---------------------------------------------------------------------------
;
; ACCEPT button
;
;---------------------------------------------------------------------------
ACCEPT_EVENT                = {eis_flare_gui_Event}
ACCEPT_EVENT.eis_flare_gui = Self
ACCEPT_EVENT.Type           = "ACCEPT"
value = ' Done '
ACCEPTbutton                = WIDGET_BUTTON(bottomBaseID             ,     $
                                   value       = value               ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = ACCEPT_EVENT)

ACCEPT_EVENT               = {eis_flare_gui_Event}
ACCEPT_EVENT.eis_flare_gui = Self
ACCEPT_EVENT.Type          = "STRUCT"
value = ' Check structure '
ACCEPTbutton               = WIDGET_BUTTON(bottomBaseID              ,     $
                                   value       = value               ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = ACCEPT_EVENT)

;Self->update_response_study
;Self->get_response_db_values

RETURN, 1

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_flare_gui::create_hunter_study_gui

HunterStudyBaseID          = WIDGET_BASE(Self.tlbID                  ,     $
                                   COLUMN      = 1                   ,     $
                                   FRAME       = 4)

;______________________________________________________________________________
; Hunter: Line List Selection
;______________________________________________________________________________
LLBaseID                   = WIDGET_BASE(HunterStudyBaseID           ,     $
                                   ROW         = 1)

value = 'Select Hunter Study Line: '
aLabel                     = WIDGET_LABEL(LLBaseID                   ,     $
                                   value       = value               ,     $
                                   xSize       = 160                 ,     $
                                   /ALIGN_LEFT)

LL_EVENT                   = {eis_flare_gui_Event}
LL_EVENT.eis_flare_gui     = Self
LL_EVENT.Type              = "LL_SELECT"
ll_list = '                                                          '
Self.llDroplistID          = WIDGET_DROPLIST(LLBaseID                ,     $
                                   value       = ll_list      ,              $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = LL_EVENT ,      $
                                   xSize       = 150                 ,     $
                                   sensitive   = 0)
WIDGET_CONTROL , Self.llDroplistID , set_droplist_select = 0
;______________________________________________________________________________
; Hunter Base: Create X & Y Threshold widgets
;______________________________________________________________________________
thresholdBase              = WIDGET_BASE(HunterStudyBaseID           ,     $
                                   ROW         = 1)

aLabel                     = WIDGET_LABEL(thresholdBase              ,     $
                                   value       = 'X/Y Threshold:'    ,     $
                                   /ALIGN_LEFT                       ,     $
                                   xSize       = 160)

THR_X_EVENT                = {eis_flare_gui_Event}
THR_X_EVENT.eis_flare_gui  = Self
THR_X_EVENT.Type           = "X_THRESHOLD"
Self.x_thresholdID         = WIDGET_TEXT(thresholdBase               ,     $
                                   value       = '0'                 ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   xSize       = 11                  ,     $
                                   /EDITABLE                         ,     $
                                   /ALL_EVENTS                       ,     $
                                   uValue      = THR_X_EVENT)

THR_Y_EVENT                = {eis_flare_gui_Event}
THR_Y_EVENT.eis_flare_gui  = Self
THR_Y_EVENT.Type           = "Y_THRESHOLD"
Self.y_thresholdID         = WIDGET_TEXT(thresholdBase               ,     $
                                   value       = '0'                 ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   xSize       = 11                  ,     $
                                   /EDITABLE                         ,     $
                                   /ALL_EVENTS                       ,     $
                                   uValue      = THR_Y_EVENT)

value = ' 32-bit number (1-4294967295)'
aLabel                     = WIDGET_LABEL(thresholdBase              ,     $
                                   value       = value               ,     $
                                   /ALIGN_LEFT)


;______________________________________________________________________________
; Hunter Base: Create X & Y Min_limit widgets
;______________________________________________________________________________
minLimitBase               = WIDGET_BASE(HunterStudyBaseID           ,     $
                                   ROW         = 1)

aLabel                     = WIDGET_LABEL(minLimitBase               ,     $
                                   value       = 'X/Y Min Limit:'    ,     $
                                   /ALIGN_LEFT                       ,     $
                                   xSize       = 160)

MINLIM_X_EVENT             = {eis_flare_gui_Event}
MINLIM_X_EVENT.eis_flare_gui = Self
MINLIM_X_EVENT.Type       = "X_MIN_LIMT"
;value = [' 1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ']
;Self.x_min_limitID        = WIDGET_DROPLIST(minLimitBase             ,     $
;                                   value       = value               ,     $
;                                   Event_Pro   = Event_Pro           ,     $
;                                   uValue      = MINLIM_X_EVENT)
Self.x_min_limitID        = WIDGET_SLIDER(minLimitBase               ,     $
                                   value       = 1                   ,     $
                                   minimum     = 1                   ,     $
                                   maximum     = 255                 ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = MINLIM_X_EVENT)


MINLIM_Y_EVENT             = {eis_flare_gui_Event}
MINLIM_Y_EVENT.eis_flare_gui = Self
MINLIM_Y_EVENT.Type       = "Y_MIN_LIMT"
;Self.y_min_limitID        = WIDGET_DROPLIST(minLimitBase             ,     $
;                                   value       = value               ,     $
;                                   Event_Pro   = Event_Pro           ,     $
;                                   uValue      = MINLIM_Y_EVENT)
Self.y_min_limitID        = WIDGET_SLIDER(minLimitBase               ,     $
                                   value       = 1                   ,     $
                                   minimum     = 1                   ,     $
                                   maximum     = 255                 ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = MINLIM_Y_EVENT)

;______________________________________________________________________________
; Hunter Base: Create X & Y Position widgets
;______________________________________________________________________________
posBase                    = WIDGET_BASE(HunterStudyBaseID           ,     $
                                   ROW         = 1)

value = 'X/Y Flare Position:'
aLabel                     = WIDGET_LABEL(posBase               ,     $
                                   value       = value    ,     $
                                   /ALIGN_LEFT                       ,     $
                                   xSize       = 160)

LOCATE_X_EVENT             = {eis_flare_gui_Event}
LOCATE_X_EVENT.eis_flare_gui = Self
LOCATE_X_EVENT.Type        = "LOCATE_X"
; JAR 3-Nov-2010. Swapped labels round!
;value = [' Peak ',' Centre ']
value = [' Centre ' , ' Peak ']
Self.locate_xID            = WIDGET_DROPLIST(posBase             ,     $
                                   value       = value               ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = LOCATE_X_EVENT)

LOCATE_Y_EVENT             = {eis_flare_gui_Event}
LOCATE_Y_EVENT.eis_flare_gui = Self
LOCATE_Y_EVENT.Type        = "LOCATE_Y"
Self.locate_yID            = WIDGET_DROPLIST(posBase             ,     $
                                   value       = value               ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = LOCATE_Y_EVENT)

;______________________________________________________________________________
; Hunter Base: Create NoData widgets
;______________________________________________________________________________
noDataBase                 = WIDGET_BASE(HunterStudyBaseID           ,     $
                                   ROW         = 1)

noDataExcBaseID            = WIDGET_BASE(noDataBase                  ,     $
                                   ROW         = 1                   ,     $
                                   /NONEXCLUSIVE)

NODATA_EVENT               = {eis_flare_gui_Event}
NODATA_EVENT.eis_flare_gui = Self
NODATA_EVENT.Type          = "NODATA"
value = 'Blah'
Self.noDataID              = WIDGET_BUTTON(noDataExcBaseID           ,     $
                                   value       = value               ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = NODATA_EVENT        ,     $
                                   SENSITIVE   = 0)

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_flare_gui::create_response_study_main_gui

Self.R_STBaseID            = WIDGET_BASE(Self.tlbID                  ,     $
                                   COLUMN      = 1                   ,     $
                                   FRAME       = 4)

R_ST_SELECTBase            = WIDGET_BASE(Self.R_STBaseID             ,     $
                                   ROW         = 1)

R_ST_EVENT                 = {eis_flare_gui_Event}
R_ST_EVENT.eis_flare_gui   = Self
R_ST_EVENT.Type            = "RESPONSE_STUDY"
value = ' Select Response study... '
response_st_button         = WIDGET_BUTTON(R_ST_SELECTBase                ,     $
                                   value       = value               ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = R_ST_EVENT          ,     $
                                   ;xSize       = 150                 ,     $
                                   ySize       = 30)

aLabel                     = WIDGET_LABEL(R_ST_SELECTBase                 ,      $
                                    value       = '  ID: '          ,      $
                                    xSize       = 50 ,                      $
                                    /ALIGN_LEFT)

Self.rst_idID              = WIDGET_LABEL(R_ST_SELECTBase ,                $
                                    value       = idValue ,                $
                                    xSize       = 50 ,                      $
                                    /ALIGN_LEFT)

aLabel                     = WIDGET_LABEL(R_ST_SELECTBase ,                $
                                    value       = ' Acronym:  ' ,      $
                                    /ALIGN_LEFT)

Self.rst_acronymID         = WIDGET_LABEL(R_ST_SELECTBase ,                $
                                    value       = '-' ,                   $
                                    xSize       = 220 ,                     $
                                    /ALIGN_LEFT)

ra2_ExcBaseID              = WIDGET_BASE(R_ST_SELECTBase             ,     $
                                   ROW         = 1                   ,     $
                                   /NONEXCLUSIVE)

RA2_EVENT                  = {eis_flare_gui_Event}
RA2_EVENT.eis_flare_gui    = Self
RA2_EVENT.Type             = "TOGGLE_RA2"
value = 'RA2'
Self.ra2_useButtonID       = WIDGET_BUTTON(ra2_ExcBaseID             ,     $
                                   value       = value               ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = RA2_EVENT           ,     $
                                   SENSITIVE   = 0)



END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_flare_gui::create_ra1_widgets

;______________________________________________________________________________
; Repoint Raster 1: Selection
;______________________________________________________________________________
RA1BaseID                  = WIDGET_BASE(Self.R_STBaseID             ,     $
                                   COLUMN      = 1                   ,     $
                                   FRAME       = 1)
Self.ra1_BaseID = RA1BaseID

RA1SelectBaseID            = WIDGET_BASE(RA1BaseID                   ,     $
                                   ROW         = 1)

value = ' Repoint Raster 1: '
aLabel                     = WIDGET_LABEL(RA1SelectBaseID            ,     $
                                   value       = value               ,     $
                                   /DYNAMIC_RESIZE                   ,     $
                                   /ALIGN_LEFT)

RA_EVENT                   = {eis_flare_gui_Event}
RA_EVENT.eis_flare_gui     = Self
RA_EVENT.Type              = "RA_SELECT"
ra_list = '                                                          '
Self.ra_1_DroplistID       = WIDGET_DROPLIST(RA1SelectBaseID         ,     $
                                   value       = ra_list             ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = RA_EVENT            ,     $
                                   xSize       = 600                 ,     $
                                   sensitive   = 0)
WIDGET_CONTROL , Self.ra_1_DroplistID , set_droplist_select = 0


;______________________________________________________________________________
; Repoint Raster 1: Pointing
;______________________________________________________________________________
RA1PointingBaseID          = WIDGET_BASE(RA1BaseID                   ,     $
                                   ROW         = 1)

RA1PointingBaseExcID       = WIDGET_BASE(RA1PointingBaseID           ,     $
                                   ROW         = 1                   ,     $
                                   /NONEXCLUSIVE)

REPOINT_X_EVENT            = {eis_flare_gui_Event}
REPOINT_X_EVENT.eis_flare_gui = Self
REPOINT_X_EVENT.Type       = "X_REPOINT"
value = 'Repoint X'
Self.x_repointID            = WIDGET_BUTTON(RA1PointingBaseExcID     ,     $
                                   value       = value               ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = REPOINT_X_EVENT)

REPOINT_Y_EVENT            = {eis_flare_gui_Event}
REPOINT_Y_EVENT.eis_flare_gui = Self
REPOINT_Y_EVENT.Type       = "Y_REPOINT"
value = 'Repoint Y'
Self.y_repointID           = WIDGET_BUTTON(RA1PointingBaseExcID      ,     $
                                   value       = value               ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   uValue      = REPOINT_Y_EVENT)


; Manual Repointing
RA1_X_EVENT                = {eis_flare_gui_Event}
RA1_X_EVENT.eis_flare_gui  = Self
RA1_X_EVENT.Type           = "RA1_X"
Self.ra1_xID               = WIDGET_TEXT(RA1PointingBaseID           ,     $
                                   value       = '0'                 ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   xSize       = 11                  ,     $
                                   /EDITABLE                         ,     $
                                   /ALL_EVENTS                       ,     $
                                   uValue      = RA1_X_EVENT         ,     $
                                   SENSITIVE   = 0)

RA1_Y_EVENT                = {eis_flare_gui_Event}
RA1_Y_EVENT.eis_flare_gui  = Self
RA1_Y_EVENT.Type           = "RA1_Y"
Self.ra1_yID               = WIDGET_TEXT(RA1PointingBaseID           ,     $
                                   value       = '0'                 ,     $
                                   Event_Pro   = Event_Pro           ,     $
                                   xSize       = 11                  ,     $
                                   /EDITABLE                         ,     $
                                   /ALL_EVENTS                       ,     $
                                   uValue      = RA1_Y_EVENT         ,     $
                                   SENSITIVE   = 0)

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_flare_gui::populate_widgets

; Update widgets with real values from entry from response DB

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Hunter Study Properties
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Line list droplist
tl     = eis_get_tl_entry(Self.resp.trig_st_seqid)
tc     = tl.time_component
sc     = tl.science_component
ra_id  = sc.studyRasters[Self.ra_index].ra_id
ll_id  = sc.studyRasters[Self.ra_index].ll_id
; Update the hunter study line list
Self->update_hunter_st_linelist , ll_id , ra_id
;
; X/Y Threshold
; Make sure threshold values are greater than zero
IF (Self.resp.x_threshold LT 1) THEN Self.resp.x_threshold = 1
IF (Self.resp.y_threshold LT 1) THEN Self.resp.y_threshold = 1
x_threshold_value = STRCOMPRESS(Self.resp.x_threshold,/REMOVE_ALL)
y_threshold_value = STRCOMPRESS(Self.resp.y_threshold,/REMOVE_ALL)
WIDGET_CONTROL , Self.x_thresholdID , set_value = x_threshold_value
WIDGET_CONTROL , Self.y_thresholdID , set_value = y_threshold_value
;
; X/Y_MIN_LIMIT - Set slider values
IF (Self.resp.x_min_limit LT 1) THEN Self.resp.x_min_limit = 1
IF (Self.resp.y_min_limit LT 1) THEN Self.resp.y_min_limit = 1
WIDGET_CONTROL , Self.x_min_limitID , set_value = Self.resp.x_min_limit
WIDGET_CONTROL , Self.y_min_limitID , set_value = Self.resp.y_min_limit
;
; Locate_X/Y: flare peak (0) or centre (1)
locate = [Self.resp.locate_x,Self.resp.locate_y]
WIDGET_CONTROL , Self.locate_xID , set_droplist_select = locate[0]
WIDGET_CONTROL , Self.locate_yID , set_droplist_select = locate[1]
;
; no_hunt_data
WIDGET_CONTROL , Self.noDataID , set_button = Self.resp.no_hunt_data
;
;
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Response study Parameters
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Update response study parameters
Self->update_response_study

; X/Y Repoint - both off (0) by default
WIDGET_CONTROL , Self.x_repointID , set_button = Self.resp.adjust_x
WIDGET_CONTROL , Self.y_repointID , set_button = Self.resp.adjust_y
;
; Sensitive?
WIDGET_CONTROL , Self.ra1_xID , sensitive = 1 - Self.resp.adjust_x
WIDGET_CONTROL , Self.ra1_yID , sensitive = 1 - Self.resp.adjust_y
;
; Set default pointing (inherited from hunter study)
xcen = STRING(format='(D-0.2)',Self.resp.ra1_xcen)
ycen = STRING(format='(D-0.2)',Self.resp.ra1_ycen)
WIDGET_CONTROL , Self.ra1_xID , set_value = STRTRIM(xcen,2)
WIDGET_CONTROL , Self.ra1_yID , set_value = STRTRIM(ycen,2)

;Self.parentClass->getProperty, eis_timeline_container = tlc
;tlc->getProperty, working_position = working_position
;temp   = tlc->get( position = working_position)
;tl     = temp->get()
;tc     = tl.time_component
;sc     = tl.science_component
;seq_id = tc.sequence_id

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_flare_gui::create_ra2_widgets

;______________________________________________________________________________
; Repoint Raster 2: Selection
;______________________________________________________________________________
Self.ra2_BaseID            = WIDGET_BASE(Self.R_STBaseID             ,     $
                                   COLUMN      = 1                   ,     $
                                   FRAME       = 1)

RA2SelectBaseID            = WIDGET_BASE(Self.ra2_BaseID             ,     $
                                   ROW         = 1)

value = ' Repoint Raster 2: '
aLabel                     = WIDGET_LABEL(RA2SelectBaseID            ,     $
                                   value       = value               ,     $
                                   /DYNAMIC_RESIZE                   ,     $
                                   /ALIGN_LEFT)

RA_EVENT                   = {eis_flare_gui_Event}
RA_EVENT.eis_flare_gui     = Self
RA_EVENT.Type              = "RA_SELECT"
ra_list = ''
;Self.ra_2_DroplistID       = WIDGET_DROPLIST(RA2SelectBaseID         ,     $
;                                   value       = ra_list             ,     $
;                                   Event_Pro   = Event_Pro           ,     $
;                                   uValue      = RA_EVENT            ,     $
;                                   xSize       = 600                 ,     $
;                                   sensitive   = 0)
;WIDGET_CONTROL , Self.ra_2_DroplistID , set_droplist_select = 0
Self.ra_2_DroplistID       = WIDGET_LABEL(RA2SelectBaseID            ,     $
                                   value       = ''                  ,     $
                                   /DYNAMIC_RESIZE)



END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_flare_gui_Cleanup , wid

WIDGET_CONTROL , wid , get_uvalue = eis_flare_gui_Event
OBJ_DESTROY    , eis_flare_gui_Event.eis_flare_gui

END
;______________________________________________________________________________
; Cleanup
;______________________________________________________________________________
PRO eis_flare_gui::CleanUp


END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_flare_gui::setProperty , blah = blah

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

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_flare_gui::GetProperty, blah = blah

;

END
;______________________________________________________________________________
; Return widget id
;______________________________________________________________________________
Function eis_flare_gui::tlbID

    Return, Self.tlbID
END
;______________________________________________________________________________
; Widget_Control wrapper
;______________________________________________________________________________
PRO eis_flare_gui::Control, _Ref_Extra=Widget_Control_Keywords

Widget_Control, Self.tlbID, _Extra=Widget_Control_Keywords

END
;______________________________________________________________________________
; Widget_Info wrapper
;______________________________________________________________________________
Function eis_flare_gui::Info, _Ref_Extra=Widget_Info_Keywords

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

END
;______________________________________________________________________________
; Realize wrapper
;______________________________________________________________________________
PRO eis_flare_gui::Realize

Widget_Control, Self.tlbID, /Realize

END
;______________________________________________________________________________
; XManager wrapper
;______________________________________________________________________________
PRO eis_flare_gui::XManager

XMANAGER , "eis_flare_gui" ,   Self.tlbID ,                                 $
    Event_Handler  = "eis_flare_gui_EV",                                    $
    cleanup        = "eis_flare_gui_Cleanup",                               $
    GROUP_LEADER   = Self.group_leader ,                                   $
    /NO_BLOCK

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_flare_gui_Event__Define

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

;Get event handler structure containing eis_flare_gui type object
Widget_CONTROL,Event.ID,get_uvalue = eis_flare_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_flare_gui's class member procedure "Handler"
eis_flare_gui_Event.eis_flare_gui->Handler,Event

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

WIDGET_CONTROL, Event.ID, Get_UValue = eis_flare_gui_Event
CASE eis_flare_gui_Event.Type OF
    "ACCEPT"          : Self->accept
    "RESPONSE_STUDY"  : eis_xstudy , parentClass = Self , /MODAL
    "RA_SELECT"       : Self->select_raster
    "LL_SELECT"       : Self->select_line , Event
    "X_THRESHOLD"     : Self->update_x_threshold
    "Y_THRESHOLD"     : Self->update_y_threshold
    "X_REPOINT"       : Self->update_adjust_x , event
    "Y_REPOINT"       : Self->update_adjust_y , event
    "LOCATE_X"        : Self.resp.locate_x    = event.index
    "LOCATE_Y"        : Self.resp.locate_y    = event.index
    "X_MIN_LIMT"      : Self.resp.x_min_limit = event.value
    "Y_MIN_LIMT"      : Self.resp.y_min_limit = event.value
    "RA1_X"           : Self->update_ra1_x
    "RA1_Y"           : Self->update_ra1_y
    "STRUCT"          : Self->help_struct
    "NODATA"          : Self.resp.no_hunt_data = event.select
    "TOGGLE_RA2"      : Self->use_ra2
    ELSE              : print,eis_flare_gui_Event.Type
ENDCASE

END
;______________________________________________________________________________
; OK, get raster X position
;______________________________________________________________________________
PRO eis_flare_gui::help_struct

print,'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
help,Self.resp,/str
print,'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'

END
;______________________________________________________________________________
; OK, get raster X position
;______________________________________________________________________________
PRO eis_flare_gui::update_ra1_x

; Retrieve new value from widget
WIDGET_CONTROL , Self.ra1_xID , get_value = newValue
; Check this is a valid number. If so save it, if not use the previous
; value
IF (is_number(newValue[0]) NE 1) THEN BEGIN
   old_value = STRING(format='(D-0.2)',Self.resp.ra1_xcen)
   old_value = STRTRIM(old_value,2)
   WIDGET_CONTROL , Self.ra1_xID , set_value = old_value
ENDIF ELSE BEGIN
   Self.resp.ra1_xcen = DOUBLE(newValue[0])
ENDELSE

END
;______________________________________________________________________________
; OK, get raster Y position
;______________________________________________________________________________
PRO eis_flare_gui::update_ra1_y

; Retrieve new value from widget
WIDGET_CONTROL , Self.ra1_yID , get_value = newValue
; Check this is a valid number. If so save it, if not use the previous
; value
IF (is_number(newValue[0]) NE 1) THEN BEGIN
   old_value = STRING(format='(D-0.2)',Self.resp.ra1_ycen)
   old_value = STRTRIM(old_value,2)
   WIDGET_CONTROL , Self.ra1_yID , set_value = old_value
ENDIF ELSE BEGIN
   Self.resp.ra1_ycen = DOUBLE(newValue[0])
ENDELSE

END
;______________________________________________________________________________
; Update adjust_x
;______________________________________________________________________________
PRO eis_flare_gui::update_adjust_x , event

Self.resp.adjust_x = event.select < 1
WIDGET_CONTROL , Self.ra1_xID , sensitive = 1 - Self.resp.adjust_x

END
;______________________________________________________________________________
; Update adjust_y
;______________________________________________________________________________
PRO eis_flare_gui::update_adjust_y , event

Self.resp.adjust_y = event.select < 1
WIDGET_CONTROL , Self.ra1_yID , sensitive = 1 - Self.resp.adjust_y

END
;______________________________________________________________________________
; Select Repoint Raster
;______________________________________________________________________________
PRO eis_flare_gui::select_line , event

Self.resp.line_index = event.index

END
;______________________________________________________________________________
; OK, get raster X position
;______________________________________________________________________________
PRO eis_flare_gui::use_ra2

stop
Self->select_raster

END
;______________________________________________________________________________
; Select Repoint Raster
;______________________________________________________________________________
PRO eis_flare_gui::select_raster

; What is index of the selected raster?
event_index = WIDGET_INFO(Self.ra_1_DroplistID , /DROPLIST_SELECT)

; Read study and get rasters
st_id = Self.resp.response_st_id
st    = db_read_study_entry(st_id)
IF (st.nRasters LE 1) THEN RETURN
; Get ra_id
WIDGET_CONTROL , Self.ra_1_DroplistID , get_value = list
item  = list[event_index]
ra_id = FIX(STRMID(item , 0 , 6))
; Update raster selection
Self.resp.raster_identifier = ra_id

; Update RA2 widgets.
; Set RA2 as next raster from RA1. Eg: if RA1 = 0, then RA2 = 1
; However, if there is only 1 raster in Response Study, then
;   de-sensitize the RA2 widgets
;
ra2_index = (event_index+1) < (st.nRasters-1)
; OK, populate raster 1 droplist
WIDGET_CONTROL , Self.ra_2_DroplistID , set_value = list[ra2_index]
sensitive = 1
; Make RA2 widgets insensitive if only 1 raster
IF (st.nRasters LE 1) THEN sensitive = 0
; Is this the last raster? If so, then de-sensitize RA2
IF (event_index GE (st.nRasters-1)) THEN sensitive = 0
; If RA2 button is toggled off, then make insensitive
ra2_use = WIDGET_INFO(Self.ra2_useButtonID , /BUTTON_SET)
IF (ra2_use NE 1) THEN BEGIN
   sensitive = 0
   Self.resp.ra2_use = 0
ENDIF

; Make widget sensitive or otherwise
WIDGET_CONTROL , Self.ra2_BaseID , sensitive = sensitive

END
;______________________________________________________________________________
; OK, load the study
;______________________________________________________________________________
PRO eis_flare_gui::loadStudy , st_id , filler = filler

; Update structure with new response study ID
Self.resp.response_st_id = st_id
; By default, reset raster ID and Line List index to zero
Self.resp.raster_identifier   = 0
; Now update widgets
WIDGET_CONTROL , Self.ra1_BaseID , sensitive = 1
Self->update_response_study

END
;______________________________________________________________________________
; Kill eis_xStudy
;______________________________________________________________________________
PRO eis_flare_gui::kill_xstudy , xstudy_obj

xstudy_obj->Control, /Destroy

END
;______________________________________________________________________________
; Load study
;______________________________________________________________________________
PRO eis_flare_gui::reset_widgets

; Update response study values
WIDGET_CONTROL , Self.rst_idID      , set_value = '-'
WIDGET_CONTROL , Self.rst_acronymID , set_value = '-'
WIDGET_CONTROL , Self.ra_1_DroplistID  , sensitive = 0 , set_value = 'None'
WIDGET_CONTROL , Self.ra1_BaseID , sensitive = 0
WIDGET_CONTROL , Self.ra2_BaseID , sensitive = 0
WIDGET_CONTROL , Self.ra2_useButtonID , sensitive = 0

END
;______________________________________________________________________________
; Load study
;______________________________________________________________________________
PRO eis_flare_gui::update_response_study

; What is response study ID?
st_id = Self.resp.response_st_id
; Check response study exists and is valid
IF (st_id EQ 0) THEN BEGIN
   Self->reset_widgets
   RETURN
ENDIF
WIDGET_CONTROL , Self.ra2_useButtonID , sensitive = Self.resp.ra2_use

st      = db_read_study_entry(st_id)
id      = STRING(format='(I06)' , st_id)
acronym = STRTRIM(st.acronym , 2)

; Update response study values
WIDGET_CONTROL , Self.rst_idID      , set_value = id
WIDGET_CONTROL , Self.rst_acronymID , set_value = STRTRIM(acronym,2)

ra_id = Self.resp.raster_identifier
IF (ra_id EQ 0) THEN BEGIN
   ra_id    = st.studyRasters[0].ra_id
   ra_index = 0
ENDIF ELSE BEGIN
   ; Get list of raster IDs - and hence its index
   ra_id_list = st.studyRasters[0:st.nRasters-1].ra_id
   ra_index   = WHERE(ra_id_list EQ ra_id , ra_count)
ENDELSE

; Update repoint raster ID
Self.resp.raster_identifier = ra_id

; OK, populate raster droplist
ra_list = Self->get_ra_droplist(st)

; OK, populate raster 1 droplist
WIDGET_CONTROL , Self.ra_1_DroplistID , set_value = ra_list
WIDGET_CONTROL , Self.ra_1_DroplistID , set_droplist_select = ra_index[0]
WIDGET_CONTROL , Self.ra_1_DroplistID , sensitive = 1

; Update RA2 widgets.
; Set RA2 as next raster from RA1. Eg: if RA1 = 0, then RA2 = 1
; However, if there is only 1 raster in Response Study, then
;   de-sensitize the RA2 widgets
;
ra2_index = (ra_index[0]+1) < (st.nRasters-1)
; OK, populate raster 1 droplist
WIDGET_CONTROL , Self.ra_2_DroplistID , set_value = ra_list[ra2_index]

; Make RA2 widgets insensitive if only 1 raster
IF (st.nRasters LE 1) THEN sensitive = 0 ELSE sensitive = 1
WIDGET_CONTROL , Self.ra2_useButtonID , sensitive = sensitive
; If RA2 button is toggled off, then make insensitive
ra2_use = WIDGET_INFO(Self.ra2_useButtonID , /BUTTON_SET)
IF (ra2_use NE 1) THEN BEGIN
   sensitive = 0
   Self.resp.ra2_use = 0
   ;
ENDIF
WIDGET_CONTROL , Self.ra2_BaseID , sensitive = sensitive


END
;______________________________________________________________________________
; OK, get the line list
;______________________________________________________________________________
PRO eis_flare_gui::update_hunter_st_linelist , ll_id , ra_id

IF (ll_id LT 1) THEN BEGIN
   WIDGET_CONTROL , Self.llDroplistID , set_value = '' , sensitive = 0
   RETURN
ENDIF
ll_list = Self->get_ll_droplist(ll_id , ra_id)
WIDGET_CONTROL , Self.llDroplistID , set_value           = ll_list   , $
                                     sensitive           = 1         , $
                                     set_droplist_select = Self.resp.line_index

END
;______________________________________________________________________________
; OK, get the line list
;______________________________________________________________________________
FUNCTION eis_flare_gui::get_ll_droplist , ll_id , ra_id

; Ca XVII 192.82
; O VI    184.12
ll      = db_read_linelist_entry(ll_id)
ll_list = STRARR(ll.nLines)

; Read raster
ra         = db_read_raster_entry(ra_id)

FOR i = 0 , ll.nLines - 1 DO BEGIN

   dims = STRING(format='(A-5,I-3,A-1,I-3,A-1)' ,  $
                     '(HxW: ',ra.windHeight,'x',ra.dataWindows[i],')')
   ll_list[i] = STRING(format='(A-20,F-6.2,A-4,A-16)' ,     $
                 ll.lines[i].name,ll.lines[i].wavelength/100.,''     ,     $
                 STRCOMPRESS(dims,/REMOVE_ALL))

ENDFOR

RETURN , ll_list

END
;______________________________________________________________________________
; OK, get the raster list
;______________________________________________________________________________
FUNCTION eis_flare_gui::get_ra_droplist , eis_study

; 000001  Test_raster_3 [RA:000002][SNS:]
; O VI   184.12

ra_list = STRARR(eis_study.nRasters)
FOR i = 0 , eis_study.nRasters - 1 DO BEGIN

   sra        = eis_study.studyRasters[i]
   idValue    = STRING(format='(I06)',sra.ra_id)
   ra_list[i] = STRING(format='(A-8,A-20,A-60)'                         ,     $
                 idValue,sra.ra_acronym,sra.ra_title)

ENDFOR

RETURN , ra_list

END
;______________________________________________________________________________
; Set X_threshold
;______________________________________________________________________________
PRO eis_flare_gui::update_x_threshold

WIDGET_CONTROL , Self.x_thresholdID , get_value = x_threshold

IF (is_number(x_threshold[0]) NE 1) THEN BEGIN
   old_value = STRCOMPRESS(Self.resp.x_threshold,/REMOVE_ALL)
   print,'resetting'
   WIDGET_CONTROL , Self.x_thresholdID , set_value = old_value
ENDIF ELSE BEGIN
   Self.resp.x_threshold = ULONG(x_threshold)
ENDELSE

END
;______________________________________________________________________________
; Set X_threshold
;______________________________________________________________________________
PRO eis_flare_gui::update_y_threshold

WIDGET_CONTROL , Self.y_thresholdID , get_value = y_threshold

IF (is_number(y_threshold[0]) NE 1) THEN BEGIN
   old_value = STRCOMPRESS(Self.resp.y_threshold,/REMOVE_ALL)
   WIDGET_CONTROL , Self.y_thresholdID , set_value = old_value
ENDIF ELSE BEGIN
   Self.resp.y_threshold = ULONG(y_threshold)
ENDELSE

END
;______________________________________________________________________________
; Accept properties and quit
;______________________________________________________________________________
FUNCTION eis_flare_gui::check_values

valid = {valid:1B , mText:['','','','','']}
valid.mText[0] = 'This Event Trigger is NOT valid!'
valid.mText[4] = 'Do you want to quit anyway?'

x_threshold = Self.resp.x_threshold
y_threshold = Self.resp.y_threshold
; X/T Threshold
lower_limit = 1
upper_limit = ULONG(2.^32)-1
IF (x_threshold LT lower_limit) OR                                         $
   (y_threshold LT lower_limit) OR                                         $
   (x_threshold GT upper_limit) OR                                         $
   (y_threshold GT upper_limit) THEN BEGIN
       valid.mText[2] = '(Error: Threshold values are out of range)'
       valid.valid    = 0
       RETURN , valid
ENDIF

; Check Response Study and Repointing Raster IDs are valid
IF (Self.resp.response_st_id LT 1) THEN BEGIN
   valid.mText[2] = '(Error: Response Study is NOT selected)'
   valid.valid    = 0
   RETURN , valid
ENDIF
IF (Self.resp.raster_identifier LT 1) THEN BEGIN
   valid.mText[2] = '(Error: : Repointing Raster is NOT selected)'
   valid.valid    = 0
   RETURN , valid
ENDIF


; X/T Min Limit - no checking required. Using lovely sliders now.

RETURN , valid

END
;______________________________________________________________________________
; Accept properties and quit
;______________________________________________________________________________
PRO eis_flare_gui::accept

; Some checking.
valid = Self->check_values()
IF (valid.valid NE 1) THEN BEGIN
    BEEP
    question = DIALOG_MESSAGE( valid.mText                           ,     $
                     DIALOG_PARENT = Self.tlbID                      ,     $
                     /QUESTION                                       ,     $
                     /DEFAULT_NO)
    IF (question EQ 'No') THEN RETURN
ENDIF

; Update response entry in response DB here.
db_save_response_entry_update , Self.resp
; Redraw timeline plot window
Self.parentClass->refresh_plot
; Quit GUI
Self->Control, /Destroy

END
;______________________________________________________________________________
; Define Data members
;______________________________________________________________________________
PRO eis_flare_gui__Define

    struct = { eis_flare_gui                                           ,     $
                    tlbID                 : 0L                         ,     $
                    group_leader          : 0L                         ,     $
                    parentClass           : OBJ_NEW()                  ,     $
                    resp                  : {eis_response_props}       ,     $
                    ra_index              : 0B                         ,     $
                    ll_id                 : 0L                         ,     $
                    resp_id               : 0L                         ,     $
                    R_STBaseID            : 0L                         ,     $
                    ra1_BaseID            : 0L                         ,     $
                    ra2_BaseID            : 0L                         ,     $
                    ra_1_DroplistID       : 0L                         ,     $
                    ra_2_DroplistID       : 0L                         ,     $
                    llDroplistID          : 0L                         ,     $
                    rst_idID              : 0L                         ,     $
                    rst_acronymID         : 0L                         ,     $
                    x_thresholdID         : 0L                         ,     $
                    y_thresholdID         : 0L                         ,     $
                    x_min_limitID         : 0L                         ,     $
                    y_min_limitID         : 0L                         ,     $
                    x_repointID           : 0L                         ,     $
                    y_repointID           : 0L                         ,     $
                    locate_xID            : 0L                         ,     $
                    locate_yID            : 0L                         ,     $
                    noDataID              : 0L                         ,     $
                    ra1_xID               : 0L                         ,     $
                    ra1_yID               : 0L                         ,     $
                    ra2_useButtonID       : 0L                               $
             }

END
