;------------------------------------------------------------------------------;
;+
; NAME: 
;		     eis_test_pointing_gui
; PURPOSE:
;		     This object
; INPUTS:
;		     GROUP -  
;-
;------------------------------------------------------------------------------;
FUNCTION eis_test_pointing_gui::Init

;-------------------create the main base---------------------------------------;
widTitle = "EIS Test Pointing GUI"
TLB_EVENT                   = {eis_test_pointing_gui_Event}
TLB_EVENT.eis_test_pointing_gui       = Self
TLB_EVENT.Type              = "TLB"
Self.tlbID                  = WIDGET_BASE(                                  $
                                    TITLE       = widTitle,                 $
                                    COLUMN      = 1,                        $
                                    ;/MODAL         ,                        $
                                    UValue      = TLB_EVENT)

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

RETURN, 1

END
;______________________________________________________________________________
; 
;______________________________________________________________________________
PRO eis_test_pointing_gui::createGUI

;; ______________________________________________________________________
;;
;; TOP base
;;
;; ______________________________________________________________________
;topBase                     = WIDGET_BASE(Self.tlbID ,                      $
;                                    COLUMN      = 1  ,                      $
;                                    FRAME       = 1 )
; ______________________________________________________________________
;
; Time base
;
; ______________________________________________________________________
timeBase                    = WIDGET_BASE(Self.tlbID ,                      $
                                    ROW         = 1  ,                      $
                                    FRAME       = 1 )

;---------------------------------------------------------------------------
;
; Time START
;
;---------------------------------------------------------------------------
startBase                   = WIDGET_BASE(timeBase ,                        $
                                    ROW         = 1)

aLabel                      = WIDGET_LABEL(startBase ,                      $
                                    value       = 'Start Time:'    ,        $
                                    /ALIGN_LEFT ,                           $
                                    xSize       = 100)
;
; Create "Start Time" text widget - set current time
;
get_utc , utc
tStart_tai = anytim2tai(utc)
Self.tstart_gui = OBJ_NEW("eis_datetime_gui", Self ,                        $
                               startBase , tStart_tai , type = 'start')


STUDY_EVENT                 = {eis_test_pointing_gui_Event}
STUDY_EVENT.eis_test_pointing_gui = Self
STUDY_EVENT.Type            = "STUDY"
STUDYbutton                 = WIDGET_BUTTON(timeBase,                       $
                                    value       = ' Select Study ',         $
                                    Event_Pro   = 'eis_test_pointing_gui_EV', $
                                    uValue      = STUDY_EVENT)

; ______________________________________________________________________
;
; TEXT base
;
; ______________________________________________________________________
textBase                    = WIDGET_BASE(Self.tlbID ,                      $
                                    COLUMN      = 1  ,                      $
                                    FRAME       = 1 )

value = STRING(format='(A-26,A-25,A-9,A-9,A-6,A-6)'        ,                $
                 'start time','stop time','sc_x','sc_y','mip','yip')
aLabel                      = WIDGET_LABEL(textBase,                        $
                                    value       = value               ,     $
                                    /ALIGN_LEFT)

Self.textbox                = WIDGET_TEXT(textBase,                         $
                                    value       = ''          ,             $
                                    ySize       = 10          ,             $
                                    xSize       = 100)

Self.st_label               = WIDGET_LABEL(textBase,                        $
                                    value       = 'No study selected' ,     $
                                    /ALIGN_LEFT               ,             $
                                    /DYNAMIC_RESIZE)
; ______________________________________________________________________
;
; Bottom base
;
; ______________________________________________________________________
bottomBase                  = WIDGET_BASE(Self.tlbID ,                      $
                                    ROW         = 1  ,                      $
                                    /FRAME           ,                      $
                                    /GRID_LAYOUT)


QUIT_EVENT                  = {eis_test_pointing_gui_Event}
QUIT_EVENT.eis_test_pointing_gui      = Self
QUIT_EVENT.Type             = "QUIT"
quitbutton                  = WIDGET_BUTTON(bottomBase,                     $
                                    value       = ' Dismiss ',              $
                                    Event_Pro   = 'eis_test_pointing_gui_EV',         $
                                    uValue      = QUIT_EVENT)


END

;______________________________________________________________________________
; 
;______________________________________________________________________________
PRO eis_test_pointing_gui_Cleanup , wid

WIDGET_CONTROL , wid , get_uvalue = eis_test_pointing_gui_Event
OBJ_DESTROY    , eis_test_pointing_gui_Event.eis_test_pointing_gui

END
;______________________________________________________________________________
; Cleanup
;______________________________________________________________________________
PRO eis_test_pointing_gui::CleanUp

OBJ_DESTROY , Self.tstart_gui

END
;______________________________________________________________________________
;
;______________________________________________________________________________
PRO eis_test_pointing_gui::setProperty, author_struct = author_struct

IF (N_ELEMENTS(author_struct) GT 0) THEN BEGIN

    stop

ENDIF

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

tlbID = Self.tlbID

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

    Return, Self.tlbID
End

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

