# Usage:
#
# 0) setDocument( "http://www.weatheroffice.ec.gc.ca/forecast/city_e.html?yow&b_templatePrint=true" );

# 1) extractTags( weatherHandler, noSymbolTable, conditions );
#    tagsToHtml( "conditions: ->[cCond], ->[cTemp], ->[cPressure], wind at ->[cWind] and ->[cHumidity] humidity" );

# 2) extractTags( weatherHandler, noSymbolTable, forecast );
#    outputDocument( "<td valign=\"top\" width=80 align=\"center\">->[forecast]</td>", 5 );

# 3) extractTags( weatherHandler, noSymbolTable, text );
#    tagsToHtml( "->[text]" );

sub weatherHandler
{
    my $html = shift;
    my $blah = shift; # noSymbolTable isn't for us
    my $type = shift;

    # print "-------------[ weatherHandler of type -$type- ]----------------\n";

    if( $type eq "conditions" )
    {
        return currentConditions( $html );
    }
    elsif ( $type eq "forecast" )
    {
        return forecast( $html );
    }
    else
    {
        return text( $html );
    }
}

sub text
{
    my $html = shift;

    my( @rValue ) = ();
    my( $done ) = 0;
    my( $cTable, $entry, $row );
    my( $textForecast );
    my( $count, $rCount );

    #print "--------------[ Text Forecast ]------------------\n";
    #print "$html\n";

    if( $html =~ /<p>.*?Environment.*?Canada.*?(Issued.*?)<\/p>/gsi )
    {
        #print "Found the text forecast: $1\n";
        $textForecast = $1;
    }

    # condition it a bit ...
    $textForecast =~ s/<hr>/<br>/gsi;
    $textForecast =~ s/<FONT.*?(color\=\".*?\").*?>/<font $1 size=-1>/gsi;
    
    push @rValue, 
    {
        'text' => $textForecast
    };

    return @rValue;
}

sub forecast
{
    my $html = shift;

    my( @rValue ) = ();
    my( $done ) = 0;
    my( $cTable, $entry, $row );
    my( $textForecast );
    my( $count, $rCount, $done );

    print "---------------------- Weather Handler (forecast) ----------------------\n";
    #print "html: $html\n";
    #exit;

    
    $count = 3;
    $done = 0;
    while( $count < 10 && !$done )
    {
        $cTable = zUtil::getNthItemNew( $count, $html, "<TABLE", "</TABLE>" );
        $row = zUtil::getNthItemNew( 1, $cTable, "<tr", "</tr>" );

        if( $row =~ /currently/gsi )
        {
            print "found the row ...\n";
            $rCount = 10;
            while( $rCount < 60 &&
                   !$done )
            {
                $entry = zUtil::getNthItemNew( $rCount, $row, "<td", "</td>" );
                #print "entry: $entry\n";
                if( $entry =~ /Forecast/gs )
                {
                    print "Got the entry with the 5 day forecast ...\n";
                    $done = 1;
                }
                
                $rCount++;
            }
        }

        $count++;
    }

    #exit;
    #print "$entry\n";
    #exit;

    if( $done )
    {
        $cTable = zUtil::getNthItemNew( 1, $entry, "<TABLE", "</TABLE>" );
    }
    else
    {
        push @rValue, 
        { 
            'forecast' => $entry
        };
        
        return @rValue;
    }

    #print( $cTable );
    #exit;

    $count = 1;
    while( $count < 6 )
    {
        $entry = "";

        $rCount = 1;
        while( $rCount < 6 )
        {
            $row = zUtil::getNthItemNew( $rCount, $cTable, "<tr", "</tr>" );
        
            $entry .= zUtil::getNthItemNew( $count, $row, "<td", "</td>" );

            # Strip off the table formatting ... we'll add our own later
            $entry =~ s/<td.*?>//gsi;
            $entry =~ s/<\/td>//gsi;
            
            # No font's please!
            $entry =~ s/<strong.*?>/<b>/gsi;
            $entry =~ s/<\/strong>/<\/b>/gsi;
            $entry =~ s/<font.*?>//gsi;
            $entry =~ s/<\/font>//gsi;

            $entry =~ s/<center.*?>//gsi;
            $entry =~ s/<\/center>//gsi;

            $entry =~ s/width=\".*?\"//gsi;
            $entry =~ s/height=\".*?\"//gsi;

            
            $entry =~ s/<img(.*?)>/<a href\=\"http\:\/\/zedd\.org\/cgi\-bin\/zii\.pl\?weather\(\)\"><img border=0 $1><\/a>/si;
            
            $entry =~ s/<p>//gsi;
            $entry =~ s/<br><br>/<br>/gsi;
            $entry =~ s/<br>&nbsp;<br>/<br>/gsi;

            $entry .= "<br>";

            $rCount++;
        }

     #   print "entry: =================> $entry <======================\n";

        push @rValue, 
        { 
            'forecast' => $entry
        };
        $count++;
    }
   
    #exit;


    return @rValue;

}

sub currentConditions
{
    my $html = shift;

    my( @rValue ) = ();
    my( $done ) = 0;
    my( $cTable, $count );
    my( $cCondition, $cWind, $cTemp, $cPressure, $cVisibility, $cHumidity, $cHumidex);

    #print "---------------------- Weather Handler (conditions) ----------------------\n";   

    $count = 5;
    $done = 0;
    while( !$done )
    {
        $cTable = zUtil::getNthItemNew( $count, $html, "<table", "</table>" );
        #print "$count\n";
        #print "table: $cTable\n";
        if( $cTable =~ /^<table(.*)Observed on/gsi )
        {
            if( !( $1 =~ /<table/gsi ) )
            {
                $done = 1;
            }
        }
        $count++;
        if( $count > 7 )
        {
            $done = 1;
        }
    }
    #print "tble: $cTable\n";
    #exit;

    $cCond = "n/a";
    $cTemp = "n/a";
    $cPressure =  "n/a";
    $cVisibility = "n/a";
    $cHumidity = "n/a";
    $cWind = "n/a";
    $cHumidex = "n/a";
    
    # Row 3 is the current conditions description
    $row = zUtil::getNthItemNew( 3, $cTable, "<tr", "</tr>" );    

    if( $row =~ /.*?<strong>(.*?)<\/strong>/gsi )
    {
        #print "current conditions are: $1\n";
        $cCondition = $1;
    }
    #exit;

    # row 4 on are the various readings, they can change, so make this
    # flexible.
    $count = 4;
    $done = 0;
    $row = zUtil::getNthItemNew( $count, $cTable, "<tr", "</tr>" ); 
    while( !$done )
    {
        if( $row =~ /.*?Temp.*?<strong>(.*?)<\/strong>/gsi )
        {
            #print "temp: $1\n";
            $cTemp=$1;
            $cTemp=~ s/<br>/ /gsi;
            $cHumidex = $cTemp;
        }        
        if( $row =~ /.*?Pressure.*?<strong>(.*?)<\/strong>/gsi )
        {
            #print "pressure: $1\n";
            $cPressure=$1;
            $cPressure=~ s/<br>/ /gsi;
        }
        if( $row =~ /.*?Humidity.*?<strong>(.*?)<\/strong>/gsi )
        {
            #print "humidity: $1\n";
            $cHumidity=$1;
            $cHumidity=~ s/<br>/ /gsi;
        }
        if( $row =~ /.*?Visibility.*?<strong>(.*?)<\/strong>/gsi )
        {
            #print "visibility: $1\n";
            $cVisibility=$1;
            $cVisibility=~ s/<br>/ /gsi;
        }
        if( $row =~ /.*?wind.*?<strong>(.*?)<\/strong>/gsi )
        {
            #print "wind: $1\n";
            $cWind = $1;
            $cWind =~ s/<br>/ /gsi;
        }       
        if( $row =~ /.*?humidex.*?<strong>(.*?)<\/strong>/gsi )
        {
            #print "humidex: $1\n";
            $cHumidex = $1;
            $cHumidex =~ s/<br>/ /gsi;
        }  

        $count++;
        $row = zUtil::getNthItemNew( $count, $cTable, "<tr", "</tr>" );
        if( $row =~ /^$/ )
        {
            $done = 1;
        }
        #print "row: ->$row<-\n";
    }
    
    push @rValue, { 'cCond' => $cCondition,
                    'cTemp' => $cTemp, 
                    'cPressure' => $cPressure,
                    'cVisibility' => $cVisibility,
                    'cHumidity' => $cHumidity,
                    'cWind' => $cWind
                    };   

    return reverse @rValue;
}

return 1;

