package BookPub::RDAS::Parser::ProductPrice::Amazon;

use lib '/app/tools/bookpub/lib';
use base 'BookPub::RDAS::Parser::ProductPrice';

sub _fieldConfig {
    {
        ProductPrice => {
            tag        => 'div',
            id         => 'priceBlock',
            class      => 'buying',
            constraint => 'and',
            container  => 'BookPub::RDAS::Parser::ProductPrice::Amazon::Price',

            filter => sub {
                my $res = shift;
                $res =~ s/[\$\s]//g;
                return $res;
            },
        },

        Title => {
            tag => 'span',
            id  => 'btAsinTitle',

            filter => sub {
                my $res = shift;
                $res =~ s/\[\w+\]//g;
                $res =~ s/\[Kindle Edition]//g;
                return $res;
            },
        },

        Author => {
            tag       => 'body',
            container => 'BookPub::RDAS::Parser::ProductPrice::Amazon::Author',
        },

        ASIN => {
            parent  => 'ProductDetail',
            tag     => 'li',
            content => '.*ASIN:.*',

            filter => sub {
                my $res = shift;
                $res =~ s/ASIN:\s+//;
                return $res;
            }
        },

        Format => {
            parent  => 'ProductDetail',
            tag     => 'li',
            content => '.*Format:.*',

            filter => sub {
                my $res = shift;
                $res =~ s/Format:\s+//;
                return $res;
            }
        },

        ISBN10 => {
            tag     => 'li',
            content => '.*ISBN-10:.*',

            filter => sub {
                my $res = shift;
                $res =~ s/ISBN-10:\s+//;
                return $res;
            }
        },

        ISBN13 => {
            tag     => 'li',
            content => '.*ISBN-13:.*',

            filter => sub {
                my $res = shift;
                $res =~ s/ISBN-13:\s+//;
                return $res;
            }
        },

        Publisher => {
            tag     => 'li',
            content => '.*Publisher:.*',

            filter => sub {
                my $res = shift;
                $res =~ s/Publisher:\s+//;
                return $res;
            }
        },

        CoverURL => {
            tag       => 'img',
            id        => 'prodImage',
            container => 'RDAS::Parser::Item::ImageSource'
        },

        Available => {
            tag   => 'span',
            class => 'availGreen'
        },

        UNAvailable => {
            tag   => 'span',
            class => 'availRed'
        },

        PreOrder => {
            tag     => 'span',
            class   => 'availOrange',
            content => '.*pre.*'
        },

        PriceBlock => {
            tag        => 'div',
            id         => 'priceBlock',
            class      => 'buying',
            constraint => 'and',
        },

        PreOrderEBook => {
            tag   => 'div',
            class => 'kicsBoxContents',

            container => 'RDAS::Parser::Item::InputSource',
        },

        PriceType => {
            tag     => 'i',
            content => '.*This price was set by the publisher*',

            filter => sub {
                return 'agency';
            }
        },

        Rank => {
            tag     => 'li',
            content => '.*Amazon Bestsellers Rank:.*',

            filter => sub {
                my $res = shift;
                $res =~ s/,//g;
                $res =~ /Amazon Bestsellers Rank:\s+#(\d+)/;
                return $1;
            },

        }
    };
}

sub available {
    my $self = shift;

    # Return cached results if they exist
    if ( defined( $self->{_available} ) ) {
        return $self->{_available};
    }

    # Digital preorder
    my $preorder = $self->PreOrderEBook();
    return -1 if ( $preorder && $preorder =~ /preorder/ );

    # Physical preorder
    return -1 if ( $self->PreOrder );

    my $available   = $self->Available();
    my $unavailable = $self->UNAvailable();

    my $result = 0;

    # First let's try to use the available field.
    if ($available) {
        $result = 1 if ( $available =~ /in stock/i );
    }

    # Next check for the unavailable field
    elsif ($unavailable) {
        $result = 0;
    }

    # If we don't have either (eBooks usually don't) then we need to do something else.
    # So we can look for the price block label
    else {
        my $label = $self->PriceBlock;

        $result = 1 if ( $label && $label =~ /(kindle|list) price/i );
    }

    $self->{_available} = $result;
    return $self->{_available} ? 1 : undef;
}

1;
