#!/usr/bin/perl
use 5.016;
use strict;

use Test::More;

use File::Spec;
use Time::Piece;

use EBook::Ishmael::EBook;
use EBook::Ishmael::ImageID qw(image_id);

# TODO: Add tests to make html() produces valid HTML

my $EPUB = File::Spec->catfile(qw/t data gpl3.epub/);

my $ebook = EBook::Ishmael::EBook->new($EPUB, undef, undef, 0);
isa_ok($ebook, 'EBook::Ishmael::EBook::Epub');

like($ebook->{Source}, qr/\Q$EPUB\E$/, "source ok");
like($ebook->{_container}, qr/container\.xml$/, "container ok");
like($ebook->{_rootfile}, qr/content\.opf$/, "rootfile ok");
is($ebook->{_contdir}, $ebook->{_unzip}, "content directory ok");

subtest "spine ok" => sub {

    plan tests => 5;

    like($ebook->{_spine}->[0], qr/titlepage\.xhtml$/, "spine[0] ok");
    like($ebook->{_spine}->[1], qr/index_split_000\.html$/, "spine[1] ok");
    like($ebook->{_spine}->[2], qr/index_split_001\.html$/, "spine[2] ok");
    like($ebook->{_spine}->[3], qr/index_split_002\.html$/, "spine[3] ok");

    is(@{ $ebook->{_spine} }, 4, "spine count ok");

};

is($ebook->metadata->title, 'gpl3', 'title ok');
is($ebook->metadata->id, 'e3f35c22-0889-4539-9f97-eebf0d391b18', 'id ok');
is($ebook->metadata->format, 'EPUB 2.0', 'format ok');
is_deeply([ $ebook->metadata->language ], [ 'en' ], 'language ok');
is_deeply([ $ebook->metadata->author ], [ 'Unknown' ], 'author ok');
is_deeply([ $ebook->metadata->contributor ],
          [ 'calibre (7.16.0) [https://calibre-ebook.com]' ],
          'contributor ok');
SKIP: {
    unless ($Time::Piece::VERSION ge '1.38') {
        skip "Time::Piece $Time::Piece::VERSION cannot parse timezones correctly", 1;
    }
    is($ebook->metadata->created, 978307200, 'created ok');
}

ok($ebook->html, "html ok");

ok($ebook->has_cover, "has cover");

subtest 'cover ok' => sub {
    my ($img, $format) = $ebook->cover;
    is($format, 'jpg', 'cover format ok');
    is(image_id($img), 'jpg', 'cover looks like a jpeg');
};

subtest 'images ok' => sub {
    is($ebook->image_num, 1, "image count ok");
    my ($img, $format) = $ebook->image(0);
    is($format, 'jpg', 'image #0 format ok');
    is(image_id($img), 'jpg', 'image #0 looks like a jpeg');
};

done_testing();
