/*
 * Copyright 2017 Red Hat, Inc. and/or its affiliates.
 *
 * 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.jbpm.workbench.pr.backend.server;

import java.util.function.Function;

import org.jbpm.workbench.ks.utils.KieServerUtils;
import org.jbpm.workbench.pr.model.ProcessNodeSummary;
import org.jbpm.workbench.pr.model.ProcessSummary;
import org.jbpm.workbench.pr.model.TimerSummary;
import org.kie.server.api.model.definition.ProcessDefinition;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;

public class ProcessSummaryMapper implements Function<ProcessDefinition, ProcessSummary> {

    @Override
    public ProcessSummary apply(final ProcessDefinition definition) {
        if (definition == null) {
            return null;
        }

        final ProcessSummary summary = new ProcessSummary(definition.getId(),
                                                          definition.getName(),
                                                          definition.getContainerId(),
                                                          definition.getVersion(),
                                                          definition.isDynamic());

        summary.setAssociatedEntities(definition.getAssociatedEntities());
        summary.setProcessVariables(definition.getProcessVariables());
        summary.setReusableSubProcesses(definition.getReusableSubProcesses());
        summary.setServiceTasks(definition.getServiceTasks());
        summary.setDynamicFormsEnabled(KieServerUtils.isKieServerRendererEnabled());
        summary.setNodes(definition.getNodes() == null ? emptyList() : definition.getNodes().stream().map(node -> new ProcessNodeSummary(node.getId(),
                                                                                                                                         node.getName(),
                                                                                                                                         node.getType(),
                                                                                                                                         node.getUniqueId())).collect(toList()));

        summary.setTimers(definition.getTimers() == null ? emptyList() : definition.getTimers().stream().map(timer -> new TimerSummary(timer.getId(),
                                                                                                                                       timer.getNodeId(),
                                                                                                                                       timer.getNodeName(),
                                                                                                                                       timer.getUniqueId())).collect(toList()));
        return summary;
    }
}
