use strict;
use warnings;

package Liberl::View::Book;
use Jifty::View::Declare -base;

# aliased in /book

template '/show' => page {
    my $id = get('id');

    my $book = Liberl::Model::Book->new;
    $book->load($id);

    { title is $book->title }

    render_region(
        name => 'book',
        path => '/book/show/full',
        arguments => {
            id => $id,
            notitle => 1,
        },
        );

    render_region(
        name     => 'comments',
        path     => '/__comment/add',
        arguments => {
            parent => $book->for_commenting,
            initial_title => '',
        },
        );

};

template '/show/preview' => sub {
    my $id = get 'id';
    my @authors = authors($id);

    my $book = Liberl::Model::Book->new;
    $book->load($id);

    my $key = $book->cover_key;
    if ($book->id) {
        dt {
            hyperlink(
                label => $book->title,
                onclick => [
                    {replace_with => '/book/show/full'},
                ]
                );
        };

        img { src is "/static/covers/small/$key.jpg" };
        hyperlink(
            label => _("Enlarge"),
                url => "/static/covers/large/$key.jpg",
            );
        p { join(', ', @authors) };
        if ($book->publisher->id) {
            p { $book->publisher->name }
        }
    }
};

template '/show/full' => sub {
    my $id = get 'id';
    my $notitle = get 'notitle';
    my @authors = authors($id);

    my $book = Liberl::Model::Book->new;
    $book->load($id);

    my $key = $book->cover_key;
    if ($book->id) {
        dt {
            if (!$notitle) {
                hyperlink(
                    label => $book->title,
                    onclick => [
                        {replace_with => '/book/show/preview'},
                    ]
                    );
            }
        };
        dd {
            img { src is "/static/covers/medium/$key.jpg" };
            hyperlink(
                label => _("Enlarge"),
                url => "/static/covers/large/$key.jpg",
                );

            p { strong { _('Authors: ') } }
            ul {
                for (@authors) {
                    li {$_};
                }
            };
            if ($book->publisher->id) {
                p {
                    strong { _('Publisher: ') };
                    outs $book->publisher->name
                }
            }
            if ($book->format->id) {
                p {
                    strong { _('Format: ') };
                    outs $book->format->name
                }
            }
            if ($book->numpages) {
                p {
                    strong { _('Pages: ') };
                    outs $book->numpages
                }
            }
            if ($book->isbn) {
                p {
                    strong { _('ISBN-13: ') };
                    outs $book->isbn
                }
            }
            if ($book->publication_date) {
                p {
                    strong { _('Publication date: ') };
                    outs $book->publication_date
                }
            }
            if ($book->description) {
                p { strong { _('Description: ') } }
                p { $book->description }
            }
        }
    }
};

sub authors {
    my $id = shift;

    my $book = Liberl::Model::Book->new;
    $book->load($id);

    my $link_authors = Liberl::Model::LinkBookAuthorCollection->new;
    $link_authors->limit(
        column => 'book',
        value  => $book,
        );

    my @authors_names = ();
    while (my $link_author = $link_authors->next()) {
        my $author = Liberl::Model::Author->new;
        $author->load($link_author->author);
        my $firstname = $author->firstname // '';
        my $lastname = $author->lastname // '';
        push @authors_names,  join(' ', ($firstname, $lastname));
    }

    return @authors_names;
}

1;

