package autumn_2023.module.comm.infomation;

import adf.core.agent.communication.standard.bundle.StandardMessage;
import adf.core.agent.communication.standard.bundle.StandardMessagePriority;
import adf.core.component.communication.util.BitOutputStream;
import adf.core.component.communication.util.BitStreamReader;
import rescuecore2.worldmodel.EntityID;

public class MessageCost extends StandardMessage{
    // int型の容量(32bit:4byte)
    public static final int SIZE_COST = 32;
    public static final int SIZE_TARGET = 32;

    private final EntityID targetId;
    private final int cost;

    public MessageCost(boolean isRadio, EntityID target, int cost) 
    {
        super(isRadio, StandardMessagePriority.NORMAL);
        this.targetId = target;
        this.cost = cost;
    }

    public MessageCost(boolean isRadio, StandardMessagePriority sendingPriority, EntityID target, int cost)
    {
        super(isRadio, sendingPriority);
        this.targetId = target;
        this.cost = cost;
    }

    public MessageCost(boolean isRadio, int from, int ttl, BitStreamReader bitStreamReader)
    {
        super(isRadio, from, ttl, bitStreamReader);
        this.targetId = new EntityID(bitStreamReader.getBits(SIZE_TARGET));
        this.cost = bitStreamReader.getBits(SIZE_COST);
    }

    public EntityID getTargetID() 
    {
        return targetId;
    }

    public int cost() 
    {
        return cost;
    }

    @Override
    public int getByteArraySize() 
    {
        return this.toBitOutputStream().size();
    }

    @Override
    public byte[] toByteArray() 
    {
        return this.toBitOutputStream().toByteArray();
    }

    @Override
    public BitOutputStream toBitOutputStream() 
    {
        final BitOutputStream bitOutputStream = new BitOutputStream();
        bitOutputStream.writeBits(this.targetId.getValue(), SIZE_TARGET);
        bitOutputStream.writeBits(this.cost, SIZE_COST);
        return bitOutputStream;
    }

    @Override
    public String getCheckKey() 
    {
        return String.format("TargetID : " + this.targetId.getValue() + " Cost : " + this.cost);
    }

}
