import random
from candidates import *

class voter:
    agent_name = None
    ballot = None
    cands = None
    cutoff = None

    def __init__(self, agent_name=None, cands=None, ballot=None, cutoff=None):
        """
        Initializes a new ballot
        """

        self.ballot = []

        if agent_name != None and cands != None and ballot != None and cutoff != None:
            self.agent_name = agent_name
            self.cands = cands
            self.ballot = ballot
            self.cutoff = cutoff
            return

        if agent_name == None:
            self.agent_name = "v%i" % random.randint(100,999)
        else: 
            self.agent_name = agent_name

        if cands == None:
            self.cands = candidates()
        else:
            self.cands = cands
        if cutoff == None:
            #cutoff is either after the first, somewhere inbetween or before the last
            self.cutoff = random.randint(1,len(self.cands.candidates)-1)
        else:
            self.cutoff = cutoff
        if ballot == None:
            for c in self.cands.candidates:
                r = random.randint(0,100000)
                self.ballot.append((r,c))
            return
        else:
            self.ballot = ballot
        return

    def __str__(self):
        """
        Returns a string representation of the voter
        """
        return "Name:%s Cut:%i Ballot:%s" % (self.agent_name, self.cutoff, self.ballot)

    def rank(self):
        self.ballot.sort(reverse=True)
        return self.ballot

    def collapse(self):
        self.rank()
        length = len(self.ballot)
        temp = []
        for (i,j) in self.ballot:
            temp.append((length,j))
            length = length-1
        self.ballot = temp
        return self.ballot
    
    def untup(self):
        b = []
        for (i,j) in self.ballot:
            b.append(j)
        self.ballot = b
        return self.ballot

