package steffen::MojoPlug::NavHelper;
use Mojo::Base 'Mojolicious::Plugin';

our $VERSION = '__VERSION__';

# Navigation Helper für Bootstrap 4,5 navbar
#
#  $self->plugin('steffen::MojoPlug::NavHelper' => { verbose => 0, bs => 5 });
#
#  $menuitadmin = [
#        {
#            text => 'Admin Tools', icon => 'fas fa-tools', priv => 'itadmin',
#            dropdown => [
#                { icon => 'fas fa-file-code', text => 'Powershell Mitarbeiter in AD', href => '/rfs/get_psscript_noadlogin'},
#            ]
#        },
#    ]
#
#   in html.ep
#   <ul class="navbar-nav">
#       <%== nav( $menuitadmin) %>
#   </ul>
#
# priv = Privilegienabfrage     benötigt das Plugin Authorization dort ist has_priv integriert
# icon = ein css Klassenicon    fontawesome funktioniert ganz gut
# navlinkclass = zusätzliche navlinkclasse zum markieren
# iconstyle = htmlstyle
# text = text des links
# textstyle = htmlstyle
# dropdown = Anfang eines Dropdowmenu
# href = Link wird im selben Fenster geöffnet
# xhref = Link wird auf in neuem Fenster geöffnet
# img = Bildurl

sub register {
    my ($self, $app, $args) = @_;
    my $verbose = $args->{verbose} || 0;
    my $bs = $args->{bs} || 4; # oder 5

    $app->helper(nav => sub {
        my ($c, $nav) = @_;

        my $html = '';
        foreach my $i (@{$nav}) {
            next if ($i->{priv} && ! $c->has_priv($i->{priv}));
            if ($i->{dropdown}) {
                $html .= '<li class="nav-item dropdown">';
                $html .= sprintf('<a href="#" class="nav-link dropdown-toggle %s"', $i->{navlinkclass} ? $i->{navlinkclass} :'').( $bs == 5 ? 'data-bs-toggle' : 'data-toggle' ) .'="dropdown" role="button" aria-haspopup="true" aria-expanded="false">';
                $html .= $i->{icon} ? sprintf('<span class="%s p%s-2" style="%s"></span>', $i->{icon}, ( $bs == 5 ? 'e' : 'r' ), ($i->{iconstyle} ? $i->{iconstyle} :'')) : '';
                $html .= sprintf('<span style="%s">%s</span>', ($i->{textstyle} ? $i->{textstyle} :''), $i->{text});
                $html .= '<span class="caret"></span></a> <ul class="dropdown-menu">';
                foreach my $d (@{$i->{dropdown}}) {
                    next if ($d->{priv} && ! $c->has_priv($d->{priv}));
                    if ($d->{xhref} ) {
                        $html .= sprintf('<li><a target="blank" class="dropdown-item form-control" href="%s">', ( $d->{xhref} =~ /http/ ? $d->{xhref} : $c->url_for($d->{xhref}) ) );
                    } elsif ($d->{href} ) {
                        $html .= sprintf('<li><a class="dropdown-item form-control" href="%s">', ( $d->{href} =~ /http/ ? $d->{href} : $c->url_for($d->{href}) ) | '' );
                    }
                    $html .= $d->{icon} ? sprintf('<span class="%s" style="%s"></span>', $d->{icon}, ($d->{iconstyle} ? $d->{iconstyle} :'') ) : '';
                    $html .= $d->{img} ? sprintf('<img height="18px" src="%s">', ( $d->{img} =~ /http/ ? $d->{img} : $c->url_for($d->{img}) ) ) : '';
                    $html .= ( $bs == 5 ? '<span class="ps-2">' : '<span class="pl-2">' );
                    $html .= sprintf('<span style="%s">%s</span>', ($d->{textstyle} ? $d->{textstyle} :''), $d->{text});
                    $html .= '</span></a></li>';
                }
                $html .= '</ul></li> ';
            } else {
                $html .= sprintf('<li><a class="nav-link %s" href="%s">', $i->{navlinkclass} ? $i->{navlinkclass} :'', ( $i->{href} =~ /http/ ? $i->{href} : $c->url_for($i->{href}) ) );
                $html .= $i->{img} ? sprintf('<img height="18px" src="%s">', ( $i->{img} =~ /http/ ? $i->{img} : $c->url_for($i->{img}) )    ) : '';
                $html .= $i->{icon} ? sprintf('<span class="%s" style="%s"></span>', $i->{icon}, ($i->{iconstyle} ? $i->{iconstyle} :'')) : '';
                $html .= ( $bs == 5 ? '<span class="ps-2">' : '<span class="pl-2">' );
                $html .= sprintf('<span style="%s">%s</span>', ($i->{textstyle} ? $i->{textstyle} :''), $i->{text});
                $html .= '</span></a></li>';
            }
        }
        return $html;
    });

    $app->log->info(sprintf('REGISTERED: %s %s', __PACKAGE__, q$Revision: 411 $)) if $verbose;
}

1;
