;+ 
;NAME:  
;  tplot_gui
;  
;PURPOSE:
;  Imports and creates plot of tplot variable in THM_GUI_NEW.
;  
;CALLING SEQUENCE:
;  tplot_gui, [datanames]
;  
;INPUT:
;  datanames: A string of space separated datanames.  Wildcard expansion is
;             supported.  If datanames is not supplied then the last values are
;             used. Each name should be associated with a data quantity.
;             (see the "STORE_DATA" and "GET_DATA" routines.) Alternatively 
;             datanames can be an array of integers or strings.  Run
;             "TPLOT_NAMES" to show the current numbering.
;            
;KEYWORDS:
;  /NO_VERIFY: Bypasses the Verify window before plotting the data in the GUI.
;             Intended to be used in cases when it is certain that the limits
;             and dlimits of the incoming tplot variables are complete and
;             correct (e.g. overview plot generation).  Use with caution.
;  /NO_DRAW:  Data is loaded, but it is not plotted.
;        
;OUTPUT:
;  none
;
;--------------------------------------------------------------------------------

pro make_plots, newpanel, varname

  compile_opt idl2, hidden
    
  ; get group object and x-axis data quantity name
  group = !thm_gui_new.loadedData->getGroup(varname)
  xname = group->getTimeName()
  
  ; get dlimits/limits and merge into superstructure
  get_data, varname, dlimits=dl, limits=l
  extract_tags,dl,l

  if (size(dl, /type) eq 8) then begin
  
    if in_set('spec', strlowcase(tag_names(dl))) then begin
    
      if dl.spec eq 1 then begin  ; create specplot
      
        groupname = group->getName()
        yname = group->getYaxisName()
        
        thm_ui_make_default_specplot, !thm_gui_new.loadedData, newpanel, xname, $
                                      yname, groupname
      
      endif else begin    ; create lineplot
      
        ynames = group->getDataNames()
        
        for j=0, n_elements(ynames)-1 do begin
        
          thm_ui_make_default_lineplot, !thm_gui_new.loadedData, newpanel, xname, $
                                        ynames[j]
        endfor
      endelse
    endif else begin    ; create lineplot
      ynames = group->getDataNames()
      
      for j=0, n_elements(ynames)-1 do begin
      
        thm_ui_make_default_lineplot, !thm_gui_new.loadedData, newpanel, xname, $
                                      ynames[j]
      endfor     
    endelse
  endif else begin   ; create lineplot
  
    ynames = group->getDataNames()
    
    for j=0, n_elements(ynames)-1 do begin
    
      thm_ui_make_default_lineplot, !thm_gui_new.loadedData, newpanel, xname, $
                                    ynames[j]
    endfor
  endelse

end


pro tplot_gui, datanames, no_verify=no_verify,no_draw=no_draw

  compile_opt idl2
  
  
  ; check type and dimension of input
  dt = size(/type,datanames)
  ndim = size(/n_dimen,datanames)
  
  
  ; check for valid input
  if dt ne 0 then begin
   if dt ne 7 or ndim ge 1 then dnames = strjoin(tnames(datanames,/all),' ') $
     else dnames=datanames
  endif else begin
    last_tplot = tnames(/tplot)
    dprint,'Recreating the last tplot command with the following tplot variables:'
    dprint, last_tplot
    dnames = strjoin(last_tplot,' ')
  endelse
  
  
  ; make sure indexes are converted array of tplot variable names
  varnames = tnames(dnames,nd,ind=ind,/all)
  varnames2 = '' ; same as above but will have pseudovars expanded to component varnames
  varnames3 = '' ; to handle successfully loaded varnames
  temp2del = '' ; array of tplot pseudovariable names to delete
  
  if nd eq 0 then begin
     dprint,'No valid variable names found to tplot! (use <VARIABLE> = TNAMES() and PRINT,<VARIABLE> to display)'
     return
  endif
  
  
  ; check if gui already running, start if not
  if (xregistered('thm_gui_new') eq 0) then begin
    thm_gui_new
    newwin = 0   ; flag if a new window needs to be created
  endif else newwin = 1
  
  
  ; add tplot variables to loadedData object
  for i=0L,nd-1 do begin
  
    ; check if pseudovariable
    get_data, varnames[i], data=d
    dSize = size(d, /type)
    
    if dSize eq 7 then begin
    ;load tplot pseudovariable
      
      subNames = tnames(d, sub_nd, ind=sub_ind, /all)
      varnames3 = [varnames3, varnames[i]]
      
      rebuild=0
      for j=0L,sub_nd-1 do begin
      ; load each component of pseudovariable
        
        fail=0
        if ~!thm_gui_new.loadedData->add(subNames[j]) then begin
