#!/usr/local/bin/perl -w
use strict;
use Tk;
use Tk::JPEG;
use POE;
use LWP::UserAgent::POE;
use Net::Amazon;
use Net::Amazon::Request::UPC;
use MIME::Base64;
use Rose::DB::Object::Loader;
use Log::Log4perl qw(:easy);
 
my @MODES = qw(books music dvd);
 
my $UA = LWP::UserAgent::POE->new();
 
my $loader = Rose::DB::Object::Loader->new(
  db_dsn => 
    "dbi:SQLite:dbname=articles.dat",
  db_options   => {
    AutoCommit => 1, RaiseError => 1 },
);
$loader->make_classes();
 
my $top = $poe_main_window;
$top->configure(-title     => "UPC Reader", -background=> "#a2b2a3");
$top->geometry("200x300");
 
my $FOOTER = $top->Label();
$FOOTER->configure(-text => "Escanear siguiente artículo");
 
my $BYWHO = $top->Label();
my $UPC   = $top->Label();
my $PHOTO = $top->Photo(-format => 'jpeg');
my $photolabel = $top->Label(-image => $PHOTO);
my $entry = $top->Entry(-textvariable => \my $UPC_VAR);
 
my $PRODUCT = $top->Label();
 
$entry->focus();
 
for my $w ($entry, $photolabel, $PRODUCT, 
           $BYWHO, $UPC, $FOOTER) {
  $w->pack(-side => 'top', -expand => 1, -fill => "x" );
}
 
$entry->bind("<Return>", \&scan_done);
 
my $session = POE::Session->create(
  inline_states => { 
    _start => sub{
      $poe_kernel->delay("_start", 60);
  } 
});
 
POE::Kernel->run();
 
###########################################
sub scan_done {
###########################################
  $PHOTO->blank();
  $PRODUCT->configure(-text => "");
  $FOOTER->configure(-text => "Procesando ...");
  $BYWHO->configure(-text => "");
  $UPC->configure(-text => $UPC_VAR);
  resp_process(
          amzn_fetch( $UPC_VAR ) );
  $UPC_VAR = "";
}
 
###########################################
sub amzn_fetch {
###########################################
  my($upc) = @_;
 
  my $resp;
 
  my $amzn = Net::Amazon->new(
      token => 'XXXXXXXXXXXXXXXXXXXX',
       ua    => $UA,
  );
 
  for my $mode (@MODES) {
 
    my $req = 
      Net::Amazon::Request::UPC->new(
          upc  => $upc,
          mode => $mode,
      );
 
     $resp = $amzn->request($req);
 
     if($resp->is_success()) {
         return($resp, $mode, $upc);
         last;
     }
 
     WARN "Nothing found in mode '$mode'";
  }
  return $resp;
}
 
###########################################
sub resp_process {
###########################################
  my($resp, $mode, $upc) = @_;

  if($resp->is_error()) {
    $PRODUCT->configure(-text => "NOT FOUND");
    return 0;
  }
 
  my ($property) = $resp->properties();
  my $imgurl = $property->ImageUrlMedium();
  img_display( $imgurl );
 
  my $a = Article->new();
  $a->upc($upc);
  $a->type($mode);
  $a->title( $property->Title() );
 
  if($mode eq "books") {
    $a->bywho( $property->author() );
  } elsif( $mode eq "music") {
    $a->bywho( $property->artist() );
  } else {
    $a->bywho( "" );
  }
 
  $BYWHO->configure(-text => $a->bywho() );
  $PRODUCT->configure(-text => $a->title() );
 
  if($a->load( speculative => 1 )) {
      $PRODUCT->configure(
               -text => "YA EXISTE");
  } else {
    $a->save();
  }
 
  $FOOTER->configure(-text => "Escanear siguiente artículo");
  return 1;
}
 
###########################################
sub img_display {
###########################################
  my($imgurl) = @_;
   
  my $imgresp = $UA->get( $imgurl );
 
  if($imgresp->is_success()) {
    $PHOTO->configure( -data => encode_base64( $imgresp->content() ));
  }
}
