# $Id: //ncom_2_4/stores/farm/lib/FAT.pm#29 $

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

sub link {
	my ($isbn) = shift;
	my ($link) = "http://www1.fatbrain.com/asp/bookinfo/bookinfo.asp?theisbn=" . $isbn;
	return $link;
}

sub name {
	return "Fatbrain.com";
}

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

sub affiliate_link {
	my ($isbn) = shift;
	my ($link) = 'http://www1.fatbrain.com/asp/bookinfo/bookinfo.asp?theisbn=' . $isbn;
	#$link .= '&from=ZHQ142';			# marching.com affiliation 
	$link .= '&from=GXG832';			# pricefarmer.com affiliation
	return $link;
}

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

	if ($days == 3) {
		$avail = "2-3 days";
	} elsif ($days == 4) {
		$avail = "3-4 days";
	} elsif ($days == 1) {
		$avail = "24 hours";
	} elsif ($days == 14) {
		$avail = "1-2 weeks";
	} elsif ($days == 21) {
		$avail = "2-3 weeks";
	} elsif ($days == 35) {
		$avail = "3-5 weeks";
	} elsif ($days == 42) {
		$avail = "5-6 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,$img_path);
	my ($res,$ua);

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

	if ($isbn) {

		$link = &link($isbn);
		# getstore($link,'fat.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\>Fatbrain\.com\s\-\sItem\snot\sfound\!\<\/title\>/ ) {
			return ('fat',$isbn,0,0,0,0,0,1000,'N');
		} else {
			return 0;
		}

		if ( $page =~ /List Price:\s+\$([0-9.,]+)/i ) {
			$list_price = $1;
			$list_price =~ s/\,//g;
		} else {
			$list_price = 0;
		}

		if ( $page =~ m#O(nline|riginal)\s+Price:\s*</B><B>(<FONT.*?>)?\$([0-9.,]+)#i ) {
			$our_price = $3;
			$our_price =~ s/\,//g;
			$club_price = $our_price;
		} else {
			$our_price = $club_price = 0;
		}

		$rating = 0;
		
		if ( $page =~ /((((In(\s|\-)Stock)|Available)\:*?)|(Usually ships in))\s+(.*?)\<\//i ) {
			$avail = $7;
		} else {
			$avail = "";
		}

		if ( $page =~ /\<title\>Fatbrain\.com\s\-\sItem\snot\sfound\!\<\/title\>/i ) {
			$days_to_ship = 1000;
		} elsif ($avail =~ /24 hour/i) {
			$days_to_ship = 1;
		} elsif ($avail =~ /2\-3.*?day/i) {
			$days_to_ship = 3;
		} elsif ($avail =~ /1\-2 week/i) {
			$days_to_ship = 14;
		} elsif ($avail =~ /3\-4.*?day/i) {
			$days_to_ship = 4;
		} elsif ($avail =~ /2\-3 week/i) {
			$days_to_ship = 21;
		} elsif ($avail =~ /5\-6 week/i) {
			$days_to_ship = 42;
		} elsif ($page =~ /Not yet/i) {
			$days_to_ship = 800;
		} elsif ($avail =~ /same day/i) {				# All of these are matched against in case
			$days_to_ship = 1;							# Fatbrain ever goes back to the old way
		} elsif ($avail =~ /2 to 3.*?day/i) {			# of stock quoting.
			$days_to_ship = 3;							#
		} elsif ($avail =~ /3 to 4.*?day/i) {			#
			$days_to_ship = 4;							#
		} elsif ($avail =~ /7 day/i) {					#
			$days_to_ship = 7;							#
		} elsif ($avail =~ /2 to 3 week/i) {			#
			$days_to_ship = 21;							#
		} elsif ($avail =~ /5 to 6 week/i) {			#
			$days_to_ship = 42;							#####
		} else {
			$days_to_ship = 900;
		}
# Old-style (pre-BN.com-integration) images
#images.fatbrain.com/08/702/128/0870212850
#
#		$isbn =~ /^([0-9]{2})([0-9]{3})([0-9]{3})/;
#		$img_path = "images.fatbrain.com/" . $1 . "/" . $2 . "/" . $3;
#
#		if ($page =~ /$img_path/) {

		if ( $page =~ m#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))));
		}

		$club_discount = $our_discount;

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

1;
