package Audio::Tagger::File::Ogg;

use strict;
use warnings;
# TODOD require correct version of Ogg::Vorbis::Header
use Ogg::Vorbis::Header;
use base qw/Audio::Tagger::File/;

# Open file and create object...
sub _open {
	my ($self, $file) = @_;
	my $ogg = Ogg::Vorbis::Header->new($file)
		or throw Audio::Tagger::File::Error("Could not open file with Ogg::Vorbis::Header : $file");

	# Get info...
	my $hash = {
		artist => {
			name     => 'ARTIST',
			id       => 'MUSICBRAINZ_ARTISTID',
			sortname => 'MUSICBRAINZ_SORTNAME'},
		track  => {
			name     => 'TITLE',
			number   => 'TRACKNUMBER',
			id       => 'MUSICBRAINZ_TRACKID'},
		album  => {
			name     => 'ALBUM',
			id       => 'MUSICBRAINZ_ALBUMID',}};

	my @com = $ogg->comment_tags;

	#TODO comment allot better or rewrite!

	while (my ($a, $meta) = each %{$hash}) {
		# Loop artist, track, album...
		while (my ($key, $find) = each %{$meta}) {
			if(my ($com) = grep /^\Q$find\E$/i, @com) {
				$self->$a->$key( ($ogg->comment($com))[0] );
			}
		}
	}

	my $length = $ogg->info("length");
	if ($length =~ m/^-?inf$/) {
		$length = 0;
#		throw Audio::Tagger::File::Error("Corrupt ogg header, length = inf!");
	}

	$self->{track}->{number}   = int($self->{track}->{number}||0); # some people fancy storing tracks as "07" and so on, we want int, not string
	$self->{track}->{duration} = int($length*1000);
	$self->{bitrate}           = int($ogg->info("bitrate_nominal") / 1000);
	$self->{samplerate}        = int($ogg->info("rate"));

	return $self;
}

sub save {
	# Commit changes to ogg file
	my $self = shift;

	my $ogg = Ogg::Vorbis::Header->new($self->absname)
		or throw Audio::Tagger::File::Error("Could not open file with Ogg::Vorbis::Header : " . $self->filename);

	# We have to cheat and use the internal methods of Ogg::Vorbis::Header
	# to be able to comply with the Xiph comments standard
	$ogg->_load_comments;

	my $hash = {
		artist => {
			name     => 'ARTIST',
			id       => 'MUSICBRAINZ_ARTISTID',
			sortname => 'MUSICBRAINZ_SORTNAME'},
		track  => {
			name     => 'TITLE',
			number   => 'TRACKNUMBER',
			id       => 'MUSICBRAINZ_TRACKID'},
		album  => {
			name     => 'ALBUM',
			id       => 'MUSICBRAINZ_ALBUMID',}};

	my @keys = $ogg->comment_tags;


	while (my ($a, $meta) = each %{$hash}) {
		while (my ($func, $value) = each %{$meta}) {
			if(my ($key) = grep /^\Q$value\E$/i, @keys) {
				# Weed out "bad" comments...
				delete $ogg->{COMMENTS}->{$key};
			}
			$ogg->{COMMENTS}->{$value} = [ $self->$a->$func ] if defined $self->$a->$func;
		}
	}

	$ogg->write_vorbis()
		or throw Audio::Tagger::File::Error("Could not save changes to file with Ogg::Vorbis::Header : " . $self->filename);
}

1;

__END__

=head1 NAME

Audio::Tagger::File::Ogg - Access and edit Xiph comments in ogg-vorbis files.

=head1 DESCRIPTION

This module is a simple wrapper around L<Ogg::Vorbis::Header> so that we can use
L<Audio::Tagger> with ogg-vorbis file

This class should not be used directly, see L<Audio::Tagger::File> instead.

=head1 BUGS/ISSUES

This version of Audio::Tagger::File::Ogg ignores the perl wrapper code that is
provided by Ogg::Vorbis::Header as it does not follow the Xiph comment specs.
Thus if there are any internal changes to Ogg::Vorbis::Header this module will
break.  For more information about the details of the Xiph spec see
L<http://xiph.org/vorbis/doc/v-comment.html>

