/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myagent.attentioncodelets;

import edu.memphis.ccrg.lida.attentioncodelets.AttentionCodeletImpl;
import edu.memphis.ccrg.lida.framework.FrameworkModule;
import edu.memphis.ccrg.lida.framework.shared.Link;
import edu.memphis.ccrg.lida.framework.shared.Linkable;
import edu.memphis.ccrg.lida.framework.shared.Node;
import edu.memphis.ccrg.lida.framework.shared.NodeStructure;
import edu.memphis.ccrg.lida.framework.shared.NodeStructureImpl;
import edu.memphis.ccrg.lida.workspace.workspacebuffers.WorkspaceBuffer;
import myagent.modules.AttentionCodeletModuleImpl;

/**
 *
 * @author Marcel
 */
public class NewFeatureAttentionCodelet extends AttentionCodeletImpl{
    
    protected AttentionCodeletModuleImpl attentionModule;
    
    @Override
    public void init(){
        super.init();
        double baseLevelActivation = (Double) getParam("baseLevelActivation", 0.0);
        setBaseLevelActivation(baseLevelActivation);
        this.attentionModule.addCodelet(this);
    }
    
    @Override
    public NodeStructure retrieveWorkspaceContent(WorkspaceBuffer buffer) {
        NodeStructure model = (NodeStructure) buffer.getBufferContent(null);
        Linkable chosenLinkable = null;
        
        double winnerActivation = 0.0;
        
        for (Linkable ln : model.getLinkables()) {
            double linkableActivation = ln.getActivation();            
            
            if(linkableActivation > winnerActivation && 
                    !attentionModule.hasCodeletSeekingFor(ln)){
                chosenLinkable = ln;
                winnerActivation = linkableActivation;
            }
        }
        
        return linkableToNodeStructure(chosenLinkable);
        
    }
    
    protected NodeStructure linkableToNodeStructure(Linkable ln){
        NodeStructureImpl ns = new NodeStructureImpl();
        
        if(ln instanceof Node) ns.addDefaultNode((Node) ln);
        else if (ln instanceof Link) ns.addDefaultLink((Link) ln);
        else {
            System.out.println("INVALID LINKABLE " + ln);
            return null;
        }
        
        return ns;
    }
    
    @Override
    public void setAssociatedModule(FrameworkModule module, String usage) {
        
        if (module instanceof AttentionCodeletModuleImpl) {
            attentionModule = (AttentionCodeletModuleImpl) module;
        }
        else super.setAssociatedModule(module, usage);
    }
    
    @Override
    public boolean bufferContainsSoughtContent(WorkspaceBuffer buffer) {
        NodeStructure model = (NodeStructure) buffer.getBufferContent(null);
        for (Linkable ln : model.getLinkables()) {
            if(!attentionModule.hasCodeletSeekingFor(ln)){
                return true;
            }
        }
        return false;
    }    
}
