/*
 * Copyright 2014 Guillaume Masclet <guillaume.masclet@yahoo.fr>.
 *
 * 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.elasticlib.node.components;

import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseEntry;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.elasticlib.common.hash.Guid;
import org.elasticlib.common.model.AgentInfo;
import org.elasticlib.common.model.ReplicationDef;
import static org.elasticlib.node.manager.storage.DatabaseEntries.entry;
import org.elasticlib.node.manager.storage.StorageManager;
import org.elasticlib.node.repository.Agent;
import org.elasticlib.node.repository.Repository;

/**
 * Manages replication agents.
 */
public class ReplicationAgentsPool {

    private static final String REPLICATION_CUR_SEQS = "replicationCurSeqs";

    private final StorageManager storageManager;
    private final RepositoriesProvider repositoriesProvider;
    private final Map<Guid, Agent> agents = new HashMap<>();
    private Database curSeqsDb;

    /**
     * Constructor.
     *
     * @param storageManager Persistent storage provider.
     * @param repositoriesProvider Repositories provider.
     */
    public ReplicationAgentsPool(StorageManager storageManager, RepositoriesProvider repositoriesProvider) {
        this.storageManager = storageManager;
        this.repositoriesProvider = repositoriesProvider;
    }

    /**
     * Initializes the pool.
     */
    public void start() {
        curSeqsDb = storageManager.openDeferredWriteDatabase(REPLICATION_CUR_SEQS);
    }

    /**
     * Stops all agents.
     */
    public void stop() {
        agents.values().forEach(Agent::stop);
        agents.clear();
    }

    /**
     * Creates and (if possible) starts a new agent.
     *
     * @param def Definition of the related replication.
     */
    public void createAgent(ReplicationDef def) {
        // Ensures agent won't see a stale value.
        curSeqsDb.delete(null, entry(def.getGuid()));
        tryStartAgent(def);
    }

    /**
     * Starts a new agent. Does nothing if agent is already started.
     *
     * @param def Definition of the related replication.
     */
    public void startAgent(ReplicationDef def) {
        if (agents.containsKey(def.getGuid())) {
            return;
        }
        startAgent(def.getGuid(),
                   repositoriesProvider.getRepository(def.getSource()),
                   repositoriesProvider.getRepository(def.getDestination()));
    }

    /**
     * Starts a new agent if its source and destination repositories are available. Does nothing if agent is already
     * started.
     *
     * @param def Definition of the related replication.
     */
    public void tryStartAgent(ReplicationDef def) {
        if (agents.containsKey(def.getGuid())) {
            return;
        }
        Optional<Repository> source = repositoriesProvider.tryGetRepository(def.getSource());
        Optional<Repository> destination = repositoriesProvider.tryGetRepository(def.getDestination());
        if (!source.isPresent() || !destination.isPresent()) {
            return;
        }
        startAgent(def.getGuid(), source.get(), destination.get());
    }

    private void startAgent(Guid guid, Repository source, Repository destination) {
        DatabaseEntry curSeqKey = entry(guid);
        Agent agent = new ReplicationAgent(source, destination, curSeqsDb, curSeqKey);

        agents.put(guid, agent);
        agent.start();
    }

    /**
     * Stops an agent. Does nothing if agent is already stopped.
     *
     * @param guid GUID of the related replication.
     */
    public void stopAgent(Guid guid) {
        if (!agents.containsKey(guid)) {
            return;
        }
        agents.remove(guid).stop();
    }

    /**
     * Stops and deletes an agent.
     *
     * @param guid GUID of the related replication.
     */
    public void deleteAgent(Guid guid) {
        stopAgent(guid);

        // Can't use a transaction on a deffered write database :(
        curSeqsDb.delete(null, entry(guid));
    }

    /**
     * Signals an agent. Does nothing if agent is not started.
     *
     * @param guid GUID of the related replication.
     */
    public void signalAgent(Guid guid) {
        if (!agents.containsKey(guid)) {
            return;
        }
        agents.get(guid).signal();
    }

    /**
     * Provides info about an existing agent, if it is started.
     *
     * @param guid GUID of the related replication.
     * @return Associated agent info, if any.
     */
    public Optional<AgentInfo> tryGetAgentInfo(Guid guid) {
        if (!agents.containsKey(guid)) {
            return Optional.empty();
        }
        return Optional.of(agents.get(guid).info());
    }
}

@@ -900,16 +900,63 @@
 ntInfo;%0A
+import org.elasticlib.common.model.AgentState;%0A
 import o
@@ -2801,34 +2801,25 @@
     if (
-agents.containsKey
+isStarted
 (def.get
@@ -3327,34 +3327,25 @@
     if (
-agents.containsKey
+isStarted
 (def.get
@@ -3747,24 +3747,287 @@
 ());%0A    %7D%0A%0A
+    private boolean isStarted(Guid guid) %7B%0A        if (!agents.containsKey(guid)) %7B%0A            return false;%0A        %7D%0A        AgentState state = agents.get(guid).info().getState();%0A        return state != AgentState.STOPPED && state != AgentState.ERROR;%0A    %7D%0A%0A
     private 
@@ -4232,32 +4232,49 @@
 eqKey);%0A%0A       
+ Agent previous =
  agents.put(guid
@@ -4283,16 +4283,87 @@
 agent);%0A
+        if (previous != null) %7B%0A            previous.stop();%0A        %7D%0A
         

Enable a stopped replication agent to be restarted