package Koha::Plugin::Pt::KEEPS::OpacGoogleAnalytics;

use Modern::Perl;

use base qw(Koha::Plugins::Base);
use C4::Languages;
use C4::Context;

our $VERSION = '1.1';

our $metadata = {
    name   => 'KEEPS - OPAC Google Analytics',
    author => 'Pedro Moura - pmoura@keep.pt',
    description => 'Plugin for having Google Analytics in OPAC',
    date_authored   => '2022-01-21',
    date_updated    => '2024-03-14',
    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,
            measurement_option=>$self->retrieve_data('measurement_option'),
            measurement_id=>$self->retrieve_data('measurement_id'),
            first_url=>$self->retrieve_data('first_url'),
            first_measurement_id=>$self->retrieve_data('first_measurement_id'),
            second_url=>$self->retrieve_data('second_url'),
            second_measurement_id=>$self->retrieve_data('second_measurement_id'),
            last_upgraded   => $self->retrieve_data('last_upgraded'),
        );
        $self->output_html( $template->output() );
    } else {
        $self->store_data(
            {
                measurement_option=>$cgi->param('measurement_option'),
                measurement_id=>$cgi->param('measurement_id'),
                first_url=>$cgi->param('first_url'),
                first_measurement_id=>$cgi->param('first_measurement_id'),
                second_url=>$cgi->param('second_url'),
                second_measurement_id=>$cgi->param('second_measurement_id'),
                last_configured_by => C4::Context->userenv->{'number'},
            }
        );
        $self->go_home();
    }
}


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

    my $mid = $self->retrieve_data('measurement_id');
    my $measurement_option = $self->retrieve_data('measurement_option');
    my $first_url = $self->retrieve_data('first_url');
    my $first_measurement_id = $self->retrieve_data('first_measurement_id');
    my $second_url = $self->retrieve_data('second_url');
    my $second_measurement_id = $self->retrieve_data('second_measurement_id');

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

    var hostname = window.location.hostname;

    if( '$measurement_option' == 'single-measurement' ){
        (function () {
            var s = document.createElement('script');
            s.type = 'text/javascript';
            s.async = true;
            s.src = 'https://www.googletagmanager.com/gtag/js?id=' + '$mid';
            var x = document.getElementsByTagName('script')[0];
            x.parentNode.insertBefore(s, x);
        })();

        window.dataLayer = window.dataLayer || [];

        function gtag() {
            dataLayer.push(arguments);
        }
        gtag('js', new Date());

        gtag('config', '$mid');
    }
    else if( '$measurement_option' == 'double-measurement' ){
        if (hostname == '$first_url' ) {
            (function () {
                var s = document.createElement('script');
                s.type = 'text/javascript';
                s.async = true;
                s.src = 'https://www.googletagmanager.com/gtag/js?id=' + '$first_measurement_id';
                var x = document.getElementsByTagName('script')[0];
                x.parentNode.insertBefore(s, x);
            })();

                window.dataLayer = window.dataLayer || [];

                function gtag() {
                    dataLayer.push(arguments);
                }
                gtag('js', new Date());

                gtag('config', '$first_measurement_id');
        }
        else if( hostname == '$second_url' ){
            (function () {
                var s = document.createElement('script');
                s.type = 'text/javascript';
                s.async = true;
                s.src = 'https://www.googletagmanager.com/gtag/js?id=' + '$second_measurement_id';
                var x = document.getElementsByTagName('script')[0];
                x.parentNode.insertBefore(s, x);
            })();

                window.dataLayer = window.dataLayer || [];

                function gtag() {
                    dataLayer.push(arguments);
                }
                gtag('js', new Date());

                gtag('config', '$second_measurement_id');
        }
    }
})

</script>
%;
}

1;