/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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.jboss.pnc.bpm;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.jboss.pnc.common.json.moduleconfig.BpmModuleConfig;
import org.jboss.pnc.rest.restmodel.bpm.BpmNotificationRest;
import org.jboss.pnc.spi.exception.CoreException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Consumer;

import static java.util.Objects.requireNonNull;

/**
 * Parent type of all BPM tasks. Task is a representation of
 * BPM process execution, keeps track of the remote process instance
 * and handles notifications.
 * Class implements Comparable based on the taskId,
 * so they can be sorted at REST endpoint.
 * BPM tasks do not have to be thread safe.
 *
 * @author Jakub Senko
 */
@EqualsAndHashCode(of = "taskId")
@ToString
public abstract class BpmTask implements Comparable<BpmTask> {

    private static final Logger log = LoggerFactory.getLogger(BpmTask.class);

    protected static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * This is an internal identifier, not one provided by BPM server.
     */
    private Integer taskId;

    private Long processInstanceId;

    @Getter
    private final List<BpmNotificationRest> events = new ArrayList<>();

    private String processName;

    protected BpmModuleConfig config;

    private ConcurrentMap<BpmEventType, List<Consumer<?>>> listeners = new ConcurrentHashMap<>();

    /**
     * Users OAuth token used to authenticate requests on remote services
     */
    private String accessToken;

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getProcessName() {
        return processName;
    }

    /* package */ void setProcessName(String processName) {
        this.processName = processName;
    }

    /**
     * Get the name of the BPM process for this task.
     * Warning: The process MUST be asynchronous so it
     * does not block BPM manager.
     */
    protected abstract String getProcessId();

    /**
     * Provide parameters to the BPM process.
     * The JSON representation of this map will be available
     * in the BPM process as "processParameters" variable.
     * Some parameters, such as "taskId" are automatically added,
     * not into the "processParameters" variable, but 'one level higher'
     * and directly accessible
     * from process variables.
     *
     * @return a map of process parameters
     * @throws CoreException
     */
    protected abstract Serializable getProcessParameters() throws CoreException;

    /* package */ void setBpmConfig(BpmModuleConfig config) {
        this.config = config;
    }

    public Integer getTaskId() {
        return taskId;
    }

    /* package */ void setTaskId(int taskId) {
        this.taskId = taskId;
    }

    public Long getProcessInstanceId() {
        return processInstanceId;
    }

    /* package */ void setProcessInstanceId(Long processInstanceId) {
        this.processInstanceId = processInstanceId;
    }

    /**
     * Listen to notifications from BPM process for this task.
     *
     * @param eventType event to follow
     */
    public <T extends BpmNotificationRest> void addListener(BpmEventType eventType, Consumer<T> listener) {
        List<Consumer<?>> consumers = listeners.computeIfAbsent(eventType, (k) -> new ArrayList<>());
        consumers.add(listener);
    }


    public <T extends BpmNotificationRest> void notify(BpmEventType eventType, T data) {
        List<Consumer<?>> listeners = this.listeners.computeIfAbsent(eventType, (k) -> new ArrayList<>());
        log.debug("will notify bpm listeners for eventType: {}, matching listeners: {}, all listeners: {}", eventType, listeners, this.listeners);
        listeners.forEach(listener -> {
            // Cast is OK because there is no unchecked method declaration to put wrong types
            ((Consumer<T>) listener).accept(data);
        });
        events.add(data);
    }

    /**
     * Extend process parameters from the task with additional useful information,
     * such as pncBaseUrl and taskId, needed for notifications.
     * Before use, taskId MUST be assigned.
     *
     * @throws CoreException
     */
    Map<String, Object> getExtendedProcessParameters() throws CoreException {
        Serializable processParameters = getProcessParameters();
        requireNonNull(processParameters);
        Map<String, Object> actualParameters = new HashMap<>();
        try {
            actualParameters.put("processParameters", MAPPER.writeValueAsString(processParameters));
        } catch (JsonProcessingException e) {
            throw new CoreException("Could not serialize process processParameters '" +
                    processParameters + "'.", e);
        }

        //global not process related parameters
        actualParameters.put("taskId", taskId);
        actualParameters.put("usersAuthToken", accessToken);

        return actualParameters;
    }

    @Override
    public int compareTo(BpmTask other) {
        requireNonNull(other);
        if (taskId == null)
            return other.getTaskId() == null ? 0 : -1;
        if (other.getTaskId() == null)
            return 1;
        return taskId.compareTo(other.getTaskId());
    }
}

@@ -1822,18 +1822,8 @@
 d%22)%0A
-@ToString%0A
 publ
@@ -6289,11 +6289,430 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public String toString() %7B%0A        return %22BpmTask%7B%22 +%0A                %22taskId=%22 + taskId +%0A                %22, processInstanceId=%22 + processInstanceId +%0A                %22, events=%22 + events +%0A                %22, processName='%22 + processName + '%5C'' +%0A                %22, config=%22 + config +%0A                %22, listeners=%22 + listeners +%0A                %22, accessToken='***'%22 +%0A                '%7D';%0A    %7D
 %0A%7D%0A

Update toString methods not to print tokens