use zUtil;

sub yahooStocksHandler
{
    $html = shift;
    $type = shift;
    my( @rValue ) = ();
    my( $table, $infoRow, $value, $change, $row );
    my( $done, $date, $link, $headline, $source );
    my( %results, $results );
    my( $headlineIndex, $stockIndex );

    print "------------------ YahooStocksHandler [$type] --------------------\n";
    print "$html\n";

    if( $type =~ /headlines/ )
    {
        # Here is the deal. The headlines normally start at table 11, but sometimes special
        # news appears in table 11 and the headlines follow in table 12. So we are going to
        # start at table 11, check to see if the headline regex (this is the same in our 'if'
        # and in the loop pulling out the headlines, so keep them in sync) is there and if
        # it is there, move onto the headline parsing. If the regex isn't there, move to 
        # the next table and try again. We'll do this until about 14 and then give up !!!

        print "headline handler\n";

        $headlineIndex = 14;
        $done = 0;

        while( !$done )
        {
            $table = zUtil::getNthItemNew( $headlineIndex, $html, "<table", "</table>" );

            if( $table =~ /^<table.*?\"yfncnhl\">/si )
            {
                $done = 1;
            }
            else
            {
                $headlineIndex++;

                if( $headlineIndex > 18 )
                {
                    $done = 1;
                }
            }
        }       

        print "headline table: $table\n";
        #exit;

        $headlineIndex = 1;
        $row = zUtil::getNthItemNew( $headlineIndex, $table, "<tr", "</tr>" );
        while( !($row =~ /^$/) )
        {
            #print "row: $row\n";
            if( $row =~ /<tr.*?valign="top"><td.*?class="yfncnhlbl">.*?<\/td><td><a href=\"(.*?)\">(.*?)<\/a>.*?<br><small><b>(.*?)<\/b>(.*?)<\/small><\/td><\/tr>/gsi )
            {
                $link=$1;
                $headline=$2;
                $source=$3;
                $date=$4;
                $headline=~ s/\s*$//gsi;
                $date =~ s/&nbsp;//gsi;
                $date =~ s/\s*$//gsi;
                $date =~ s/\(//gsi;
                $date =~ s/\)//gsi;

                $keeper = "";
                $total_count = 0;
                $limit=10;
                my @words = split( / / ,$headline); #split into words
                foreach my $word (@words)
                {
                    if( $total_count < $limit )
                    {
                        $total_count++;
                        $keeper .= "$word ";
                    }
                }
                if( $total_count >= $limit )
                {
                    $keeper .= "<b>...</b>";
                }
                $headline = $keeper;


                print "--- match! $date ----\n";
                push @rValue,
                {
                    'day' => "",
                    'month' => "",
                    'date' => $date,
                    'symbol' => "",
                    'link' => $link,
                    'headline' => $headline,
                    'source' => $source
                 };		  
            }
            $headlineIndex++;
            $row = zUtil::getNthItemNew( $headlineIndex, $table, "<tr", "</tr>" );       
        }
    }
    else
    {
        # not headlines ... just the quote

        print "quote handler\n";
        $change = "n/a";
        $last = "n/a";
        if( $html =~ /<div>.*?(<span class="time_rtq_ticker">.*?)<\/div>/gsi )
        {
            print "Found what should be a stock quote!\n";
            print "$1\n\n\n";

            $stock_span=$1;

            $value = zUtil::getNthItemNew( 2, $stock_span, "<span", "</span>" );
            $value =~ s/\<.*?\>//gsi;
            
            $up_down = zUtil::getNthItemNew( 5, $stock_span, "<span", "</span>" );
            $up_down =~ s/\<.*?\>//gsi;

            $up_down_dollar = zUtil::getNthItemNew( 4, $stock_span, "<span", "</span>" );
            $up_down_dollar =~ s/\<.*?\>//gsi;
            $up_down_dollar =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/;
            
            print "price: $value\n";
            print "up_down: $up_down\n";
            print "up_down_dollar: $up_down_dollar\n";

            # if the up down is <4:00pm EDT or EST>, then we are pre-market
            if ( $up_down_dollar =~ /PM/ )
            {
                if( $html =~ /<div>.*?(<span class="yfs_rtq_quote">.*?)<\/div>/gsi )
                {
                    $stock_span_pre = $1;

                    print "stock html: $stock_span_pre\n";

                    $value = zUtil::getNthItemNew( 2, $stock_span_pre, "<span", "</span>" );
                    $value =~ s/\<.*?\>//gsi;
                    
                    $up_down = zUtil::getNthItemNew( 5, $stock_span_pre, "<span", "</span>" );
                    $up_down =~ s/\<.*?\>//gsi;

                    $up_down_dollar = zUtil::getNthItemNew( 4, $stock_span_pre, "<span", "</span>" );
                    $up_down_dollar =~ s/\<.*?\>//gsi;
                    $up_down_dollar =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/;

                    print "price: $value\n";
                    print "up_down: $up_down\n";
                    print "up_down_dollar: $up_down_dollar\n";

                    # for the up down colour below ...
                    $stock_span = $stock_span_pre;
                }
            }

            $change = "$up_down_dollar $up_down";
            if( $stock_span =~ /Down/gsi )
            {
                $change = "-" . $change;
            }
            else
            {
                $change = "+" . $change;
            }
        
            print "Change: $change\n\n";
        }
        
        push @rValue,
        {
            'change' => $change,
            'last' => $value
        };
        undef( $change );
        undef( $value );
    }

    return @rValue;
}

return 1;

