package net.powermatcher.mock;

import java.util.HashMap;
import java.util.Map;
import java.util.Observer;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import net.powermatcher.api.Agent;
import net.powermatcher.api.AgentRole;
import net.powermatcher.api.Session;
import net.powermatcher.api.data.Bid;
import net.powermatcher.api.data.Price;
import net.powermatcher.api.monitoring.AgentEvent;
import net.powermatcher.api.monitoring.AgentObserver;
import net.powermatcher.api.monitoring.ObservableAgent;

public class MockAgent implements Agent, AgentRole, ObservableAgent {

    private Map<String, Object> agentProperties;
    private Price lastPriceUpdate;
    private Session session;
    private String desiredParentId;
    private String clusterId;

    /**
     * Collection of {@link Observer} services.
     */
    private final Set<AgentObserver> observers = new CopyOnWriteArraySet<AgentObserver>();

    public MockAgent(String agentId) {
        this.agentProperties = new HashMap<String, Object>();
        this.agentProperties.put("agentId", agentId);
    }

    @Override
    public void connectToMatcher(Session session) {
        this.session = session;
    }

    @Override
    public void matcherRoleDisconnected(Session session) {
        this.session = null;
    }

    @Override
    public void updatePrice(Price newPrice) {
        this.lastPriceUpdate = newPrice;
    }

    public void sendBid(Bid newBid) {
        this.session.updateBid(newBid);
    }

    public Price getLastPriceUpdate() {
        return lastPriceUpdate;
    }

    public Map<String, Object> getAgentProperties() {
        return agentProperties;
    }

    @Override
    public String getAgentId() {
        return (String) agentProperties.get("agentId");
    }

    @Override
    public String getClusterId() {
        return clusterId;
    }

    @Override
    public String getDesiredParentId() {
        return desiredParentId;
    }

    @Override
    public String getObserverId() {
        return this.getAgentId();
    }

    @Override
    public void addObserver(AgentObserver observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(AgentObserver observer) {
        observers.remove(observer);
    }

    public void publishEvent(AgentEvent event) {
        for (AgentObserver observer : observers) {
            observer.update(event);
        }
    }
}

@@ -774,20 +774,17 @@
 terId;%0A%0A
-    
+%09
 /**%0A    
@@ -1946,32 +1946,139 @@
 arentId;%0A    %7D%0A%0A
+    public void setDesiredParentId(String desiredParentId) %7B%0A%09%09this.desiredParentId = desiredParentId;%0A%09%7D%0A%0A
     @Override%0A  

fix core tests with new sessionManager