package Koha::Plugin::Pt::KEEPS::OpacCookies;

use utf8;

use Modern::Perl;
use URI::Escape;

use C4::Languages;
use C4::Context;

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

our $VERSION = '1.01';

our $metadata = {
    name   => 'KEEPS - OPAC Cookies',
    author => 'Pedro Moura - pmoura@keep.pt',
    description => 'Plugin for EU cookies law implementation in OPAC.',
    date_authored   => '2022-01-21',
    date_updated    => '2024-04-04',
    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 configure {
    my ($self, $args) = @_;
    my $cgi = $self->{'cgi'};

    unless ($cgi->param('save')) {
        my $lang_dialect = C4::Languages::getlanguage($cgi);

        my $template = $self->get_template({ file => 'configure.tt' });

        $template->param(
            lang_dialect => $lang_dialect,
            cookie_policy_url => $self->retrieve_data('cookie_policy_url'),
            last_upgraded   => $self->retrieve_data('last_upgraded'),
        );

        $self->output_html( $template->output() );
    } else {
        $self->store_data(
            {
                cookie_policy_url => $cgi->param('cookie_policy_url'),
                last_configured_by => C4::Context->userenv->{'number'},
            }
        );
        $self->go_home();
    }
}


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

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

    # pass the plugin_dir as a query param so that the .pl has access
    # to the plugin_dir
    my $encoded_path = uri_escape($path);

    return qq%
<script>
\$(document).ready(function () {

    var lang = \$('html').attr('lang');
    if (lang == null) {
        lang = \$('html').attr('xml:lang');
    }

    // Set the global configs to synchronous 
    \$.ajaxSetup({
        async: false
    });

    var TOKENS = {};
    \$.getJSON(`$path/i18n/\${lang}.json`, function (data) {
        TOKENS = data;
    });

    \$.ajaxSetup({
        async: true
    });

    // cookie for language
    if(typeof \$.cookie('KohaOpacLanguage') === 'undefined') {
        \$.cookie('KohaOpacLanguage', lang, { expires: 7, path: '/' });
    }

    \$.getScript('$path/jquery.cookie.js', function() {
        \$.getScript('$path/jquery.cookiecuttr.js', function() {
            \$('head').append('<link rel="stylesheet" href="$path/cookies.css" type="text/css"/>');
            \$('#tags').hide();

            \$.cookieCuttr({
                cookieNotificationLocationBottom : true,
                cookieAnalytics : false,
                cookiePolicyLink : '$path/cookies.pl?path=$encoded_path',
                cookieMessage : `\${TOKENS.cookieMessage}`,
                cookieAcceptButtonText : `\${TOKENS.cookieAcceptButtonText}`
            });
        });
    });
});
</script>
%;
}

1;