;+
; NAME        : EIT_PA_UTIL
;
; PURPOSE     : Routines to handle various user actions from EIT_PA_EV
;
; CATEGORY    :
;
; EXPLANATION : 
;
; SYNTAX      : 
;
; EXAMPLES    : none
; 
; CALLED BY   :
;
; CALLS TO    : none
;
; ARGUMENTS   :	none
;
; KEYWORDS    : none
;
; OUTPUTS     : none
;
; COMMON      : none
;
; RESTRICTIONS: none
;
; SIDE EFFECTS: none
;
; PROJECT     : SOHO - EIT
;
; HISTORY     : V1,  Elaine Einfalt (EIT)
;
; CONTACT     : eit@xanado.nascom.nasa.gov
;-

pro EIT_PA_UTIL, sel_struc, event=event, $
		 do_date=do_date, do_cat=do_cat, do_pick=do_pick, $
		 do_draw=do_draw, do_movie=do_movie, $
		 movie_process=movie_process
common eit_pa_list, in_list, out_list, out_pa

 ;
 ; The user input a date for a new catalog list
 ;

 if keyword_set(do_date) then begin

		; Get user input date
		widget_control, sel_struc.sel_dat_text, get_value=date

		; Convert user supplied time to ecs format
                date = anytim2utc(date(0), /vms, /date)
		widget_control, sel_struc.sel_dat_text, set_value=date

		; Read catalog infor for that day
                oneday = EIT_CATRD( date, /no_next, wave=sel_struc.cur_line)

		if strtrim(oneday(0),2) ne '' then begin    ; have a catalog

		   in_list = oneday

		   ; Load catalog info from selected date into the select list
		   widget_control, sel_struc.cat_list_id, $
				   set_value=in_list
		   
		endif else begin
		   widget_control, sel_struc.cat_list_id, $
				   set_value='No data on this date'
		   in_list=''
		   return
		endelse
 endif

 ;
 ; The user clicked on the movie button, so display all eit EMS  movies in
 ; the top select window
 ;

 if keyword_set(do_movie) then begin

		dirspec = getenv('MOVIE_DIR')          
		movielist = find_file(concat_dir(dirspec(0),'ems*.*'))

		in_list = movielist

	        ; Load catalog info from selected date into the select list
	  	widget_control, sel_struc.cat_list_id, set_value=in_list

 endif


 ;
 ; The user clicked in the top list window
 ;

 if keyword_set(do_cat) then begin

		; The user clicked an item in the top list
		; So what do we do with the user's selection


		if in_list(0) eq '' then return		; nothing to do



		; Which one(s) did the user click or highlight

 		user_picks = widget_info(event.id, /list_select)

		case sel_struc.mode of

		   0: begin	; move this entry to the bottom process list
				; if this is already there
				; (If we are in movie mode then break open file
				; and write the contents to bottom window)

		      if not(sel_struc.movie) then begin   ; not in movie mode

		         ; Move this/these to out_list
		         if out_list(0) ne '' then begin

		            ; Something is in the out list, 
			    ; so merge and make uniq

		            new_pick= [out_list, in_list(user_picks)]
		            new_pick = new_pick(sort(new_pick))
		            out_list = new_pick(uniq(new_pick))

		         endif else begin

		           ; Nothing in out list, so just send user pick(s) 
			   ; to list
		           out_list = in_list(user_picks)

	                 endelse


 	                 ; What is the current top visible item
		         top_item = widget_info(sel_struc.pick_list_id, $
						/list_top)

		         ; Write out the new and improved user selected list
		         ; keeping the top visisble entry at the top

	                 widget_control, sel_struc.pick_list_id,	$
		      		      set_value = out_list,   $
				      set_list_top = top_item

		      endif else begin		; in movie mode

		         ; Need the time info from the movie file
    		         ;    STAMP_TIME strarr of time for each frame

		         restore, in_list(user_picks)	; restore movie file
			 b0 = 0				; clean up big array

			 ; Need to construct an out_list to help code later,
			 ; since we dont' get a full catalog output for movies

			 n_frames = n_elements(stamp_time)
			 frames = strtrim(indgen(n_frames),2) 

			 out_list = frames + ': ' + stamp_time + ' (' + $
				    strtrim(wave,2) +')      ' + $
				    in_list(user_picks)

		         ; Write out the time stamps for the movie frames

	                 widget_control, sel_struc.pick_list_id,	$
		      		         set_value = out_list
			

		      endelse

		      end


		   1: begin	; view a single clicked image
				; if a range is highlighted, use first image

		      widget_control, sel_struc.sel_base, sensitive=0
		      if not(sel_struc.movie) $
 		         then EIT_PA_VIEW, in_list(event.index), movie=0 $
		         else print, 'EIT_PA_UTIL, can''t single view movie file'
		      widget_control, sel_struc.sel_base, sensitive=1

		      end


		   2: begin	; process mode does nothing in top list

			print,'%EIT_PA_UTIL, Use process mode only with bottom list'

		      end

		   else: print,'%EIT_PA_UTIL, Impossible case'
		endcase



 endif


 ;
 ; The user clicked in the bottom list window
 ;

 if keyword_set(do_pick) then begin

		; The user clicked an item in the bottom list
		; So what do we do with the user's selection

		case sel_struc.mode of


		   0: begin	

		      ; User in select mode, and just clicked in the bottom list
		      ; So, remove the item(s) selected from the process list

		      ; Which one(s) did the user pick

 		      user_picks = widget_info(event.id, /list_select)  ; picked

		      ; How many did the user pick

		      numb_picks = n_elements(user_picks)

		      ; How many total are in the list

		      list_number = widget_info(event.id, /list_number) ; total

		      ; Change the array to reflect the usser's subtraction(s)

		      case 1 of

			; All items (or only item) were removed
		        numb_picks eq list_number : $
			   out_list = ''
	
			; First element, or first several, are removed
		        user_picks(0) eq 0 : $		
			   out_list = out_list(1+(numb_picks-1):list_number-1)

			; Last item, or last several, are to be removed
		        user_picks(numb_picks-1) eq list_number-1: $
			   out_list = out_list(0:list_number-numb_picks-1)

			; Middle element, or several, are to be removed
		        else: $
			   out_list = [out_list(0:user_picks(0)-1), $
				       out_list(user_picks(numb_picks-1)+1:*)]
		      endcase

   		      ; What is the current top visible item
		      top_item = widget_info(event.id, /list_top)

		      ; Write out the new and improved user selected list
	              widget_control, sel_struc.pick_list_id,	$
				set_value = out_list, $
				set_list_top = top_item
		   endelse

		   1: begin	; view a single clicked image
				; if a range is highlighted, use first image

		      widget_control, sel_struc.sel_base, sensitive=0
 		      EIT_PA_VIEW, out_list(event.index), $
				   movie=sel_struc.movie
		      widget_control, sel_struc.sel_base, sensitive=1

		      end

		   2: begin	; process the select item for R and theta

		      if out_list(0) eq '' then begin
		         widget_control, sel_struc.pick_list_id, $
			        set_value='You must first select some images'
		         return
	              endif	

		      ; Process the currently highlighted item in bottom list

  		      item = widget_info(sel_struc.pick_list_id, /list_select)

		      EIT_PA_PROCESS, out_list(item), $
				      sel_struc=sel_struc

		      end

		endcase 			; of sel_struc.mode

 endif


 ;
 ; The user clicked in the image window
 ;

 if keyword_set(do_draw) then begin


		; A mouse click in the display (draw) window

		if event.press then return	; ignore button down click


		; Which list item are we doing
		
		item = widget_info(sel_struc.pick_list_id, /list_select)


		; put a mark, kind of close, to where user just clicked

	   	xyouts, event.x-1, event.y-1, 'o', /device
	   	xyouts, event.x, event.y+10, strtrim(item,2), /device


		tnames = tag_names(event)


		 ; For all release mouse clicks inside the draw window, 
		 ; figure the arcsec, heliographic, and R/theta coordinates

		 ; the event x and y are in device, convert to the 
		 ; established coord system (arcsec)

		 data_coord = convert_coord(event.x,event.y, /device, /to_data)

		 ; In arcsecs

		arcsec = ',  X:' + string(round(data_coord(0)),'(i5.1)') + $
                         ',  Y:' + string(round(data_coord(1)),'(i5.1)')

