use strict;
use warnings;

use Liberl::Provider;
use Business::ISBN;

=head1 NAME

Liberl::Action::SearchISBN

=cut

package Liberl::Action::SearchISBN;
use base qw/Liberl::Action Jifty::Action/;

use Jifty::Param::Schema;
use Jifty::Action schema {
    param isbn =>
        label is _('ISBN'),
        is mandatory,
        ;
    param library =>
        label is _('Library'),
        render as 'select',
        available are defer {
            my $libraries = Liberl::Model::LibraryCollection->new;
            $libraries->unlimit;
            [{
                display_from => 'name',
                value_from   => 'name',
                collection   => $libraries,
             }];
    };
};

=head2 validate_isbn

=cut

sub validate_isbn {
    my $self = shift;
    my $isbn = shift;

    my $number = Business::ISBN->new($isbn);
    
    if (defined($number)) {
        if ($number->is_valid) {
            return $self->validation_ok('isbn');
        }
    } 
    return $self->validation_error(isbn => _("Not a valid ISBN"));
}

=head2 canonicalize_isbn

=cut

sub canonicalize_isbn {
    my $self = shift;
    my $isbn = shift;

    my $number = Business::ISBN->new($isbn);
    if (defined($number)) {
        if ($number->is_valid) {
            return $number->as_isbn13->as_string([]);
        }
    }
    return $isbn;
}

=head2 take_action

=cut

sub take_action {
    my $self = shift;
    my $isbn = $self->argument_value('isbn');
    my $library = $self->argument_value('library');

    my $provider = Liberl::Provider->new('Amazon',
        token => '142TF8CHT48WYPPS6J82',
        locale => 'fr',
        );
    my $resp = $provider->search($isbn);

    if($resp->is_success()) {
        for my $prop ($resp->properties) {
            $self->result->content(title            => $prop->title());
            $self->result->content(authors          => join("\n", $prop->authors()));
            $self->result->content(publisher        => $prop->publisher());
            $self->result->content(year             => $prop->year());
            $self->result->content(isbn             => $prop->isbn());
            $self->result->content(edition          => $prop->edition());
            $self->result->content(ean              => $prop->ean());
            $self->result->content(numpages         => $prop->numpages());
            $self->result->content(publication_date => $prop->publication_date());
            $self->result->content(ImageUrlSmall    => $prop->ImageUrlSmall());
            $self->result->content(ImageUrlMedium   => $prop->ImageUrlMedium());
            $self->result->content(ImageUrlLarge    => $prop->ImageUrlLarge());
            $self->result->content(library          => $library);
        }
        $self->report_success;
    }
    else {
        $self->report_error;
    }
    
    return 1;
}

=head2 report_success

=cut

sub report_success {
    my $self = shift;
    # Your success message here
    $self->result->message(_('Success'));
}

=head2 report_error

=cut

sub report_error {
    my $self = shift;
    $self->result->error('Book not found');;
}

1;

