from default_model import DefaultModel
from probs_from_game import TrainTable, approval

translate = {
    "Merlin": "merlin",
    "Morgana": "morgana",
    "Oberon": "oberon",
    "Mordred": "mordred",
    "Percival": "percival",
    "GLance": "good",
    "G": "good",
    "ELance": "assassin"
}

class DataModel(DefaultModel):
    def __init__(self, game):
        super(DataModel, self).__init__(game)
        self.table = TrainTable("models/dataset.json")

    #def mission(self, team, fails, must_fail, rnd):
        #pass

    def votes(self, team, votes, fail_req, rnd, voten, proposer):
        total = 1.0
        n_actually_good_people = sum(
            [int(self.is_good(x, rnd)) for x in team])
        n_spies = len(team) - n_actually_good_people

        prole = self.player_role(proposer)
        for player, vote in enumerate(votes):
            role = self.player_role(player)
            if role == "Merlin":
                m_spies = n_spies
                roles = [self.player_role(x) for x in team]
                if "Mordred" in roles:
                    m_spies -= 1
                data = self.table.MerlinVote(rnd + 1, voten + 1,
                                             proposer_role=translate[prole],
                                             evil_count=n_spies,
                                             known_evil=m_spies,
                                             size=len(votes))
            elif role == "Percival":
                roles = [self.player_role(x) for x in team]
                tc = ""
                if "merlin" in roles:
                    tc += "merlin"
                if "morgana" in roles:
                    tc += "morgana"
                if tc == "":
                    tc = "neither"
                data = self.table.PercivalVote(rnd + 1, voten + 1,
                                               proposer_role=translate[prole],
                                               evil_count=n_spies,
                                               team_contains=tc,
                                               size=len(votes))
            elif role == "G" or role == "GLance":
                data = self.table.GoodVote(rnd + 1, voten + 1,
                                           proposer_role=translate[prole],
                                           evil_count=n_spies,
                                           size=len(votes))
            elif role == "Mordred":
                data = self.table.MordredVote(rnd + 1, voten + 1,
                                              proposer_role=translate[prole],
                                              evil_count=n_spies,
                                              size=len(votes))
            elif role == "Morgana":
                data = self.table.MorganaVote(rnd + 1, voten + 1,
                                              proposer_role=translate[prole],
                                              evil_count=n_spies,
                                              size=len(votes))
            elif role == "Oberon":
                data = self.table.OberonVote(rnd + 1, voten + 1,
                                             proposer_role=translate[prole],
                                             evil_count=n_spies,
                                             size=len(votes))
            elif role == "ELance" or "E":
                data = self.table.EvilVote(rnd + 1, voten + 1,
                                           proposer_role=translate[prole],
                                           evil_count=n_spies,
                                           size=len(votes))
            else:
                continue
            app = approval(data)
            if vote == 1:
                total = total * app
            else:
                total = total * (1.0 - app)
        return total





