#!/usr/bin/perl -w
###########################################
# Mike Schilli, 2006 (m@perlmeister.com)
###########################################
use strict;
use Videodir;
use Curses::UI::POE;
use Curses;

my $MPLAYER = "/usr/bin/mplayer";
my $V = Videodir->new();

my $CUI = Curses::UI::POE->new(
  -color_support => 1, 
  inline_states  => {
    _start => sub {
        $poe_kernel->delay('wake_up', 60);
    },
    wake_up => \&wake_up_handler,
});

my $WIN = $CUI->add(qw( win_id Window ));

my $TOP = $WIN->add(qw( top Label 
  -y 0 -width -1 -paddingspaces 1 
  -fg white -bg blue
  ), -text => top_text());

my $LBOX = $WIN->add(qw( lb Listbox
  -padtop 1 -padbottom 1 -border 1 ),
  -onchange    => \&selected,
  -onselchange => \&changed, 
);

my $BOTTOM = $WIN->add(qw( bottom Label
  -y -1 -width -1 -paddingspaces 1 
  -fg white -bg blue
  ), -text => bottom_text(),
);

$CUI->set_binding(sub { selected($LBOX) 
                      }, KEY_ENTER());
$CUI->set_binding(sub { exit 0; }, "q");
$CUI->set_binding(\&delete_confirm, "d");
$CUI->set_binding(\&keep, "*");

redraw(); # draw inital listbox content
$CUI->mainloop;

###########################################
sub ttl_icon {
###########################################
  my($ttl) = @_;
  return $ttl <  0 ? "!" : 
         $ttl <= 5 ? " " : "*" ;
}

###########################################
sub changed {
###########################################
    $BOTTOM->text(bottom_text());
}

###########################################
sub selected {
###########################################
  my $cmd = "$MPLAYER " . 
            active_item()->{path} . 
            ">/dev/null 2>&1";
  `$cmd &`;
}

###########################################
sub bottom_text {
###########################################
  my $item = active_item();

    # Work around PGdown bug
  return unless defined $item;

  my $str = sprintf "%d/%d | %.1f days" .
    " old | %s GB | TTL %s", 
    $LBOX->get_active_id() + 1,
    scalar @{$V->{items}},
    $item->{age}, $item->{size},
    $item->{ttl};

  return $str;
}

###########################################
sub wake_up_handler {
###########################################
    $V->rescan(); # Get newly added files
    redraw();

    redraw() if $V->shrink();
        # Re-enable timer
    $poe_kernel->delay('wake_up', 60);
}

###########################################
sub top_text {
###########################################
    return "tv1.0 | " . $V->{total_size}
     . " GB total | $V->{max_gigs} GB max";
}

###########################################
sub delete_confirm {
###########################################
  my $item = active_item();

  my $yes = $CUI->dialog(
    -title     => "Confirmation required",
    -buttons   => ['yes', 'no'],
    -message => "Are you sure you want " .
             "to delete $item->{file}?",
    qw( -tbg white -tfg red -bg white
        -fg red -bbg white -bfg red ));

  if($yes) {
    $V->remove($item->{file});
    redraw();
  }
}

###########################################
sub redraw {
###########################################
  $LBOX->{-values} = 
    [ map { $_->{file} } @{$V->{items}} ];

  $LBOX->{-labels} = { 
    map { $_->{file} => 
      ttl_icon($_->{ttl}) . " $_->{file}"
    } @{$V->{items}}
  };

  $LBOX->draw(1);
  $TOP->text(top_text());
  $BOTTOM->text(bottom_text());
}

###########################################
sub keep {
###########################################
  my $it = active_item();
  $V->{meta}->{$it->{file}}->{keep} = 1000;
  $V->meta_save();
  $V->rescan();
  redraw();
}

###########################################
sub active_item {
###########################################
  return $V->{items}->[ 
      $LBOX->get_active_id() ];
}
