package Koha::AdditionalContents;

# Copyright ByWater Solutions 2015
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.

use Modern::Perl;

use Array::Utils qw( array_minus );

use Koha::Database;
use Koha::Exceptions;
use Koha::AdditionalContent;
use Koha::AdditionalContentsLocalizations;

use base qw(Koha::Objects);

=head1 NAME

Koha::AdditionalContents - Koha Additional content object set class

=head1 API

=head2 Class Methods

=cut

=head3 search_for_display

my $contents = Koha::AdditionalContents->search_for_display({
    category => 'news', # news or html_customizations
    location => 'slip',
    lang => 'es-ES',
    library_id => $branchcode
})

Return Koha::AdditionalContents set for display to user

You can limit the results by location, language and library by optional params

library_id should be valid branchcode of defined library

location is one of this:
- slip - for ISSUESLIP notice
- staff_only - for intranet
- opac_only - for OPAC
- staff_and_opac - for intranet and online catalogue
- OpacNavRight - Right column in the online catalogue
- opacheader
- OpacCustomSearch
- OpacMainUserBlock
- opaccredits
- OpacLoginInstructions
- OpacSuggestionInstructions
- ArticleRequestsDisclaimerText
- CookieConsentBar
- CookieConsentPopup

=cut

sub search_for_display {
    my ( $self, $params ) = @_;

    my $search_params;
    $search_params->{location}     = $params->{location};
    $search_params->{branchcode}   = $params->{library_id} ? [ $params->{library_id}, undef ] : undef;
    $search_params->{published_on} = { '<=' => \'CAST(NOW() AS DATE)' };
    $search_params->{-or}          = [
        expirationdate => { '>=' => \'CAST(NOW() AS DATE)' },
        expirationdate => undef
    ];
    $search_params->{category} = $params->{category} if $params->{category};

    my $contents = $self->SUPER::search( $search_params, { order_by => 'number' } );
    my @all_content_id = $contents->get_column('id');

    my ( $translated_contents, @translated_content_id );
    if ( $params->{lang} && $params->{lang} ne 'default' ) {
        $translated_contents = Koha::AdditionalContentsLocalizations->search(
            {
                additional_content_id => [$contents->get_column('id')],
                lang => $params->{lang},
            }
        );
        @translated_content_id = $translated_contents->get_column('additional_content_id');
    }

    my $default_contents    = Koha::AdditionalContentsLocalizations->search(
        {
            additional_content_id => [array_minus(@all_content_id, @translated_content_id)],
            lang                  => 'default',
        }
    );

    return Koha::AdditionalContentsLocalizations->search(
        {
            id => [ $translated_contents ? $translated_contents->get_column('id') : (), $default_contents->get_column('id') ]
        },
    );

}

=head3 find_best_match

    Koha::AdditionalContents->find_best_match({
        category => , location => , lang => , library_id =>
    });

    When choosing the best match, a match on lang and library is preferred.
    Next a match on library and default lang. Then match on All libs and lang.
    Finally a match with All libs and default lang.

=cut

sub find_best_match {
    my ( $self, $params ) = @_;
    my $library_id = $params->{library_id};
    my $lang = $params->{lang};

    my $contents = $self->SUPER::search({
        category => $params->{category},
        location => $params->{location},
        branchcode => [ $library_id, undef ],
    });

    my $rs = Koha::AdditionalContentsLocalizations->search({
            additional_content_id => [ $contents->get_column('id') ],
            lang => [ $lang, 'default' ],
        });

    # Pick the best
    my ( $alt1, $alt2, $alt3 );
    while( my $rec = $rs->next ) {
        return $rec if $library_id && $rec->branchcode && $rec->branchcode eq $library_id && $lang && $rec->lang eq $lang;
        $alt1 = $rec if !$alt1 && $library_id && $rec->branchcode && $rec->branchcode eq $library_id;
        $alt2 = $rec if !$alt2 && $lang && $rec->lang eq $lang;
        $alt3 = $rec if !$alt3;
    }
    return $alt1 // $alt2 // $alt3;
}

=head3 _type

=cut

sub _type {
    return 'AdditionalContent';
}

=head3 object_class

=cut

sub object_class {
    return 'Koha::AdditionalContent';
}

=head1 AUTHOR

Kyle M Hall <kyle@bywatersolutions.com>

=cut

1;
