#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'open-uri'

Shoes.app :title => "Weather, powered by National Weather Service" do
  stack do
    background blue, :height => 60
    flow :margin => 20 do
      caption "Weather Shrug: ", :stroke => white
      state_list = list_box :items => ['AL','AK','AS','AZ','AR','CA','CO','CT','DE','DC','FM','FL','GA','GU','HI','ID','IL','IN','IA','KS','KY','LA','ME','MH','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PW','PA','PR','RI','SC','SD','TN','TX','UT','VT','VI','VA','WA','WV','WI','WY']
      button "Go" do
        Thread.start do
          stations_page = open(URI("http://www.weather.gov/data/current_obs/seek.php?state=#{state_list.text.downcase!}&Find=Find")).read
          stations = stations_page.scan(%r!<td headers="Station Name"><a href="(.*?)">(.*?)<\/a>(.*)<\/td>!)
          stations.each do | each_station |
            station_page = open(URI(each_station[0])).read
            station_name = each_station[2].to_s
            station_temp = station_page.scan(%r!\sTemperature\s(?:.*?),(.*?)\)!mx)[0][0].gsub(%r!Helvetica\">!, '')
            @weather_stack.append do
              para "Station Name: " + station_name
              para "Station Temp: " + station_temp + ")"
            end
          end
  
        end
      end
    end
    @weather_stack = stack :margin => 20 do
    end
  end
end