/*
 * Copyright 2003 - 2020 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.efaps.esjp.ui.rest;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.ws.rs.core.Response;

import org.efaps.admin.program.esjp.EFapsApplication;
import org.efaps.admin.program.esjp.EFapsUUID;
import org.efaps.admin.ui.AbstractCommand;
import org.efaps.admin.ui.AbstractMenu;
import org.efaps.admin.ui.Menu;
import org.efaps.esjp.ui.rest.dto.ActionDto;
import org.efaps.esjp.ui.rest.dto.ActionType;
import org.efaps.esjp.ui.rest.dto.NavItemDto;
import org.efaps.esjp.ui.util.WebApp;
import org.efaps.util.EFapsException;

@EFapsUUID("addb3347-919d-455a-8a82-39e0180471d8")
@EFapsApplication("eFaps-WebApp")
public abstract class NavController_Base
{

    public Response getMyDesk()
        throws EFapsException
    {
        final var toolbarUUID = WebApp.MAINTOOLBAR.get();
        final var menu = Menu.get(UUID.fromString(toolbarUUID));
        final List<NavItemDto> navItems = new ArrayList<>();
        for (final var command : menu.getCommands()) {
            ActionType actionType = null;
            if (command.getTargetTable() != null) {
                actionType = ActionType.GRID;
            }
            navItems.add(NavItemDto.builder()
                            .withId(command.getUUID().toString())
                            .withLabel(command.getLabelProperty())
                            .withChildren(addChildren(command))
                            .withAction(ActionDto.builder()
                                            .withType(actionType)
                                            .build())
                            .build());
        }
        final Response ret = Response.ok()
                        .entity(navItems)
                        .build();
        return ret;
    }

    protected List<NavItemDto> addChildren(final AbstractCommand _command)
    {
        final List<NavItemDto> ret = new ArrayList<>();
        if (_command instanceof AbstractMenu) {
            for (final var command : ((AbstractMenu ) _command).getCommands()) {
                ActionType actionType = null;
                if (command.getTargetTable() != null) {
                    actionType = ActionType.GRID;
                } else if (command.getTargetForm() != null) {
                    actionType = ActionType.FORM;
                } else if (command.getTargetSearch() != null) {
                    actionType = ActionType.SEARCH;
                }
                ret.add(NavItemDto.builder()
                            .withId(command.getUUID().toString())
                            .withLabel(command.getLabelProperty())
                            .withChildren(addChildren(command))
                            .withAction(ActionDto.builder()
                                            .withType(actionType)
                                            .build())
                            .build());
            }
        }
        return ret;
    }
}

@@ -888,16 +888,66 @@
 ommand;%0A
+import org.efaps.admin.ui.AbstractCommand.Target;%0A
 import o
@@ -2904,32 +2904,183 @@
 rm() != null) %7B%0A
+                    if (command.getTarget() == Target.MODAL) %7B%0A                        actionType = ActionType.MODAL;%0A                    %7D else %7B%0A    
                 
@@ -3113,16 +3113,38 @@
 e.FORM;%0A
+                    %7D%0A
         

Update NavController