use strict;
use warnings;

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

# aliased in /add

template '/library' => page {
    { title is _('Create a new library') }

    render_region(
        name => 'add_library',
        path => '/add/library/widget',
        );
};

template '/library/widget' => sub {
    my $next = get 'next' // Jifty::Continuation->new(
        request => Jifty::Request->new( path => "/" ) );
    
    form {
        my $create = new_action(class => 'CreateLibrary');
        render_action($create, [ qw( name place public ) ]);
        form_return(
            label   => _('Create'),
            onclick => [
                { submit => $create },
            ],
            call => $next,
            );
    }
};

template '/format' => page {
    { title is _('Create a new format') }

    render_region(
        name => 'add_format',
        path => '/add/format/widget',
        );
};

template '/format/widget' => sub {
    form {
        my $create = new_action(class => 'CreateFormat');
        render_action($create, [ qw( name ) ]);
        form_submit(
            label   => _('Create'),
            onclick => [
                { submit => $create },
            ]);
    }
};

template '/book' => page {
    { title is _('Add a book') }

    hyperlink(
        label => _('By ISBN'),
        onclick => [
            { replace_with => '/add/book/form/isbn',
              region       => 'add_form' },
        ]);

    hyperlink(
        label => _('Manual'),
        onclick => [
            { replace_with => '/add/book/form/manual',
              region       => 'add_form' },
        ]);

    render_region(
        name => 'add_form',
        path => '/add/book/form/isbn',
        );

    div {
        render_region(
            name => 'add_log',
            );
    }
};

template '/book/log' => sub {
    my $title = get('title');
    if ($title) {
        em { $title };
        outs(_(' successfully added.'));
    }
};

template '/book/form/manual' => sub {
    form {
        my $add = new_action(
            class => 'AddBook',
            arguments => {
                title            => get('title'),
                authors          => get('authors'),
                publisher        => get('publisher'),
                year             => get('year'),
                isbn             => get('isbn'),
                edition          => get('edition'),
                ean              => get('ean'),
                numpages         => get('numpages'),
                publication_date => get('publication_date'),
                ImageUrlSmall    => get('ImageUrlSmall'),
                ImageUrlMedium   => get('ImageUrlMedium'),
                ImageUrlLarge    => get('ImageUrlLarge'),
                library          => get('library'),
            },
            );
        render_action($add, [ qw(library title authors publisher isbn edition numpages format publication_date description ImageUrlSmall ImageUrlMedium ImageUrlLarge cover) ]);
        form_submit(
            label   => _('Add a book'),
            onclick => [
                { submit => $add },
                { region => 'add_form-add_isbn_result',
                  replace_with => '/__jifty/empty' },
                { region => 'add_log',
                  prepend => '/add/book/log',
                  args   => {
                      title => { result_of => $add, name => 'title' }
                  },
                }
            ],
            );
    };
};

template '/book/form/isbn' => sub {
    my $action = Jifty->web->new_action(class => 'SearchISBN');
    my $search = get('search');

    form {
        render_param($action => 'isbn', focus => 1);
        render_param($action => 'library');
        form_submit(
            label   => _('Search'),
            onclick => [
                { submit       => $action },
                { region       => 'add_form-add_isbn_result',
                  replace_with => '/add/book/isbn_result',
                  args         => {
                      title            => { result_of => $action, name => 'title' },
                      authors          => { result_of => $action, name => 'authors' },
                      publisher        => { result_of => $action, name => 'publisher' },
                      year             => { result_of => $action, name => 'year' },
                      isbn             => { result_of => $action, name => 'isbn' },
                      edition          => { result_of => $action, name => 'edition' },
                      ean              => { result_of => $action, name => 'ean' },
                      numpages         => { result_of => $action, name => 'numpages' },
                      publication_date => { result_of => $action, name => 'publication_date' },
                      ImageUrlSmall    => { result_of => $action, name => 'ImageUrlSmall' },
                      ImageUrlMedium   => { result_of => $action, name => 'ImageUrlMedium' },
                      ImageUrlLarge    => { result_of => $action, name => 'ImageUrlLarge' },
                      library          => { result_of => $action, name => 'library' },
                  },
                },
            ],
            );
    };

    if ($search) {
        render_region(
            name => 'add_isbn_result',
            path => '/add/book/isbn_result',
            arguments => {
                title            => $search->content('title'),
                authors          => $search->content('authors'),
                publisher        => $search->content('publisher'),
                year             => $search->content('year'),
                isbn             => $search->content('isbn'),
                edition          => $search->content('edition'),
                ean              => $search->content('ean'),
                numpages         => $search->content('numpages'),
                publication_date => $search->content('publication_date'),
                ImageUrlSmall    => $search->content('ImageUrlSmall'),
                ImageUrlMedium   => $search->content('ImageUrlMedium'),
                ImageUrlLarge    => $search->content('ImageUrlLarge'),
                library          => $search->content('library'),
            },
            );
    }
    else {
        render_region(
            name => 'add_isbn_result',
            );
    }
};

template '/book/isbn_result' => sub {
    hyperlink(
        label => _('Close'),
        onclick => [
            {replace_with => '/__jifty/empty'},
        ]);

    if (defined get('ImageUrlMedium')) {
        img { src is get('ImageUrlMedium') };
    }

    render_region(
        name => 'add_isbn_form',
        path => '/add/book/form/manual',
        );
};

template '/book/isbn/widget' => sub {
    my $action = new_action(class => 'SearchISBN', moniker => 'add_isbn');
    span {
        form {
            form_next_page(url => '/add/book');
            render_param($action => 'isbn',
                         label => _('Quick add:'),
                         hints => _('By ISBN'),
                );
        }
    };
};

1;

