package Koha::Plugin::Pt::KEEPS::Isbd_xslt;

use Modern::Perl;

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

our $VERSION = '1.04';

our $metadata = {
    name   => 'KEEPS - ISBD_xslt',
    author => 'Keep Solutions - joliveira@keep.pt',
    description => 'Processa a vista ISBD com stylesheets',
    date_authored   => '2023-05-05',
    date_updated    => '2023-09-28',
    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 ) = @_;

    my $opac_isbd_slim_utils_file = '/KEEPS/koha/var/lib/koha/plugins/Koha/Plugin/Pt/KEEPS/Isbd_xslt/files/opac/xslt_default/ISBDslimUtils.xsl';
    my $opac_xslt_pt_file = '/KEEPS/koha/var/lib/koha/plugins/Koha/Plugin/Pt/KEEPS/Isbd_xslt/files/opac/xslt_default/KEEPS_isbd_pt-PT.xsl';
    my $opac_xslt_en_file = '/KEEPS/koha/var/lib/koha/plugins/Koha/Plugin/Pt/KEEPS/Isbd_xslt/files/opac/xslt_default/KEEPS_isbd_en.xsl';
    my $opac_xslt_es_file = '/KEEPS/koha/var/lib/koha/plugins/Koha/Plugin/Pt/KEEPS/Isbd_xslt/files/opac/xslt_default/KEEPS_isbd_es-ES.xsl';
    my $opac_isbd_slim_utils;
    my $opac_xslt_pt;
    my $opac_xslt_en;
    my $opac_xslt_es;

    open(my $opac_isbd_slim_utils_fh, '<', $opac_isbd_slim_utils_file) or die "Não foi possível abrir o arquivo $opac_isbd_slim_utils_file: $!";
    while (my $line = <$opac_isbd_slim_utils_fh>) {
        $opac_isbd_slim_utils .= $line;
    }
    close($opac_isbd_slim_utils_fh);

    open(my $opac_xslt_pt_fh, '<', $opac_xslt_pt_file) or die "Não foi possível abrir o arquivo $opac_xslt_pt_file: $!";
    while (my $line = <$opac_xslt_pt_fh>) {
        $opac_xslt_pt .= $line;
    }
    close($opac_xslt_pt_fh);

    open(my $opac_xslt_en_fh, '<', $opac_xslt_en_file) or die "Não foi possível abrir o arquivo $opac_xslt_en_file: $!";
    while (my $line = <$opac_xslt_en_fh>) {
        $opac_xslt_en .= $line;
    }
    close($opac_xslt_en_fh);

    open(my $opac_xslt_es_fh, '<', $opac_xslt_es_file) or die "Não foi possível abrir o arquivo $opac_xslt_es_file: $!";
    while (my $line = <$opac_xslt_es_fh>) {
        $opac_xslt_es .= $line;
    }
    close($opac_xslt_es_fh);


    $self->store_data(
        {
            opac_isbd_slim_utils => $opac_isbd_slim_utils,
            opac_xslt_pt => $opac_xslt_pt,
            opac_xslt_en => $opac_xslt_en,
            opac_xslt_es => $opac_xslt_es,
            intranet_isbd_slim_utils => $opac_isbd_slim_utils,
            intranet_xslt_pt => $opac_xslt_pt,
            intranet_xslt_en => $opac_xslt_en,
            intranet_xslt_es => $opac_xslt_es,

        }
    );

    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'};

    my $kohaflushmemcached = 'kohaflushmemcached';
    my $command_kohaflushmemcached = "bash -ic '$kohaflushmemcached'";
    my $kohaplack = 'kohaplack';
    my $command_kohaplack = "bash -ic '$kohaplack'";
    my $status;

    if ($cgi->param('default_configuration') eq 'reset') {

        my $copy_default = 'cp -r /KEEPS/koha/var/lib/koha/plugins/Koha/Plugin/Pt/KEEPS/Isbd_xslt/files/opac/xslt_default/* /KEEPS/koha/var/lib/koha/plugins/Koha/Plugin/Pt/KEEPS/Isbd_xslt/files/opac/xslt/ && cp -r /KEEPS/koha/var/lib/koha/plugins/Koha/Plugin/Pt/KEEPS/Isbd_xslt/files/opac/xslt_default/* /KEEPS/koha/var/lib/koha/plugins/Koha/Plugin/Pt/KEEPS/Isbd_xslt/files/intranet/xslt/';
                      
        my $bash_command = "bash -ic '$copy_default'";
        system($bash_command);  
        system($command_kohaflushmemcached);
        system($command_kohaplack);
        $self->go_home();
        return;
    }


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

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

        # Fill in the template
        $template->param(
            lang_dialect => $lang_dialect,
            last_upgraded   => $self->retrieve_data('last_upgraded'),
            opac_isbd_slim_utils => $self->retrieve_data('opac_isbd_slim_utils'),
            opac_xslt_pt => $self->retrieve_data('opac_xslt_pt'),
            opac_xslt_en => $self->retrieve_data('opac_xslt_en'),
            opac_xslt_es => $self->retrieve_data('opac_xslt_es'),
            intranet_isbd_slim_utils => $self->retrieve_data('intranet_isbd_slim_utils'),
            intranet_xslt_pt => $self->retrieve_data('intranet_xslt_pt'),
            intranet_xslt_en => $self->retrieve_data('intranet_xslt_en'),
            intranet_xslt_es => $self->retrieve_data('intranet_xslt_es'),
        );



        $self->output_html( $template->output() );
    } else {
        
        $self->store_data(
            {
                last_configured_by => C4::Context->userenv->{'number'},
                opac_isbd_slim_utils => $cgi->param('opac_isbd_slim_utils'),
                opac_xslt_pt => $cgi->param('opac_xslt_pt'),
                opac_xslt_en => $cgi->param('opac_xslt_en'),
                opac_xslt_es => $cgi->param('opac_xslt_es'),
                intranet_isbd_slim_utils => $cgi->param('intranet_isbd_slim_utils'),
                intranet_xslt_pt => $cgi->param('intranet_xslt_pt'),
                intranet_xslt_en => $cgi->param('intranet_xslt_en'),
                intranet_xslt_es => $cgi->param('intranet_xslt_es'),
            }
        );

        my %params_opac = (
            'opac_xslt_pt' => 'KEEPS_isbd_pt-PT.xsl',
            'opac_xslt_es' => 'KEEPS_isbd_es-ES.xsl',
            'opac_xslt_en' => 'KEEPS_isbd_en.xsl',
            'opac_isbd_slim_utils' => 'ISBDslimUtils.xsl'
        );

        foreach my $param (keys %params_opac) {
            my $opac_xslt = $cgi->param($param);
            next unless $opac_xslt;            
            my $file_path = "/KEEPS/koha/var/lib/koha/plugins/Koha/Plugin/Pt/KEEPS/Isbd_xslt/files/opac/xslt/$params_opac{$param}";
            my $new_content = $opac_xslt;
            open(my $fh, '>:encoding(utf-8)', $file_path) or die "Não foi possível abrir o arquivo: $!";
            print $fh $new_content;
            close($fh);
        }

        my %params_intranet = (
            'intranet_xslt_pt' => 'KEEPS_isbd_pt-PT.xsl',
            'intranet_xslt_es' => 'KEEPS_isbd_es-ES.xsl',
            'intranet_xslt_en' => 'KEEPS_isbd_en.xsl',
            'intranet_isbd_slim_utils' => 'ISBDslimUtils.xsl'
        );

        foreach my $param (keys %params_intranet) {
            my $intranet_xslt = $cgi->param($param);
            next unless $intranet_xslt;            
            my $file_path = "/KEEPS/koha/var/lib/koha/plugins/Koha/Plugin/Pt/KEEPS/Isbd_xslt/files/intranet/xslt/$params_intranet{$param}";
            my $new_content = $intranet_xslt;
            open(my $fh, '>:encoding(utf-8)', $file_path) or die "Não foi possível abrir o arquivo: $!";
            print $fh $new_content;
            close($fh);
        }

        system($command_kohaflushmemcached);
        system($command_kohaplack);

        $self->go_home();
    }
}

sub intranet_js {
    my ( $self ) = @_;   
    return qq%
        <script>
            \$('a').each(function() {
                var href = \$(this).attr('href');
        if (href && href.includes('/cgi-bin/koha/catalogue/ISBDdetail.pl')) {
            href = href.replace('/cgi-bin/koha/catalogue/ISBDdetail.pl', '/plugin/Koha/Plugin/Pt/KEEPS/Isbd_xslt/files/intranet/keeps-ISBDdetail.pl');
            \$(this).attr('href', href);
        }
        });
        </script>
    %;
}

sub opac_js {
    my ( $self ) = @_;   
    return qq%
        <script>
            \$('a').each(function() {
                var href = \$(this).attr('href');
        if (href && href.includes('/cgi-bin/koha/opac-ISBDdetail.pl')) {
            href = href.replace('/cgi-bin/koha/opac-ISBDdetail.pl', '/plugin/Koha/Plugin/Pt/KEEPS/Isbd_xslt/files/opac/keeps-opac-ISBDdetail.pl');
            \$(this).attr('href', href);
        }
        });
        </script>
    %;
}

1;