<%INIT>
my $request_path = $HTML::Mason::Commands::r->path_info;
$request_path =~ s!/{2,}!/!g;

my $query_string = sub {
    my %args = @_;
    my $u    = URI->new();
    $u->query_form(map { $_ => $args{$_} } sort keys %args);
    return $u->query;
};

#warn keys %session;

my $assets;
eval {
    $assets = Menu->child(  assets => title => loc('Assets'), path => '/AssetTracker/index.html', sort_order => 3 );
};
if ($@) {
    return;
}

$assets->child( new => title => loc('New Search') => path => "/AssetTracker/Search/Build.html?NewQuery=1" );

if ( $request_path =~ m{^/AssetTracker/} ) {
    PageWidgets()->delete('simple_search');
    PageWidgets()->delete('create_ticket');
    PageWidgets()->child( AT_simple_search => raw_html => $m->scomp('/AssetTracker/Elements/SimpleSearch') );
    PageWidgets()->child( create_asset => raw_html => $m->scomp('/AssetTracker/Elements/CreateAsset') );
}

    if ( $request_path =~ m{^/AssetTracker/Asset/} ) {
        if ( ( $DECODED_ARGS->{'id'} || '' ) =~ /^(\d+)$/ ) {
            my $id  = $1;
            my $obj = RTx::AssetTracker::Asset->new( $session{'CurrentUser'} );
            $obj->Load($id);

            if ( $obj and $obj->id ) {
                my $actions = PageMenu()->child( actions => title => loc('Actions'), sort_order  => 95 );
                my $tabs = PageMenu();
                $tabs->child( display => title => loc('Display') => path => "/AssetTracker/Asset/Display.html?id=" . $id );
                $tabs->child( history => title => loc('History') => path => "/AssetTracker/Asset/History.html?id=" . $id );

                my %can = %{ $obj->CurrentUser->PrincipalObj->HasRights( Object => $obj ) };
                $can{'_ModifyOwner'} = $can{'OwnAsset'};
                my $can = sub {
                    unless ($_[0] eq 'ExecuteCode') {
                        return $can{$_[0]} || $can{'SuperUser'};
                    } else {
                        return !RT->Config->Get('DisallowExecuteCode')
                            && ( $can{'ExecuteCode'} || $can{'SuperUser'} );
                    }
                };

                # comment out until we can do it for an individual custom field
                #if ( $can->('ModifyAsset') || $can->('ModifyCustomField') ) {
                $tabs->child( basics => title => loc('Basics'), path => "/AssetTracker/Asset/Modify.html?id=" . $id, );

                #}

                if ( $can->('ModifyAsset') || $can->('_ModifyOwner') || $can->('Watch') || $can->('WatchAsAdminCc') ) {
                    $tabs->child( people => title => loc('People'), path => "/AssetTracker/Asset/ModifyPeople.html?id=" . $id,);
                }

                if ( $can->('ModifyAsset') && RT->Config->Get('EnableIP') ) {
                    $tabs->child( ips => title => loc('IPs'), path => "/AssetTracker/Asset/ModifyIPs.html?id=" . $id, );
                }
                if ( $can->('ModifyAsset') ) {
                    $tabs->child( fields => title => loc('Fields'), path => "/AssetTracker/Asset/ModifyFields.html?id=" . $id, );
                    $tabs->child( links => title => loc('Links'), path => "/AssetTracker/Asset/ModifyLinks.html?id=" . $id, );
                }

                #if ( $can->('ModifyAsset') || $can->('ModifyCustomField') || $can->('_ModifyOwner') ) {
                $tabs->child( jumbo => title => loc('Jumbo'), path => "/AssetTracker/Asset/ModifyAll.html?id=" . $id, );
                #}

                my $hide_resolve_with_deps = RT->Config->Get('HideResolveActionsWithDependencies')
                    && $obj->HasUnresolvedDependencies;

                my $current   = $obj->Status;
                my $lifecycle = $obj->TypeObj->Lifecycle;
                my $i         = 1;
                foreach my $info ( $lifecycle->Actions($current) ) {
                    my $next = $info->{'to'};
                    next unless $lifecycle->IsTransition( $current => $next );

                    my $check = $lifecycle->CheckRight( $current => $next );
                    next unless $can->($check);

                    next if $hide_resolve_with_deps
                        && $lifecycle->IsInactive($next)
                        && !$lifecycle->IsInactive($current);

                    my $url = '/AssetTracker/Asset/Display.html?'
                        . $query_string->(
                        Status => $next,
                        id     => $id,
                    );
                    my $key = $info->{'label'} || ucfirst($next);
                    $actions->child( $key => title => loc( $key ), path => $url);
                }

                #AT removed ticket taking/stealing

                if ( defined $session{"assets"} ) {
                    # we have to update session data if we get new ItemMap
                    my $updatesession = 1 unless ( $session{"assets"}->{'item_map'} );

                    my $item_map = $session{"assets"}->ItemMap;

                    if ($updatesession) {
                        $session{"assets"}->PrepForSerialization();
                    }

                    my $search = $assets; #Menu()->child('search');
                    # Don't display prev links if we're on the first ticket
                    if ( $item_map->{$id}->{prev} ) {
                        $search->child( first =>
                            title => '<< ' . loc('First'), class => "nav", path => "/AssetTracker/Asset/Display.html?id=" . $item_map->{first});
                        $search->child( prev => title => '< ' . loc('Prev'),   class => "nav", path => "/AssetTracker/Asset/Display.html?id=" . $item_map->{$id}->{prev});
                    }
                    # Don't display next links if we're on the last ticket
                    if ( $item_map->{$id}->{next} ) {
                        $search->child( next =>
                            title => loc('Next') . ' >',  class => "nav", path => "/AssetTracker/Asset/Display.html?id=" . $item_map->{$id}->{next});
                        $search->child( last =>
                            title => loc('Last') . ' >>', class => "nav", path => "/AssetTracker/Asset/Display.html?id=" . $item_map->{last});
                    }
                }
            }
        }
    }

    if (
        $request_path =~ m{^/AssetTracker/(?:Asset|Search)/}
        || ( $request_path =~ m{^/AssetTracker/index\.html}
            && $DECODED_ARGS->{'q'} )
      )
    {
        my $search = $assets; #->child('search');
        my $args      = '';
        my $has_query = '';
        my $current_search = $session{"CurrentAssetSearchHash"} || {};
        my $search_id = $DECODED_ARGS->{'SavedSearchLoad'} || $DECODED_ARGS->{'SavedSearchId'} || $search->{'SearchId'} || '';

        $has_query = 1 if ( $DECODED_ARGS->{'Query'} or $current_search->{'Query'} );

        my %query_args = (
            SavedSearchId => ( $search_id eq 'new' ) ? undef : $search_id,
            SavedChartSearchId => $DECODED_ARGS->{'SavedChartSearchId'} || $current_search->{SavedChartSearchId},
            Query => $DECODED_ARGS->{'Query'} || $current_search->{'Query'},
            Format => $DECODED_ARGS->{'Format'} || $current_search->{'Format'},
            OrderBy => $DECODED_ARGS->{'OrderBy'} || $current_search->{'OrderBy'} ||'',
            Order => $DECODED_ARGS->{'Order'} || $current_search->{'Order'} ||'',
            Page => $DECODED_ARGS->{'Page'} || $current_search->{'Page'},
            RowsPerPage => ( defined $DECODED_ARGS->{'RowsPerPage'}
                             ? $DECODED_ARGS->{'RowsPerPage'}
                             : $current_search->{'RowsPerPage'})
                         );
        for my $field (qw(Order OrderBy)) {
            if ( ref( $query_args{$field} ) eq 'ARRAY' ) {
                $query_args{$field} = join( "|", @{ $query_args{$field} } );
            } elsif (not defined $query_args{$field}) {
                delete $query_args{$field};
            }
        }

        $args = "?" . ($QueryString || $query_string->(%query_args));


        my $current_search_menu;
        if ( $request_path =~ m{^/AssetTracker/Asset} ) {
            $current_search_menu = $search->child( current_search => title => loc('Current Search') );
            $current_search_menu->path("/AssetTracker/Search/Results.html$args") if $has_query;
        } else {
            $current_search_menu = PageMenu();
        }

        $current_search_menu->child( edit_search =>
            title => loc('Edit Search'), path => "/AssetTracker/Search/Build.html" . ( ($has_query) ? $args : '' ) );
        $current_search_menu->child( advanced =>
            title => loc('Advanced'),    path => "/AssetTracker/Search/Edit.html$args" );
        if ($has_query) {
            $current_search_menu->child( results => title => loc('Show Results'), path => "/AssetTracker/Search/Results.html$args" );
        }

        if ( $has_query ) {
            my $bulk = $current_search_menu->child( bulk => title => loc('Bulk Update'), path => "/AssetTracker/Search/Bulk.html$args" );
            $bulk->child( batch => title => loc('Batch Update multiple assets'), path => "/AssetTracker/Search/Bulk.html$args" );
            $bulk->child( grid  => title => loc('Grid Update multiple assets'),  path => "/AssetTracker/Search/Grid.html$args" );

            my $more = $current_search_menu->child( more => title => loc('Feeds') );

            $more->child( spreadsheet => title => loc('Spreadsheet'), path => "/AssetTracker/Search/Results.tsv$args" );
            $more->child( importable => title => loc('Importable Spreadsheet'), path => "/AssetTracker/Search/Export.html$args" );

            if ($request_path =~ m{^/AssetTracker/Search/Results.html}
                &&                        #XXX TODO better abstraction
                $session{'CurrentUser'}->HasRight( Right => 'SuperUser', Object => RT->System )) {
                my $shred_args = $query_string->(
                    Search         => 1,
                    Plugin         => 'Assets',
                    'Assets:query' => $query_args{'Query'},
                    'Assets:limit' => $query_args{'Rows'},
                );

                $more->child( shredder => title => loc('Shredder'), path => '/Admin/Tools/Shredder/?' . $shred_args);
            }

        }
    }

    if ( $request_path =~ m{^/AssetTracker/Admin/Types} ) {
        my $type = 'Types';
        my $tabs = PageMenu();

        my $section;
        if ( $request_path =~ m{^/AssetTracker/Admin/$type/?(?:index.html)?$}
             || (    $request_path =~ m{^/AssetTracker/Admin/$type/(?:Modify.html)$}
                  && $DECODED_ARGS->{'Create'} )
           )

        {
            $section = $tabs;

        } else {
            $section = $tabs->child( select => title => loc('Asset Types'),
                                     path => "/AssetTracker/Admin/$type/" );
        }

        $section->child( select => title => loc('Select'),
                         path   => "/AssetTracker/Admin/$type/" );
        $section->child( create => title => loc('Create'),
                         path => "/AssetTracker/Admin/$type/Modify.html?Create=1" );
    }

    if ( $request_path =~ m{^/AssetTracker/Admin/Types} ) {
        if ( $DECODED_ARGS->{'id'} && $DECODED_ARGS->{'id'} =~ /^\d+$/
                ||
              $DECODED_ARGS->{'Type'} && $DECODED_ARGS->{'Type'} =~ /^\d+$/
                ||
              $DECODED_ARGS->{'AssetType'} && $DECODED_ARGS->{'AssetType'} =~ /^\d+$/
                ) {
            my $id = $DECODED_ARGS->{'Type'} || $DECODED_ARGS->{'AssetType'} || $DECODED_ARGS->{'id'};
            my $type_obj = RTx::AssetTracker::Type->new( $session{'CurrentUser'} );
            $type_obj->Load($id);

            if ( $type_obj and $type_obj->id ) {
                my $type = PageMenu();
                $type->child( basics => title => loc('Basics'),   path => "/AssetTracker/Admin/Types/Modify.html?id=" . $id );
                $type->child( people => title => loc('Watchers'), path => "/AssetTracker/Admin/Types/People.html?id=" . $id );

                my $templates = $type->child(templates => title => loc('Templates'), path => "/AssetTracker/Admin/Types/Templates.html?id=" . $id);
                $templates->child( select => title => loc('Select'), path => "/AssetTracker/Admin/Types/Templates.html?id=".$id);
                $templates->child( create => title => loc('Create'), path => "/AssetTracker/Admin/Types/Template.html?Create=1;AssetType=".$id);

                my $scrips = $type->child( scrips => title => loc('Scrips'), path => "/AssetTracker/Admin/Types/Scrips.html?id=" . $id);
                $scrips->child( select => title => loc('Select'), path => "/AssetTracker/Admin/Types/Scrips.html?id=" . $id );
                $scrips->child( create => title => loc('Create'), path => "/AssetTracker/Admin/Types/Scrip.html?Create=1;AssetType=" . $id);

                my $ticket_cfs = $type->child( 'asset-custom-fields' => title => loc('Asset Custom Fields'),
                    path => '/AssetTracker/Admin/Types/CustomFields.html?SubType=RTx::AssetTracker::Asset&id=' . $id );

#                my $txn_cfs = $type->child( 'transaction-custom-fields' => title => loc('Transaction Custom Fields'),
#                    path => '/AssetTracker/Admin/Types/CustomFields.html?SubType=RTx::AssetTracker::Asset-RT::Transaction&id='.$id );

                $type->child( 'group-rights' => title => loc('Group Rights'), path => "/AssetTracker/Admin/Types/GroupRights.html?id=".$id );
                $type->child( 'user-rights' => title => loc('User Rights'), path => "/AssetTracker/Admin/Types/UserRights.html?id=" . $id );
                $type->child( 'history' => title => loc('History'), path => "/AssetTracker/Admin/Types/History.html?id=" . $id );

            }
        }
    }

    if ( $request_path =~ m{^/AssetTracker/Admin/Global/(Scrip|Template)s?\.html} ) {
        my $type = $1;
        my $tabs = PageMenu();

        # With only two elements, swapping between dropdown and menu is kinda dumb
        # In the glorious future this should be cleaner.

        $tabs->child( select => title => loc('Select'), path => "/AssetTracker/Admin/Global/${type}s.html" );
        $tabs->child( create => title => loc('Create'), path => "/AssetTracker/Admin/Global/${type}.html?Create=1" );
    }


    my $tools = Menu->child("tools");

    if ( $session{'CurrentUser'}->HasRight( Right => 'AssetImport', Object => RT->System ) ) {
        $tools->child( asset_import =>
            title       => loc('Asset Import'),
            description => loc('Import assets from an Excel 94 XML formatted file'),
            path        => '/AssetTracker/Tools/Import.html',
        );
    }

    if ( my $admin = $tools->child("config") ) {
        my $types = $admin->child( 'asset-types' => title => loc('Asset Types'), path => '/AssetTracker/Admin/Types/', description => loc('Manage asset types and asset-specific properties') );
        $types->child( select => title => loc('Select'), path => "/AssetTracker/Admin/Types/index.html" );
        $types->child( create => title => loc('Create'), path => "/AssetTracker/Admin/Types/Modify.html?Create=1" );

        my $admin_global = $admin->child("global");

        my $scrips = $admin_global->child( at_scrips =>
            title       => loc('AT Scrips'),
            description => loc('Modify scrips which apply to all asset types'),
            path        => '/AssetTracker/Admin/Global/Scrips.html',
        );
        $scrips->child( select => title => loc('Select'), path => "/AssetTracker/Admin/Global/Scrips.html" );
        $scrips->child( create => title => loc('Create'), path => "/AssetTracker/Admin/Global/Scrip.html?Create=1" );

        my $templates = $admin_global->child( at_templates =>
            title       => loc('AT Templates'),
            description => loc('Edit system templates for asset tracker'),
            path        => '/AssetTracker/Admin/Global/Templates.html',
        );
        $templates->child( select => title => loc('Select'), path => "/AssetTracker/Admin/Global/Templates.html" );
        $templates->child( create => title => loc('Create'), path => "/AssetTracker/Admin/Global/Template.html?Create=1" );

        my $cfadmin = $admin_global->child("custom-fields");
        $cfadmin->child( types =>
            title       => loc('Asset Types'),
            description => loc('Select custom fields for all asset types'),
            path        => '/AssetTracker/Admin/Global/CustomFields/Types.html');
        $cfadmin->child( assets =>
            title       => loc('Assets'),
            description => loc('Select custom fields for assets of all types'),
            path        => '/AssetTracker/Admin/Global/CustomFields/Type-Assets.html',
        );
    }

</%INIT>
<%ARGS>
$show_menu => 1
$QueryString => ''
</%ARGS>