;          ok = error_message('Add failed: ' + subNames[j] + '. the pseudovariable ' + varnames[i] + $
;               ' has an invalid variable.  Nothing will be plotted, but some data might have' + $
;               ' already been added to the GUI.', /traceback, /center, title='Error in TPLOT_GUI')
          fail=1
          rebuild=1
          ;return
        endif
        
        if ~fail then begin
          varnames2 = [varnames2, subNames[j]]
        endif
      endfor
      
      if rebuild then begin
      ; create a new temp pseudovar that only contains valid sub-variables
        get_data, varnames[i], dlimits=dl, limits=l
        store_data, varnames[i]+'_tmp', data=varnames2, dlimits=dl, limits=l
        varnames[i]=varnames[i]+'_tmp'
        varnames3[i+1] = varnames[i]
        temp2del = [temp2del, varnames[i]]
      endif
  
    endif else begin
    ; load standard tplot variable
  
      fail=0
      if ~!thm_gui_new.loadedData->add(varnames[i]) then begin
        ok = error_message('Add failed: ' + varnames[i], /traceback, /center, $
               title='Error in TPLOT_GUI')
        fail=1
      endif
      
      if ~fail then begin
        varnames2 = [varnames2, varnames[i]]
        varnames3 = [varnames3, varnames[i]]
      endif
    endelse
  endfor
  
  if n_elements(varnames2) gt 1 then begin
    varnames2 = varnames2[1:*] ; remove empty string at beginning of array
  endif else begin
    if array_equal(varnames2,'', /no_typeconv) then return
  endelse
 
  if n_elements(varnames3) gt 1 then begin
    varnames3 = varnames3[1:*] ; remove empty string at beginning of array
  endif else begin
    if array_equal(varnames3,'', /no_typeconv) then return
  endelse
  
  nd3 = n_elements(varnames3)
  
  ; verify incoming tplot variables
  ;thm_ui_verify_data,!thm_gui_new.guiId, varnames, !thm_gui_new.loadedData, $
  ;                   !thm_gui_new.windowStorage, !thm_gui_new.historywin, $
  ;                   success=success
  if ~keyword_set(no_verify) then begin
    thm_ui_verify_data,!thm_gui_new.guiId, varnames2, !thm_gui_new.loadedData, $
                       !thm_gui_new.windowStorage, !thm_gui_new.historywin, $
                       success=success
  endif else success=1
 
  dataObjNames = !thm_gui_new.loadedData->getall()
  
  if ~success then begin
    
    dprint,'Data verify canceled. No tplot variables were imported.'
    !THM_GUI_NEW.historyWin->update,'Data verify canceled. No tplot variables were imported.'
    return
    
  endif else begin
  
    if ~keyword_set(no_draw) then begin
  
      ; add new window to gui
      if newwin then begin
      
        pageSettings = Obj_New('THM_UI_PAGE_SETTINGS', backgroundcolor=[255,255,255])
        
        if ~!thm_gui_new.windowStorage->add(settings=pageSettings, isactive=1) then begin
          error_message,'Error initializing new window for TPLOG_GUI.',/traceback, /center, $
                 title='Error in TPLOT_GUI'
        endif  
      
      activeWindow = !thm_gui_new.windowStorage->GetActive()
      
      ; add window name to gui window menu
      activeWindow[0]->GetProperty, Name=name
      !thm_gui_new.windowMenus->Add, name
      !thm_gui_new.windowMenus->Update, !thm_gui_new.windowStorage
      
      endif
      
      
      ; update draw object and draw new window
      !thm_gui_new.drawObject->Update, !thm_gui_new.windowStorage, !thm_gui_new.loadedData
      !thm_gui_new.drawObject->draw
      
      ;get current time range (as set by tlimit)
      ctrange = timerange(/current)
      
      ; create a separate panel for each data quantity
      for i=0L,nd3-1 do begin
      
        ; create panel
        thm_ui_make_default_panel, !thm_gui_new.windowStorage, outpanel=newpanel, $
                                   trange=ctrange
    
        ; check if pseudovariable
        get_data, varnames3[i], data=d
        dSize = size(d, /type)
        
        if dsize eq 7 then begin
        ;plot tplot pseudovariable
        
          subNames = tnames(d, sub_nd, ind=sub_ind, /all)
    
          for k=0L,sub_nd-1 do begin
          ; load each component of pseudovariable
          
            make_plots, newpanel, subNames[k]
            
          endfor
        endif else begin
        ; plot standard tplot variable
        
          make_plots, newpanel, varnames3[i]
        
        endelse
      endfor
      
      
      !thm_gui_new.drawObject->update,!thm_gui_new.windowStorage, !thm_gui_new.loadedData
      !thm_gui_new.drawObject->draw
      
    endif
  endelse


store_data, delete=tnames(temp2del)
end