from Messaging.messaginginterface import Message
import umsgpack

# Uses MessagePack for message transfer.

class Proposal(Message):
    ballot_number = None
    vote = None

    def __init__(self):
        pass

    def serialise(self) -> str:
        return umsgpack.packb({"ballot": self.ballot_number, "vote": self.vote})

    def deserialise(self, binary: bytearray):
        '''
        Deserialises a given proposal message into a proposal object.
        '''
        obj = umsgpack.unpackb(binary)
        old_ballot = self.ballot_number
        old_vote = self.vote
        try:
            self.ballot_number = obj['ballot']
            self.vote = obj['vote']
        except:
            # Reset the values in case they have been changed.
            self.ballot_number = old_ballot
            self.vote = old_vote
            print("Error: binary object is not a proposal")

    