/*
ECE649 Spring 2010
Group 7
Justin Ray/justinr2
(other names would go here)
 */

package simulator.elevatorcontrol;

import java.util.BitSet;
import simulator.framework.ReplicationComputer;
import simulator.payloads.CanMailbox;
import simulator.payloads.translators.CanPayloadTranslator;


/************************************
CLASS:
CoinCountCanPayloadTranslator
AUTHOR:
Justin Ray
REVISION DATE:
CONSTRUCTOR PARAMETERS:
CanMailbox payload
DESCRIPTION:
CanPayloadTranslators convert between bit-level message representations used in
the CAN network simulation and semantically valid typed representations (e.g.
boolean, int, enum types, etc) used by the Controller classes.

Translators are a convenience feature, but they are also required because they
are used by the MessageInjector to inject network messages into unit and
integration tests.

This controller has an index parameter that refers to which chute the message
corresponds to.

This translator is used by the CAN network simulation framework for the
mButton message
 *************************************/
public class ButtonCanPayloadTranslator extends CanPayloadTranslator {

    private final int index;

    public ButtonCanPayloadTranslator(CanMailbox payload, int index) {
        super(payload, 1, MessageDictionary.BUTTON_BASE_CAN_ID + ReplicationComputer.computeReplicationId(index));
        this.index = index;
    }

    //this method is required for reflection
    public void set(boolean vend) {
        setButtonState(vend);
    }

    public void setButtonState(boolean vend) {
        BitSet b = mailbox.getMessagePayload();
        b.set(0, vend);
        mailbox.setMessagePayload(b, byteSize);
    }

    public boolean getButtonState() {
        return mailbox.getMessagePayload().get(0);
    }

    @Override
    public String payloadToString() {
        return "Button[" + index + "]=" + getButtonState();
    }

}
