# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# Copyright (C) 2021 Znuny GmbH, https://znuny.org/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --

package Kernel::Output::HTML::TicketOverviewMenu::Sort;

use strict;
use warnings;

use Kernel::System::VariableCheck qw(:all);

our @ObjectDependencies = (
    'Kernel::Config',
    'Kernel::Output::HTML::Layout',
    'Kernel::System::JSON',
    'Kernel::System::Log',
);

sub new {
    my ( $Type, %Param ) = @_;

    # allocate new hash for object
    my $Self = {};
    bless( $Self, $Type );

    # get needed params
    for my $Needed (qw(Action UserID)) {
        $Self->{$Needed} = $Param{$Needed} || die "Got no $Needed!";
    }

    return $Self;
}

sub Run {
    my ( $Self, %Param ) = @_;

    my $SortConfiguration = $Kernel::OM->Get('Kernel::Config')->Get('TicketOverviewMenuSort')->{SortAttributes}
        || {
        Age   => 1,
        Title => 1,
        };

    if ( !IsHashRefWithData($SortConfiguration) ) {
        $Kernel::OM->Get('Kernel::System::Log')->Log(
            Priority => 'error',
            Message  => "Wrong configuration 'TicketOverviewMenuSort###SortAttributes' for ticket"
                . " overview sort options.",
        );
        return;
    }

    # get layout object
    my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');

    my @SortData;
    my $SelectedSortByOption;
    for my $CurrentSortByOption ( sort keys %{$SortConfiguration} ) {

        # add separator
        if (@SortData) {
            push @SortData, {
                Key      => '-',
                Value    => '-------------------------',
                Disabled => 1,
            };
        }

        my $TranslatedValue =
            $LayoutObject->{LanguageObject}->Translate('Order by') . ' "' .
            $LayoutObject->{LanguageObject}->Translate($CurrentSortByOption) . '"';

        for my $CurrentOrderBy (qw(Down Up)) {

            my $Selected = 0;
            if (
                $CurrentSortByOption eq $Param{SortBy}
                && $CurrentOrderBy eq $Param{OrderBy}
                )
            {
                $Selected             = 1;
                $SelectedSortByOption = 1;
            }

            my $OrderByTranslation = $CurrentOrderBy eq 'Down' ? 'ascending' : 'descending';
            $OrderByTranslation = $LayoutObject->{LanguageObject}->Translate($OrderByTranslation);

            push @SortData, {
                Key      => "$CurrentSortByOption|$CurrentOrderBy",
                Value    => "$TranslatedValue ($OrderByTranslation)",
                Selected => $Selected,
            };
        }

    }

    return if !@SortData;

    my %ReturnData;
    $ReturnData{HTML} = $LayoutObject->BuildSelection(
        Data  => \@SortData,
        Name  => 'SortBy',
        Title => $LayoutObject->{LanguageObject}->Translate('Order by'),
        Class => 'Modernize',
    );

    return if !$ReturnData{HTML};

    # build redirect param hash for Core.App.InternalRedirect
    my %RedirectParams;
    $RedirectParams{Action} = $Self->{Action};
    for my $PossibleParam (qw(Filter)) {
        if ( $Param{$PossibleParam} ) {
            $RedirectParams{$PossibleParam} = $Param{$PossibleParam};
        }
    }

    if ( $Param{LinkFilter} ) {
        my @SplittedLinkFilters = split( /[;&]/, $Param{LinkFilter} );
        for my $CurrentLinkFilter ( sort @SplittedLinkFilters ) {
            my @KeyValue = split( /=/, $CurrentLinkFilter );
            if ( defined $KeyValue[1] ) {
                $RedirectParams{ $KeyValue[0] } = $KeyValue[1];
            }
        }
    }

    $RedirectParams{SortBy}  = 'Selection[0]';
    $RedirectParams{OrderBy} = 'Selection[1]';

    my $JSONObject               = $Kernel::OM->Get('Kernel::System::JSON');
    my $RedirectParamsJSONString = $JSONObject->Encode(
        Data => \%RedirectParams,
    );

    # Values of SortBy and OrderBy must be barewords, not strings, because they are evaluated
    # in JS. So, remove the quotes around them added by JSON encoding above.
    # "Selection[0]" becomes Selection[0].
    $RedirectParamsJSONString =~ s{"(Selection\[(?:0|1)\])"}{$1}g;

    $LayoutObject->AddJSOnDocumentComplete( Code => <<"JS" );
\$("#SortBy").change(function(){
    var Selection = \$(this).val().split('|');
    if ( Selection.length === 2 ) {
        Core.App.InternalRedirect($RedirectParamsJSONString);
    }
});
JS

    $ReturnData{HTML} = '<li class="AlwaysPresent SortBy">'
        . $ReturnData{HTML}
        . '</li>';

    $ReturnData{Block} = 'DocumentActionRowHTML';

    return \%ReturnData;
}

1;
