#############################
# Mike Schilli, 2008
# (m@perlmeister.com)
#############################
package Waterscore;

use base Exporter;
@EXPORT_OK = qw(waterscore);

use strict;
use warnings;
use Weather::Google;
use Log::Log4perl qw(:easy);

#############################
sub waterscore {
#############################
 my ($loc) = @_;

 my $w =
   Weather::Google->new(
  94114);

 my ($cond, $temp, $humi) =
   $w->current('condition',
  'temp_c', 'humidity');

 if (!defined $cond) {
  ERROR
    "Failed to get weather";
  return undef;
 }

 ($humi) = $humi =~ /(\d+)/;
 DEBUG
   "$cond, $temp C, $humi%";

 my $score = 50;
 $score += 50 if $humi < 50;
 $score -= 30
   if $cond =~ /Showers/;
 $score -= 80
   if $cond =~ /Rain/;
 $score += 80
   if $cond =~ /Sunny/;

 if ($temp > 20) {
  $score +=
    ($temp - 15) * 10;
 }

 if ($temp < 16) {
  $score /= 2.0;
 }

 $score = 100
   if $score > 100;
 $score = 0 if $score < 0;
 DEBUG "Score: $score";
 return $score;
}

1;
