# $Id: //ncom_2_4/stores/farm/lib/BN.pm#37 $

package BN;
use LWP::UserAgent;
use HTTP::Request::Common;
use lib "/ncom/stores/farm/lib";
use FarmAgent;
use strict;

sub link {
	my ($isbn) = shift;
	my ($link);
	# Hack to farm their Harry Potter DVD & VHS pids
	if ( $isbn eq '0790767341' ) {
		$link = 'http://video.barnesandnoble.com/search/product.asp?EAN=85392133123';
	} elsif ( $isbn eq '079076735X' ) {
		$link = 'http://video.barnesandnoble.com/search/product.asp?EAN=85392246724';
	} elsif ( $isbn eq '0790767325' ) {
		$link = 'http://video.barnesandnoble.com/search/product.asp?EAN=85392133130';
		# And farm the Lord of the Rings DVDs
	} elsif ( $isbn eq '0780638379' ) {
		$link = 'http://video.barnesandnoble.com/search/product.asp?EAN=794043554223';
	} elsif ( $isbn eq '0780637003' ) {
		$link = 'http://video.barnesandnoble.com/search/Product.asp?EAN=794043541537';
	} else { # And the normal link
		$link = 'http://shop.barnesandnoble.com/booksearch/isbninquiry.asp?isbn=' . $isbn;
	}
	return $link;
}

sub name {
	my ( $pretty ) = shift;
	return "Barnes & Noble" if ( $pretty eq "pretty" );
	return "Barnesandnoble.com";
}

sub pf_link {
	my ($isbn) = shift;
	my ($link) = 'http://www.pricefarmer.com/cgi-bin/link?site=bn&isbn=' . $isbn;
	return $link;
}

sub affiliate_link {
	my ($isbn) = shift;
	#	my ($link) = 'http://service.bfast.com/bfast/click?bfmid=2181&sourceid=38305570&bfpid=' . $isbn . '&bfmtype=book';
	my ( $link ) = &link( $isbn );
	return $link;
}

sub avail {
	my ($days) = shift;
	my $avail;

	if ($days == 3) {
		$avail = "2-3 days";
	} elsif ($days == 1) {
		$avail = "24 hours";
	} elsif ($days == 14) {
		$avail = "1-2 weeks";
	} elsif ($days == 800) {
		$avail = "Advance order";
	} elsif ($days == 900) {
		$avail = "N/A";
	} elsif ($days == 1000) {
		$avail = "N/A";
	} else {
		$avail = $days . " days";
	}
	return $avail;	
}

