#!perl

# TODO add test that checks that File actually deletes undefed items

use Test::More qw(no_plan);
use Test::Deep;
#use Test::MockObject;

use File::Basename;
use File::Copy;
use diagnostics;
use Cwd qw/abs_path/;
use blib;

BEGIN {
	use_ok( 'Audio::Tagger::File' );
}

require_ok('Audio::Tagger::File');
my $file;
eval{$file = Audio::Tagger::File->open;};
ok(!defined $file, "create new object without sending filename");

my $f     = "t/test-test.ogg";
my $f_org = "t/test.ogg";

copy $f_org, $f or die "Could not copy file: $!";

$file = Audio::Tagger::File->open($f);
my $album = {
	name   => 'albumname',
	artist => $file->album->artist,
	id     => 'albumid',
	};
my $artist = {
	sortname => 'sortname',
	name     => 'name',
	id       => 'artistid'};
my $track = {
	number     => '1',
	name       => 'trackname',
	id         => 'trackid',
	duration   => 4405,
	};

isa_ok($file, "Audio::Tagger::File");
isa_ok($file, "Audio::Tagger::Meta");
isa_ok($file->artist, "Audio::Tagger::Meta::Artist");
isa_ok($file->track,  "Audio::Tagger::Meta::Track");
isa_ok($file->album,  "Audio::Tagger::Meta::Album");

my $filename = abs_path($f);
my ($name, $path, $suffix) = fileparse($filename, qw/\.mp3$ \.ogg$/);
$suffix =~ s/^\.//;

is_deeply($file->artist, $artist, "$f: checking artist deeply");
is_deeply($file->track, $track, "$f: checking track deeply");
is_deeply($file->album, $album, "$f: checking album deeply");

is($file->absname,   $filename, "$f: testing filename");
is($file->base,  $name, "$f: testing basename");
is($file->folder, $path, "$f: testing folder");
is($file->ext,    $suffix, "$f: testing extemsion");

ok($file->artist->name("test"), "$f: Changed artist name");
ok($file->artist({id => "test-id"}), "$f: Changed artist-id");
ok($file->save, "$f: Saved changes");

$artist->{name} = "test";
$artist->{id} = "test-id";

$file = Audio::Tagger::File->open($f);
is_deeply($file->artist, $artist, "$f: checking artist deeply, after re-opening file");

unlink $f;
