import json
from DecisionSystem.messages import ConnectSwarm, SubmitVote, Message, deserialise, RequestVote
from multiprocessing import Pool
from messenger import Messenger

class BallotVoter:
    def __init__(self, on_vote, messenger: Messenger):

        # Load config file
        cfg = None
        with open('config.json') as json_config:
            cfg = json.load(json_config)
        self._cfg = cfg

        self.messenger = messenger
        self.messenger.add_message_callback(self.on_message)
        self.messenger.add_connect(self.on_connect)
        self.on_vote = on_vote

    def on_connect(self, rc):
        print("Connected with result code " + str(rc))

        # Should subscribes be handled by the messenger?
        #self.client.subscribe('swarm1/voters', qos=1)

        # Need to set a will as well to broadcast on unexpected disconnection, so commander
        # knows it is no longer part of the set of voters. 
        # Leaving this until core centralised system is working.
        #will_message = {"type": "UDisconnect"}

        # Tell commander we are now connected.
        self.send_connect()

    def on_message(self, message):
        print("Message Received!")
        messageD = deserialise(message.payload)
        print("Message Type: " + messageD.type)
        # Ok message.
        if messageD.type == RequestVote().type:
            print('Received vote message')
            self.submit_vote()
        elif messageD.type == "listening":
            self.send_connect()
        elif messageD.type == "disconnectedcommander":
            # Elect new leader...
            # Might just use raft's method for electing a leader, 
            # by doing a random timeout then choosing itself as the candidate,
            # and collects who is part of the swarm. This may need to be another class...
            pass

    def submit_vote(self):
        v = self.on_vote()
        if v == None:
            print('Could not get vote')
            return
        print("Got Vote")
        vote = SubmitVote(v, self.messenger.id)
        print('Created Vote Message')
        self.messenger.broadcast_message(self.messenger.swarm, vote.serialise())
        print('published vote')

    def submit_vote_multicore(self):
        '''
        Uses multiple processes to work on multiple frames at the same time
        to take an average for this voter, or if it could not find a hand
        in one frame but can in another.
        '''
        pass

    def send_connect(self):
        # Send a connected message to let any commanders know that 
        # it is available. 
        self.messenger.broadcast_message(self.messenger.swarm, ConnectSwarm(self.messenger.id).serialise())