/*
 * Copyright (c) 2022-2023 nosqlbench
 *
 * 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 io.nosqlbench.engine.core.lifecycle.activity;

import io.nosqlbench.nb.api.engine.activityimpl.ActivityDef;
import io.nosqlbench.nb.api.components.core.NBComponent;
import io.nosqlbench.engine.api.activityapi.core.Activity;
import io.nosqlbench.engine.api.activityapi.core.ActivitiesAware;
import io.nosqlbench.engine.api.activityapi.core.ActionDispenser;
import io.nosqlbench.engine.api.activityapi.core.MotorDispenser;
import io.nosqlbench.engine.api.activityapi.input.InputDispenser;
import io.nosqlbench.engine.api.activityapi.output.OutputDispenser;
import io.nosqlbench.engine.api.activityimpl.CoreServices;
import io.nosqlbench.engine.api.activityimpl.motor.CoreMotorDispenser;
import io.nosqlbench.engine.api.activityimpl.uniform.StandardActionDispenser;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Consolidates the activity type and activity instantiation logic into one place
 * per scope. Within the lifetime of this ActivityLoader, all activities may
 * see each other by name.
 */
public class ActivityLoader {
    private static final Logger logger = LogManager.getLogger("ACTIVITIES");
    private final Map<String, Activity> activityMap = new ConcurrentHashMap<>();

    public ActivityLoader() {
    }

    public synchronized Activity loadActivity(ActivityDef activityDef, final NBComponent parent) {
        activityDef = activityDef.deprecate("yaml", "workload").deprecate("type", "driver");

        // Create the activity instance directly (using Activity for compatibility)
        final Activity activity = new Activity(parent, activityDef);

        // Assemble the dispensers like StandardActivityType did
        final InputDispenser inputDispenser = CoreServices.getInputDispenser(activity);
        if (inputDispenser instanceof ActivitiesAware) {
            ((ActivitiesAware) inputDispenser).setActivitiesMap(activityMap);
        }
        activity.setInputDispenser(inputDispenser);

        final ActionDispenser actionDispenser = new StandardActionDispenser(activity);
        if (actionDispenser instanceof ActivitiesAware) {
            ((ActivitiesAware) actionDispenser).setActivitiesMap(activityMap);
        }
        activity.setActionDispenser(actionDispenser);

        final OutputDispenser outputDispenser = CoreServices.getOutputDispenser(activity).orElse(null);
        if ((null != outputDispenser) && (outputDispenser instanceof ActivitiesAware)) {
            ((ActivitiesAware) outputDispenser).setActivitiesMap(activityMap);
        }
        activity.setOutputDispenser(outputDispenser);

        final MotorDispenser<?> motorDispenser = new CoreMotorDispenser<>(activity, inputDispenser, actionDispenser, outputDispenser);
        if (motorDispenser instanceof ActivitiesAware) {
            ((ActivitiesAware) motorDispenser).setActivitiesMap(activityMap);
        }
        activity.setMotorDispenser(motorDispenser);

        this.activityMap.put(activity.getAlias(), activity);
        ActivityLoader.logger.debug("Resolved activity for alias '{}'", activityDef.getAlias());
        return activity;
    }

    public void purgeActivity(final String activityAlias) {
        activityMap.remove(activityAlias);
    }
}