Widget_Control, Self.tlbID, _Extra=Widget_Control_Keywords

End

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

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

End

;______________________________________________________________________________
; Realize wrapper
;______________________________________________________________________________
PRO eis_test_pointing_gui::Realize

Widget_Control, Self.tlbID, /Realize
	
End

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

XMANAGER , "eis_test_pointing_gui" ,   Self.tlbID ,                                 $
    Event_Handler  = "eis_test_pointing_gui_EV",                                    $
    cleanup        = "eis_test_pointing_gui_Cleanup",                               $
    GROUP_LEADER   = Self.group_leader ,                                    $
    /NO_BLOCK

end

;______________________________________________________________________________
; 
;______________________________________________________________________________
PRO eis_test_pointing_gui_Event__Define

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

;Get event handler structure containing eis_test_pointing_gui type object
Widget_CONTROL,Event.ID,get_uvalue = eis_test_pointing_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_test_pointing_gui's class member procedure "Handler"
eis_test_pointing_gui_Event.eis_test_pointing_gui->Handler,Event

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

WIDGET_CONTROL, Event.ID, Get_UValue = eis_test_pointing_gui_Event
CASE eis_test_pointing_gui_Event.Type OF
    "QUIT"      : Self->Control, /Destroy
    "STUDY"     : Self->select_study
    ELSE        : print,eis_test_pointing_gui_Event.Type
ENDCASE
END
;______________________________________________________________________________
; 
;______________________________________________________________________________
PRO eis_test_pointing_gui::select_study

eis_xstudy , parentClass = Self

END
;______________________________________________________________________________
; 
;______________________________________________________________________________
PRO eis_test_pointing_gui::loadstudy , st_id , response_study = response_study

; Load study
Self.st = db_read_study_entry(st_id)
WIDGET_CONTROL , Self.textbox , set_value = ''

; Update study info
st_string = STRING(format='(A-10,I06,A-2,A-10,A-20)' ,                      $
                        'Study ID:'                ,                        $
                        Self.st.id                 ,                        $
                        ''                         ,                        $
                        'Acronym:'                 ,                        $
                        Self.st.acronym)
WIDGET_CONTROL , Self.st_label , set_value = STRTRIM(st_string,2)

; Finally, get raster pointing
Self->get_raster_pointing

END
;______________________________________________________________________________
; 
;______________________________________________________________________________
PRO eis_test_pointing_gui::get_raster_pointing

IF (Self.st.nRasters EQ 0) THEN BEGIN
    BEEP
    mText = 'This is an engineering study. Aborting!'
    complain = DIALOG_MESSAGE( mText       ,                        $
                         DIALOG_PARENT = Self.tlbID,                $
                         /ERROR )
    RETURN

ENDIF

; Get study start time from datetime GUI
Self.tstart_gui->getProperty , datetime = st_start_time

WIDGET_CONTROL , Self.textbox , ySize = (Self.st.nRasters + 1) > 10

FOR i = 0 , Self.st.nRasters - 1 DO BEGIN

    sra = Self.st.studyRasters[i]

    ; Get raster start & stop time
    sra.ra_start_time = st_start_time
    sra.ra_stop_time  = st_start_time + (sra.ra_duration/1000.)

    ; Reset st_start_time
    st_start_time     = sra.ra_stop_time

    ; Get spacecraft pointing info
    pointing = eis_get_raster_pointing(sra.ra_start_time , sra.ra_stop_time)
    ; Update raster pointing
    IF (N_TAGS(pointing) NE 0) THEN BEGIN

        sra.x_point = pointing.x_point
        sra.y_point = pointing.y_point
        sra.sc_x    = pointing.x_point
        sra.sc_y    = pointing.y_point

        ; Get MIP and YIP
        ra_id       = sra.ra_id
        mirror_pos  = eis_get_mirror_position(0. , 0. , ra_id)
        sra.mip     = mirror_pos.mip
        sra.yip     = mirror_pos.yStart

    ENDIF


    line = STRING(format='(A-25,A-25,F-9.2,F-9.2,I-6,I-6)' ,                $
                     anytim2utc(sra.ra_start_time,/stime ) ,                $
                     anytim2utc(sra.ra_stop_time ,/stime ) ,                $
                     sra.sc_x                              ,                $
                     sra.sc_y                              ,                $
                     sra.mip                               ,                $
                     sra.yip                                                $
                 )

    IF (i EQ 0) THEN append = 0 ELSE append = 1
    WIDGET_CONTROL , Self.textbox , set_value = line , append = append


ENDFOR

END
;______________________________________________________________________________
; Define Data members
;______________________________________________________________________________
PRO eis_test_pointing_gui__define

    struct          =	{ eis_test_pointing_gui                         ,   $
                                    tlbID             : 0L              ,   $
                                    group_leader      : 0L              ,   $
                                    tstart_gui        : OBJ_NEW()       ,   $
                                    textbox           : 0L              ,   $
                                    st_label          : 0L              ,   $
                                    st                : {eis_study}     ,   $
                                    pointing_output   : 0L                  $
                      }


END
