use zUtil;

sub googleFinanceHandler
{
    $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 "------------------ googleFinanceHandler: $url --------------------\n";

    $text_dump = `w3m -dump $url | grep -A 3 "/ Stock Price"`;
    $text_dump_two = `w3m -dump $url | grep -A 3 "$symbol"`;
    if ( $text_dump_two eq "" ) {
        $text_dump_two  = `w3m -dump $url | grep -A 3 "^$displaysymbol"`;
    }
    print( "text dump: $text_dump\n" );
    print( "text dump2: $text_dump_two\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+(.*)?$/ ) {
            print( "quote! a: --$1-- b: --$2-- c: --$3--\n" );
            $quote_found = 1;
            $quote = $1;
            $change = "$2 $3";
        }
    }

    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;

    $html=`/usr/local/zeddweb/scripts/cfscrape_fetch.py $url`;
    if( ! ($html =~ /("entities".*?\\n"])/) ) {
        print "no entities. re-fetching\n";
        sleep 2;
        $html=`/usr/local/zeddweb/scripts/cfscrape_fetch.py $url`;
        if( ! ($html =~ /("entities".*?\\n"])/) ) {
            print "no entities. re-fetching (again)\n";
            sleep 2;
            $html=`/usr/local/zeddweb/scripts/cfscrape_fetch.py $url`;
        }
    }
    print "$html\n";

    $count = 1;
    $done = 0;
    $table = zUtil::getNthItemNew( $count, $html, "<table", "</table>" );
    while( !$done ) {
        if( $table =~ /^$/ ) {
            $done = 1;
        }
        if ( $table =~ /Open/ ) {
            print "open found!\n";
            $data = $table;
            $done = 1;
        }

        print "[$count]: $table\n\n";
        $count++;
        if( $count > 20 ) {
            $done = 1;
        }

        $table = zUtil::getNthItemNew( $count, $html, "<table", "</table>" );        
    }

    print "====================================================\n";
    print "data: $data\n";
    print "====================================================\n";
    #exit 1;
    
    if( ! $data =~ /^$/ ) {
        $data =~ s/<div>//gsi;
        $data =~ s/<\/div>//gsi;
        print "data: $data\n";
        $open = 0;
        if( $data =~ /<td.*?>Open<\/td><td.*?>(.*?)<\/td>/ ) {
            $open = $1;
            $open =~ s/<.*>//gsi;
            $open =~ s/,//gsi;
            print "open: $open\n";
        }

#        exit 1;
        $current = 0;
        if( $html =~ /("entities".*?\\n"])/ ) {
            $entities = $1;
            print "entities!!! $entities\n";
            if( $entities =~ /$symbol\\",\\"(.*?)\\"/ ) {
                print "current price: $1\n";
                $current = $1;
                $current =~ s/,//gsi;

                if( $open =~ /-/ ) {
                    $open = $current;
                    print "adjusted open: $open\n";
                }
            }
        } else {
            print "couldn't find the entities: $html\n";
        }

        if( $open != 0 && $current != 0 ) {
            $change_value = $current - $open;

            if( $change_value >= 0) {
                $change_percent = sprintf("%.2f", (($current - $open)/$current)*100);
                $change = sprintf("+%.2f (%s\%)", $change_value, $change_percent);
            } else {
                $change_percent = sprintf("%.2f", (($open - $current)/$current)*100);
                $change = sprintf("%.2f (%s\%)", $change_value, $change_percent);
            }

            print "change: $change\n";

            push @rValue,
            {
                'change' => $change,
                'last' => $current
            };
        } else {
            print "no open no change!";
            #exit 1;
            push @rValue,
            {
                'change' => $open,
                'last' => "n/a"
            };
        }
    } else {
        push @rValue,
        {
            'change' => "n/a",
            'last' => "n/a"
        };
    }

    #exit;
    return @rValue;
}

return 1;

