package Koha::Plugin::Pt::KEEPS::SocialNetworks;

use Modern::Perl;

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

our $VERSION = '1.0';

our $metadata = {
    name   => 'KEEPS - OPAC Social Networks',
    author => 'Keep Solutions',
    description => 'Allows to share Koha content to several social networks',
    date_authored   => '2022-01-27',
    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 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,
            social_networks => $self->retrieve_data('social_networks'),
            last_upgraded   => $self->retrieve_data('last_upgraded'),
        );

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

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

    my $path = $self->get_plugin_http_path();
    my $order_config = $self->retrieve_data('social_networks') || '["facebook", "email", "twitter", "whatsapp"]';

    return qq%
<script>
    var pathname = window.location.pathname;

    if (pathname.match("/cgi-bin/koha/opac-detail.pl")) {
        window.social_plugin_path = "$path";
        window.social_config_arr = \$.parseJSON('$order_config');
        \$.getScript(social_plugin_path + "/social-networks.js");
    }

</script>
%;
}

1;