use strict;
use warnings;

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

use DateTime;
use DateTime::Duration;

require Liberl::View::Book;
alias Liberl::View::Book under 'book/';

require Liberl::View::Add;
alias Liberl::View::Add under 'add/';

require Liberl::View::Books;
alias Liberl::View::Books under 'books/';

private template 'menu' => sub {
    outs_raw(Jifty->web->navigation->render_as_yui_menubar);
#    outs_raw(Jifty->web->navigation->render_as_hierarchical_menu_item);
};

template '/' => page {
    { title is _('Liberl') }

    my $libraries = Liberl::Model::LibraryCollection->new;
    if (Jifty->web->current_user->id && $libraries->count_all) {
        div {
            render_region(
                name => 'quick_add',
                path => '/add/book/isbn/widget',
                );
        }
    }

    div {
        render_region(
            name => 'quick_search',
            path => '/search/widget',
            );
    }

    div {
        h2 { _('Recent books') };
        render_region(
            name => 'books_recent',
            path => '/books/recent/widget',
            arguments => { days => 7 },
            );
    }

    div {
        h2 { _('Last books') };
        render_region(
            name => 'books_last',
            path => '/books/last/widget',
            arguments => { count => 5 },
            );
    }
};

template '/search' => page {
    { title is _('Search') };

    my ($books, $search) = get(qw(books search));

    form {
        render_param
            $search => 'title_contains',
            label => _('Title:');
        form_submit
            label => 'Search',
            submit => $search;
    };
    if ($books) { 
        show(
            '/books/list' =>
            books => $books,
            );
    }
};

template '/search/widget' => sub {
    my $action = new_action(class => 'SearchBook', moniker => 'search');
    $action->sticky_on_success(1);
    form {
        form_next_page url => '/search';
        render_param $action, 'title_contains', label => 'Search by title:';
    };
};

template '/manage' => page {
    { title is _('Manage your database') };

    ul {
        li {
            hyperlink(
                label => _('Add a new library'),
                url   => '/add/library',
                );
            render_region(
                name => 'add_library',
                path => '/add/library/widget',
                );
        }

        li {
            hyperlink(
                label => _('Add a new format'),
                url   => '/add/format',
                );
            render_region(
                name => 'add_format',
                path => '/add/format/widget',
                );
        }
    }
};

1;

