package Finance::Quote::PrudentialHK;
require 5.004;

use strict;

use vars qw($VERSION $PRUDENTIALHK_URL $PRUDENTIALHK_URL_SUFFIX);

use LWP::UserAgent;
use HTTP::Request::Common;

$VERSION = '1.18';
$PRUDENTIALHK_URL = 'http://www.prudential.com.hk/PruServlet?module=fund&purpose=searchHistFund&fundCd=';
$PRUDENTIALHK_URL_SUFFIX = '_U';

sub methods { return (prudentialhk => \&prudentialhk); }

{
  my @labels = qw/date year month day method source name currency price/;

  sub labels { return (prudentialhk => \@labels); }
}

sub prudentialhk {
  my $quoter  = shift;
  my @symbols = @_;

  return unless @symbols;
  my ($ua, $reply, $url, %funds, $name);
  my (@decoded_html, $decoded_html, $price_date, $currency, $price, $date);

  foreach my $symbol (@symbols) {
    $name = $symbol;
    $url = $PRUDENTIALHK_URL;
    $url = $url . $name . $PRUDENTIALHK_URL_SUFFIX;
    $ua    = $quoter->user_agent;
    $reply = $ua->request(GET $url);
    unless ($reply->is_success) {
	  foreach my $symbol (@symbols) {
        $funds{$symbol, "success"}  = 0;
        $funds{$symbol, "errormsg"} = "HTTP failure";
	  }
	  return wantarray ? %funds : \%funds;
    }

    #my @decoded_html = split(/\n+/,Encode::decode_utf8($reply->content));
    my @decoded_html = split(/\n+/,$reply->content);
    my $decoded_html;
    
    while ($decoded_html = shift @decoded_html){
	    ($decoded_html =~ /fundPriceCell/) && last;
    }

    #example: <td align="center" class="fundPriceCell1">26/04/2013</td><td align="center" class="fundPriceCell1">17.4430</td><td align="center" class="fundPriceCell1">17.4430</td>
    $price_date = $decoded_html;
                      
    $price_date =~ s/\<td.*?\>//g;
    ($date, $price) = split(/\<\/td.*?\>/, $price_date);

    #all products in Prudential HK are currently in USD only
    $currency = "USD";

    $funds{$name, 'method'}   = 'prudentialhk';
    $funds{$name, 'price'}    = $price;
    $funds{$name, 'currency'} = $currency;
    $funds{$name, 'success'}  = 1;
    $funds{$name, 'symbol'}  = $name;
    $funds{$name, 'source'}   = 'Finance::Quote::PrudentialHK';
    $funds{$name, 'name'}   = $name;
    $funds{$name, 'p_change'} = "";  # p_change is not retrieved (yet?)

    $date =~ /(\d\d?)\/(\d\d?)\/(\d\d\d?\d?)/;
    $quoter->store_date(\%funds, $name, {year => $3, month => $2, day => $1});
    

    # Check for undefined symbols
    foreach my $symbol (@symbols) {
	  unless ($funds{$symbol, 'success'}) {
        $funds{$symbol, "success"}  = 0;
        $funds{$symbol, "errormsg"} = "Fund name not found";
	  }
    }
  }
  return %funds if wantarray;
  return \%funds;
}

1;


=head1 NAME

Finance::Quote::PrudentialHK - Obtain fund prices from Prudential HK

=head1 SYNOPSIS

    use Finance::Quote;

    $q = Finance::Quote->new;

    %fundinfo = $q->fetch("prudentialhk","Fund code");

=head1 DESCRIPTION

This module obtains information about Prudentail HK fund prices from
http://www.prudential.com.hk/PruServlet?module=fund&purpose=searchHistFund&fundCd=XXX_U
where XXX is the fund code (GGF, FEF, etc)

=head1 FUND CODE

You can get the Fund Code from the end of URL of the fund in Prudential page.

=head1 LABELS RETURNED

Information available may include the following labels:
date method source name currency price

=head1 SEE ALSO

www.prudential.com.hk

=head1 KNOWN BUGS

Doesn't work with Money USD Fund.

=cut