;		print, 'Arcsec : ' + arcsec



;		; In heliographic
;
;		 lat_lon = round(ARCMIN2HEL([data_coord(0)/60.], $
;				[data_coord(1)/60.], $
;                                date=image_time,/soho))
;
;		 if lat_lon(0) ge 0 $
;		    then lat = strtrim(abs(lat_lon(0)),2) + ' N,  ' $
;                    else lat = strtrim(abs(lat_lon(0)),2) + ' S,  '
;
;		 if lat_lon(1) ge 0 $
;		    then if lat_lon(1) lt 95 $
;			     then lon = strtrim(abs(lat_lon(1)),2) + ' W' $
;                             else lon = '90 W' $
;		    else if lat_lon(1) gt -95 $
;			      then lon = strtrim(abs(lat_lon(1)),2) + ' E' $
;                              else lon = '90 E'
;
;		 print, 'Helio : ' + lat + ' ' + lon
  

		 ; In R and Theta

 		theta_r = cv_coord(/degrees, $
			  from_rect=[data_coord(0),data_coord(1)], $
                    	  /to_polar)

		 if theta_r(0) lt 0 then theta = 180 + theta_r(0) + 90 $
		 else if theta_r(0) ge 90 then theta = theta_r(0) - 90 $
		 else theta = 270 + theta_r(0)

		 r_theta =  ',  R:' + string(theta_r(1)/960.,'(f5.2)') + $
                            ',  Theta:' +string(fix(theta),'(i3.3)') 

;                 print, 'R/Theta : ' + r_theta 
 
		 ; OK, so let's put R/theta (and x/y)  into the out_pa index
		 ; the date and time are already in fields 0:19

		 str = out_pa(item) + $			; area to splice in
		       ',  R:xxxxx,  Theta:xxx,  X:xxxxx,  Y:xxxxx'
		 strput, str, r_theta+arcsec, 19	; splice in r/t

		 ; Assign it to the array that will be passed back to the user

		 out_pa(item) = str 				; assign 

		 tot_al = widget_info(sel_struc.pick_list_id, /list_number)


		 widget_control, sel_struc.pick_list_id, set_list_select=item


		 ; Ok so done with that one, automatically move to the next
		 ; image. If the user wants to go redo one they will need to
	         ; click in the pick list, aftet this next image is displayed

		 ; How many total are in the list

		 list_number = widget_info(sel_struc.pick_list_id, $
					   /list_number) 

		 if item+1 lt list_number then begin	

		    ; Not at the end of list, yet, so move to next image

		    ; Highlight the next item in the list

		    widget_control, sel_struc.pick_list_id, $
				    set_list_select=item+1
				    
		    ; Use the currently highlighted item (we just set it)

		    EIT_PA_UTIL, sel_struc, event=event, /do_pick

		 endif else begin	; at last item in the list

	   	   xyouts, event.x+10, event.y+10, 'DONE', /device
	   	   xyouts, event.x, event.y, 'x', /device

		 endelse

 endif


return
end