sub fetch {
	my ($isbn,$master_list_price,$timeout) = @_;
	my ($link,$page,$list_price,$our_price,$club_price,$days_to_ship,$avail,$cover,$rating);
	my ($our_discount,$club_discount);
	my ($res,$ua);

	# Set a default timeout of 30 secs
	$timeout = $timeout || 30;

	if ($isbn) {
		$link = &link($isbn);
		# getstore($link,'bn.html');
		# $page = get($link);

		# Get the page
		$ua = LWP::UserAgent->new;

		# Set the timeout
		$ua->timeout($timeout);

		# Set the user agent
		$ua->agent(FarmAgent::agent());

		$res = $ua->request (GET $link);

		# Test for success
		if ($res->is_success) {
			$page = $res->content;
		} elsif ( $res->content =~ /<title>Barnes\&nbsp\;\&amp\;\&nbsp\;Noble\.com\s\-\sErrors\sEncountered<\/title>/i ||
				  # When they can't find the ISBN they now just send you to
				  # their search page.
				  $res->content =~ m!<title>Barnes\s*(?:&nbsp;)*Noble.com\s*(?:&nbsp;)*-\s*(?:&nbsp;)*Advanced\s*(?:&nbsp;)*Book\s*(?:&nbsp;)*Search.*?</title>!i ) {
			return ('bn',$isbn,0,0,0,0,0,1000,'N');
		} else {
			return 0;
		}

		if ( $page =~ /(?:List|Retail)\s*(?:&nbsp;)*\s*Price:[^\$]+\$([0-9.,]+)/i ||
			# This is the old pattern test it just in case the above fails.
			 $page =~ /Retail\s+Price:\s*\$([0-9.,]+)/i ) {
			$list_price = $1;
			$list_price =~ s/\,//g;
		} else {
			$list_price = 0;
		}

		if ( $page =~ /Our\s*(?:&nbsp;)*\s*(?:New)?\s*(?:&nbsp;)*\s*Price:[^\$]+\$([0-9.,]+)/i ||
			# The older pattern before the template change.
			$page =~ /Our\s+(?:New\s+)?Price:\s*<font.*?>\s*\$([0-9.,]+)/i ) {
			$our_price = $1;
			$our_price =~ s/\,//g;
		} else {
			$our_price = 0;
		}
		
		if ( $page =~ m!Reader(?:'|&\#39;)s\s*Advantage(?:</a>)?\s*Price:[^\$]+\$([0-9.,]+)!i ||
			# Again, this was the old template.
			$page =~ /Readers'\s+Advantage\s+Price:\s*<font.*?>\s*\$([0-9.,]+)/i ) {
			$club_price = $1;
			$club_price =~ s/\,//g;
		} else {
			# We have to add 0.0001 so it will round up from 0.0050
			$club_price = sprintf ( "%.2f", ( $our_price * 0.95 ) + 0.0001 );
			# Going to 10% club discount for Christmas (through 24 Nov 02).
			# $club_price = sprintf ( "%.2f", ( $our_price * 0.90 ) + 0.0001 );
		}

		if ( $page =~ m/Average\s+Rating:.*?([0-9])stars.gif(.{200})?/s ) {
#			 $page =~ m/Average\s+Rating[\000-\377]*?(star.*?)\n/i ) {
			my ( $starstring ) = $2;
#			$rating = 0;
#			while ( $starstring =~ m/star_reviews\.gif(.*)/ ) {
#				$rating += 1;
#				$starstring = $1;
#			}
			$rating = $1;
			if ( $starstring =~ m/starhalf/i ) {
				$rating .= ".5";
			} else {
				$rating .= ".0";
			}
		} else {
			$rating = 0;
		}
	
		# They're getting devious with their availability html. We'll
		# just capture the next 60 characters and I doubt they can make up
		# enough clunky html to fill the space.
		if ( $page =~ /(?:(?:In.Stock:)|(?:Available:))(.{60})/i ) {
			 # Old method
			 # $page =~ /((In.Stock:)|(Available:))\s*(.*?)\</ ) {
			$avail = $1;
		} else {
			$avail = "";
		}

	 	# $page =~ /<title>Barnes\&nbsp\;\&amp\;\&nbsp\;Noble\.com\s\-\sErrors\sEncountered<\/title>/ ||
		#	 $page =~ m!this ISBN is not in our database.!i ) {
		if ($page =~ m!<title>Barnes\s*(?:&nbsp;)*Noble.com\s*(?:&nbsp;)*-\s*(?:&nbsp;)*Advanced\s*(?:&nbsp;)*Book\s*(?:&nbsp;)*Search.*?</title>!i ) {
			$days_to_ship = 1000;
		} elsif ($avail =~ /2-3 day/i) {
			$days_to_ship = 3;
		} elsif ($avail =~ /24 hour/i) {
			$days_to_ship = 1;
		} elsif ($avail =~ /1-2 week/i) {
			$days_to_ship = 14;
		} elsif ($page =~ /advance /) {	
			$days_to_ship = 800;
		} else {
			$days_to_ship = 900;
		}

		if ($page =~ /akamai\.net\/.*\/images\.barnesandnoble\.com\/images\//) {
			$cover = "Y";
		} else {
			$cover = "N";
		}
	
		$our_discount = 0;
		$club_discount = 0;

		# BAMM has a list for this item -- use it
		if (($master_list_price > 0) && ($our_price > 0)) {
			$our_discount = int(.5 + (100 * (1 - ($our_price / $master_list_price))));
		} elsif (($list_price > 0) && ($our_price > 0)) {
			$our_discount = int(.5 + (100 * (1 - ($our_price / $list_price))));
		}

		if (($master_list_price > 0) && ($club_price > 0)) {
			$club_discount = int(.5 + (100 * (1 - ($club_price / $master_list_price))));
		} elsif (($list_price > 0) && ($club_price > 0)) {
			$club_discount = int(.5 + (100 * (1 - ($club_price / $list_price))));
		}
	}

	return ('bn',$isbn,$list_price,$our_price,$club_price,$our_discount,$club_discount,$days_to_ship,$cover,$rating);		
}

1;
