package autumn_2023.module.complex;

import adf.core.agent.info.*;
import adf.core.component.module.complex.Search;
import adf.core.component.module.algorithm.Clustering;
import adf.core.agent.module.ModuleManager;
import adf.core.agent.develop.DevelopData;
import rescuecore2.worldmodel.EntityID;
import java.util.*;

public class AuctionFBSearch extends Search{
    private EntityID result;
    private Random random = new Random(1);

    private Clustering clustering;

    public AuctionFBSearch(
            AgentInfo ai, WorldInfo wi, ScenarioInfo si,
            ModuleManager mm, DevelopData dd)
    {
        super(ai, wi, si, mm, dd);
    
        this.clustering =mm.getModule("SampleSearch.Clustering.Fire",
                "adf.sample.module.algorithm.SampleKMeans");
        this.registerModule(this.clustering);
    }

    @Override
    public EntityID getTarget()
    {
        return this.result;
    }

    @Override
    public Search calc()
    {
        EntityID me = this.agentInfo.getID();
        int idx = this.clustering.getClusterIndex(me);
        Collection<EntityID> cluster = this.clustering.getClusterEntityIDs(idx);
            
        // 擬似乱数を用いてランダムに選択するために，Listに変換します
        List<EntityID> list = new ArrayList<>(cluster);
        
        // 擬似乱数を用いてランダムに選択します
        int n = list.size();
        int r = this.random.nextInt(n);
        this.result = list.get(r);
            
        return this;
    }
}
