package Koha::Plugin::Pt::KEEPS::AuthoritiesNavigator;

use Modern::Perl;

use base qw(Koha::Plugins::Base);

our $VERSION = '1.0';

our $metadata = {
    name   => 'KEEPS - Authorities Navigator',
    author => 'Keep Solutions',
    description => 'Allows navigating through authorities records',
    date_authored   => '2022-01-24',
    date_updated    => undef,
    minimum_version => '21.05',
    maximum_version => undef,
    version         => $VERSION,
};

sub new {
    my ( $class, $args ) = @_;

    $args->{'metadata'} = $metadata;
    $args->{'metadata'}->{'class'} = $class;

    my $self = $class->SUPER::new($args);

    return $self;
}

# Mandatory even if does nothing
sub install {
    my ( $self, $args ) = @_;

    return 1;
}

# Mandatory even if does nothing
sub upgrade {
    my ( $self, $args ) = @_;

    return 1;
}

# Mandatory even if does nothing
sub uninstall {
    my ( $self, $args ) = @_;

    return 1;
}

sub intranet_js {
    my ( $self ) = @_;

    my $path = $self->get_plugin_http_path();

    return qq%
<script>
    var pathname = window.location.pathname;
    if (pathname.match('/authorities/detail.pl')) {
        var plugin_path = "$path";
        
        \$('#toolbar').append(`
            <div class="btn-group" style="margin-left: 20px;">
                <a id="first-authid" class="btn btn-default btn-small disabled">
                    <i class="fa fa-fast-backward"></i>
                </a>
            </div>
        `);

        \$('#toolbar').append(`
            <div class="btn-group">
                <a id="previous-authid" class="btn btn-default btn-small disabled">
                    <i class="fa fa-backward"></i>
                </a>
            </div>
        `);

        \$('#toolbar').append(`
            <div class="btn-group">
                <a id="next-authid" class="btn btn-default btn-small disabled">
                    <i class="fa fa-forward"></i>
                </a>
            </div>
        `);

        \$('#toolbar').append(`
            <div class="btn-group">
                <a id="last-authid" class="btn btn-default btn-small disabled">
                    <i class="fa fa-fast-forward"></i>
                </a>
            </div>
        `);

        const params = new URLSearchParams(window.location.search);
        var authid = params.get('authid');

        \$.ajax({
            url: plugin_path + `/auth-navigator.pl?authid=` + authid,
        }).done(function(data) {

            if (data.first_authid) {
                var href = `/cgi-bin/koha/authorities/detail.pl?authid=` + data.first_authid;
                \$('#first-authid').removeClass('disabled');
                \$('#first-authid').attr('href', href);
            }
            
            
            if (data.previous_authid) {
                var href = `/cgi-bin/koha/authorities/detail.pl?authid=` + data.previous_authid;
                \$('#previous-authid').removeClass('disabled');
                \$('#previous-authid').attr('href', href);
            }
            
            
            if (data.next_authid) {
                var href = `/cgi-bin/koha/authorities/detail.pl?authid=` + data.next_authid;
                \$('#next-authid').removeClass('disabled');
                \$('#next-authid').attr('href', href);
            }
            
            
            if (data.last_authid) {
                var href = `/cgi-bin/koha/authorities/detail.pl?authid=` + data.last_authid;
                \$('#last-authid').removeClass('disabled');
                \$('#last-authid').attr('href', href);
            }
        });
    }
</script>
%;
}

1;