# JStrack:  copyright 1997/2010 by Jim Graham, N5IAL, all rights reserved.
#

proc select_storm_name {} {
   global st_names

   set i 0
   array set retval {}
   foreach name $st_names {
      incr i
      set retval($i) "$name"
      puts "$i) $retval($i)"
   }
   incr i
   puts "$i) never mind...."
   set choice [gets stdin]
   puts ""
   if {$choice < 1 || $choice >= $i} return ""
   return "$retval($choice)"
}


proc select_storm_ts {name} {
   global st_times advisory_list

   set i 0
   array set retval {}
   foreach timestamp $st_times($name) {
      incr i
      set retval($i) $timestamp
      set idx $name$timestamp
      puts "$i) $name $timestamp (Advisory $advisory_list($idx))"
   }
   incr i
   puts "$i) never mind...."
   set choice [gets stdin]
   puts ""
   if {$choice < 1 || $choice >= $i} return ""
   return "$retval($choice)"
}


proc select_storm_fc_ts {name} {
   global st_times advisory_list fc_tm_idx

   set i 0
   array set retval {}
   foreach timestamp $st_times($name) {
      set idx $name$timestamp
      if {[array get fc_tm_idx $idx] != ""} {
         incr i
         set retval($i) $timestamp
         puts "$i) $name $timestamp (Advisory $advisory_list($idx))"
      }
   }
   if {$i == 0} {
      puts "No forecast data is available for this storm."
      puts "Press <return> to return to menu"   ;  gets stdin
      return
   }
   incr i
   puts "$i) never mind...."
   set choice [gets stdin]
   puts ""
   if {$choice < 1 || $choice >= $i} return ""
   return "$retval($choice)"
}


proc st_menu {} {
   global st_names
   set done 0
   while {!$done} {
      puts ""
      puts "1) List active storms"
      puts "2) Details on a storm at a given time"
      puts "3) Forecast details for storm at a given time"
      puts "4) Display history of a storm"
      puts "5) Reload storm data"
      puts "6) Enter data manually for storm"
      puts "7) Read NHC advisory data for storm"
      puts "8) Display the Saffir-Simpson scale"
      puts "0) Exit from menu"
      puts ""
      puts -nonewline "> "
      flush stdout
      set choice [gets stdin]
      puts ""
      if {[string compare "$choice" "1"] == 0} {
         if {$st_names == ""} {
            puts "No storm information is loaded"
         } else {
            foreach name $st_names { puts "$name" }
         }
         puts "Press <return> to return to menu"   ;  gets stdin
      } elseif {[string compare "$choice" "2"] == 0} { menu_st_index
      } elseif {[string compare "$choice" "3"] == 0} { menu_fc_index
      } elseif {[string compare "$choice" "4"] == 0} { menu_st_hist
      } elseif {[string compare "$choice" "5"] == 0} { menu_reload
      } elseif {[string compare "$choice" "6"] == 0} { st_data
      } elseif {[string compare "$choice" "7"] == 0} { read_adv
      } elseif {[string compare "$choice" "8"] == 0} { sschart
      } elseif {[string compare "$choice" "0"] == 0} { return
      } else { puts "eh?" }
      puts ""
   }
}

proc menu_fc_index {{name ""}} {
   global st_names

   if {$st_names == ""} {
      puts "No storm information is loaded"
      return
   }

   set name [select_storm_name]
   if {$name == ""} { return }
   set timestamp [select_storm_fc_ts $name]
   if {$timestamp == ""} { return }

   fc_info $name $timestamp
   puts "Press <return> to return to menu"  ;  gets stdin
}


proc menu_st_index {} {
   global st_names

   if {$st_names == ""} {
      puts "No storm information is loaded"
      return
   }

   set name [select_storm_name]
   if {$name == ""} { return }
   set timestamp [select_storm_ts $name]
   if {$timestamp == ""} { return }

   st_info $name $timestamp
   puts "Press <return> to return to menu"  ;  gets stdin
}

proc menu_st_hist {} {
   global st_names

   if {$st_names == ""} {
      puts "No storm information is loaded"
      return
   }

   set name [select_storm_name]
   if {$name == ""} { return }

   st_hist $name
   puts "Press <return> to return to menu"  ;  gets stdin
}

# This can probably be improved quite a bit...but for now....

proc menu_reload {} {
   global advisory_list current_storm_name storms

   set i 0
   array set selected_storm {}
   catch {
      foreach filename [glob $storms/*.trk] {
         incr i
         set selected_storm($i) [file tail [file rootname $filename]]
         puts "$i) [string toupper $selected_storm($i)]"
      }
   }
   incr i ; puts "$i) load all storm data" ; set loadall $i
   incr i ; puts "$i) load storm data from past years" ; set loadpast $i
   puts "0) never mind..."
   set choice [gets stdin]
   if {$choice > 0 && $choice < $loadall} {
      wipeclean $selected_storm($choice)
      source $storms/$selected_storm($choice).trk
   } elseif {$choice == $loadall} {
      wipeclean
      foreach filename [glob $storms/*.trk] {
         source $filename
         newtrk_curr
         plot_last $current_storm_name
      }
   } elseif {$choice == $loadpast} {
      menu_past_reload
   }
   set fakeset ""  ;# prevents the last result from being displayed
}


proc menu_past_reload {} {
   global advisory_list current_storm_name storms

   set i 0
   array set selected_storm {}
   catch {
      foreach dir [glob $storms/old/{1,2}*] {
         set dir [file tail $dir]
         if {[file isdirectory $storms/old/$dir] && $dir != "locks"} {
            foreach filename [glob $storms/old/$dir/*.trk] {
               incr i
      set selected_storm($i)    old/$dir/[file rootname [file tail $filename]]
      puts "$i) [string toupper $dir/[file rootname [file tail $filename]]]"
            }
         }
      }
   }
   puts "0) never mind..."
   set choice [gets stdin]
   if {$choice > 0 && $choice < $loadall} {
      wipeclean $selected_storm($choice)
      source $storms/$selected_storm($choice).trk
   } elseif {$choice == $loadall} {
      wipeclean
      foreach filename [glob $storms/*.trk] {
         source $filename
         newtrk_curr
         plot_last $current_storm_name
      }
   } elseif {$choice == $loadpast} {
      menu_past_reload
   }
   set fakeset ""  ;# prevents the last result from being displayed
}

