use zUtil;

sub marketsInsiderFinanceHandler
{
    $html = shift;
    $url = shift;
    $symbol = shift;
    $displaysymbol = shift;
    my( @rValue ) = ();
    my( $table, $infoRow, $value, $change, $row );
    my( $done, $date, $link, $headline, $source );
    my( %results, $results );
    my( $headlineIndex, $stockIndex );
    my( $open, $current, $change, $html );

    # http://www.jarloo.com/real-time-google-stock-api/
    # look up symbols here, i.e. for nasdaq  https://www.google.com/finance?q=NASDAQ

    print "------------------ marketsFinanceHandler: $url ------- $symbol -------------\n";

    $text_dump = `w3m -dump '$url' | grep -A 3 "$symbol"`;
    #$text_dump = `w3m -dump '$url'`;
    print( "text dump: $text_dump\n" );

    #exit 1;

    # if( $text_dump =~ /^([0-9]*?\.[0-9]*?)\s*?(.*)?\s*?(.*)?$/ ) {
    $quote_found = 0;
    
    my @lines = split /\n/, $text_dump;
    foreach my $line (@lines) {
        print( "line: $line\n" );
        if( $line =~ /^([0-9].*)?\s+?(.*)?\s+(.*)?$/ && !$quote_found ) {
            print( "quote! a: --$1-- b: --$2-- c: --$3--\n" );
            $quote_found = 1;
            $quote = $1;
            $change = "$2 $3";
        }
    }

    #exit 1;

    # if ( $quote_found == 0 ) {
    #     my @lines = split /\n/, $text_dump_two;
    #     foreach my $line (@lines) {
    #         print( "line from two: $line\n" );
    #         if( $line =~ /^([0-9].*)?\s+?(.*)?\s+(.*)?$/ ) {
    #             print( "quote! a: --$1-- b: --$2-- c: --$3--\n" );
    #             $quote_found = 1;
    #             $quote = $1;
    #             $change = "$2 $3";
    #         }
    #         if( $line =~ /^\$([0-9].*)$/ ) {
    #             print( "=====================================\n" );
    #             print( "quote type 2: a: --$1-- b: --$2-- c: --$3--\n" );
    #             print( "=====================================\n" );
    #             $quote_found = 1;
    #             $quote = $1;
    #             $change = "$2 $3";
    #         }
    #         if( $line =~ /^(\(.*?[0-9].*?%\))(.*)$/ ) {
    #             print( "=====================================\n" );
    #             print( "quote type 2a: a: --$1-- b: --$2-- c: --$3--\n" );
    #             print( "=====================================\n" );
    #             $quote_found = 1;
    #             if (not $quote eq "" ) {
    #                 print( "   quote already present, grabbing change\n" );
    #                 $change = "$1 $2";
    #             }
    #         }
    #     }
    #     #if( $line =~ /^\$([0-9].*)$/ ) {
    #     #}
    # }

    #exit 1;

    if ( $quote_found == 0 ) {
        $change = "n/a";
        $quote = "n/a";
        $quote_found = 1;
    }
    
    if( $quote_found ) {
        print( "quote was found\n" );
        push @rValue,
        {
            'change' => $change,
            'last' => "$quote"
        };

        #exit 1;
        return @rValue;
    }

    return @rValue;

    exit 1;

}

return 1;

