package Xchat::VPIT::MPD;

use strict;
use warnings;

use Audio::MPD;

use Xchat qw<:all>;

use lib get_info 'xchatdir'; 
use Xchat::XPI;

our $VERSION = 0.04;

my $ph;

hook_command $_, sub {
 my $mpd = Audio::MPD->new;
 unless ($mpd) {
  print $ph "Couldn't connect to the MPD server\n";
  return EAT_ALL;
 }

 my $status = $mpd->status;
 unless ($status) {
  print $ph "MPD doesn't seem to be running\n";
  return EAT_ALL;
 }
 if ($status->state eq 'stop') {
  print $ph "MPD is stopped\n";
  return EAT_ALL;
 }

 my $song = $mpd->current;
 my $title;
 if ($title = $song->title) {
  $title = $_ . ' - ' . $title for grep defined, $song->album, $song->artist;
 } else {
  $title = $song->file;
  $title =~ s!\.\w+$!!;
  $title = $1 if $title =~ m!/([^/]*)$!;
 }

 my $time = $status->time;
 my $duration = sprintf '[%d:%02d/%d:%02d]',
                        $time->sofar_mins, $time->sofar_secs,
                        $time->total_mins, $time->total_secs;
 command 'ACTION np: ' . $title . ' ' . $duration;

 return EAT_ALL;
}, {
 help_text => "$_, output which song is currently played by mpd"
} for qw<MPD NP>;

$ph = Xchat::XPI->new(
 name   => 'MPD Client',
 tag    => 'MPD',
 desc   => 'Music Player Daemon client',
 author => 'Vincent Pit (VPIT)',
 email  => 'perl@profvince.com',
 url    => 'http://www.profvince.com',
);

1;
