package autumn_2023.centralized;

import adf.core.agent.communication.standard.bundle.centralized.CommandFire;
import adf.core.agent.communication.standard.bundle.centralized.CommandScout;
import adf.core.agent.develop.DevelopData;
import adf.core.agent.info.AgentInfo;
import adf.core.agent.info.ScenarioInfo;
import adf.core.agent.info.WorldInfo;
import adf.core.agent.module.ModuleManager;
import adf.core.component.centralized.CommandPicker;
import adf.core.component.communication.CommunicationMessage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import rescuecore2.standard.entities.Area;
import rescuecore2.standard.entities.Human;
import rescuecore2.standard.entities.StandardEntity;
import rescuecore2.standard.entities.StandardEntityURN;
import rescuecore2.worldmodel.EntityID;

public class CentralizedControlCommandPickerFire extends CommandPicker {

  private int scoutDistance;

  private Collection<CommunicationMessage> messages;
  private Map<EntityID, EntityID> allocationData;

  public CentralizedControlCommandPickerFire(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) {
    super(ai, wi, si, moduleManager, developData);
    this.messages = new ArrayList<>();
    this.allocationData = null;
    this.scoutDistance = developData.getInteger(
        "adf.impl.centralized.DefaultCommandPickerFire.scoutDistance", 40000);
  }

  /**
   * TargetAllocatorの計算結果を入力するためのメソッド
   * 
   * @param allocationData TargetAllocatorのgetResultメソッドの戻り値
   */
  @Override
  public CommandPicker
      setAllocatorResult(Map<EntityID, EntityID> allocationData) {
    this.allocationData = allocationData;
    return this;
  }

  /**
   * 司令を生成・リスト化するためのメソッド
   * 司令はTargetAllocatorから受け取った司令内容をもとに生成される
   */
  @Override
  public CommandPicker calc() {
    this.messages.clear();
    if (this.allocationData == null) {
      return this;
    }
    for (EntityID agentID : this.allocationData.keySet()) {
      StandardEntity agent = this.worldInfo.getEntity(agentID);
      if (agent != null
          && agent.getStandardURN() == StandardEntityURN.FIRE_BRIGADE) {
        StandardEntity target = this.worldInfo.getEntity(this.allocationData.get(agentID));
        if (target != null) {
          
          if (target instanceof Human) {
            CommandFire command = new CommandFire(true, agentID, target.getID(), CommandFire.ACTION_AUTONOMY);
            this.messages.add(command);
          }
        }
      }
    }
    return this;
  }

  /**
   * calcメソッドで生成した司令リストを取得するためのメソッド
   * @return 司令リスト
   */
  @Override
  public Collection<CommunicationMessage> getResult() {
    return this.messages;
  }
}