from otree.api import (
    models,
    widgets,
    BaseConstants,
    BaseSubsession,
    BaseGroup,
    BasePlayer,
    Currency as c,
    currency_range,
)
import random


doc = """
Modified dictator game
"""


class Constants(BaseConstants):
    name_in_url = 'dictator_modif'
    players_per_group = 2
    num_rounds        = 8

    instructions_template = 'dictator_modif/instructions.html'

    # Initial amount allocated to each player
    endowment = c(100)
    multiplier = 3

    dgm1 = {0: [40, 3, 1], 1: [40, 1, 3], 2: [60, 2, 1], 3: [60, 1, 2],
           4: [75, 2, 1], 5: [75, 1, 2], 6: [60, 1, 1], 7: [100, 1, 1]}

    dgm2 = {7: [40, 3, 1], 6: [40, 1, 3], 5: [60, 2, 1], 4: [60, 1, 2],
           3: [75, 2, 1], 2: [75, 1, 2], 1: [60, 1, 1], 0: [100, 1, 1]}

    dgm3 = {0: [40, 3, 1], 2: [40, 1, 3], 1: [60, 2, 1], 4: [60, 1, 2],
           3: [75, 2, 1], 6: [75, 1, 2], 5: [60, 1, 1], 7: [100, 1, 1]}

    dgm4 = {5: [40, 3, 1], 6: [40, 1, 3], 7: [60, 2, 1], 0: [60, 1, 2],
           1: [75, 2, 1], 2: [75, 1, 2], 3: [60, 1, 1], 4: [100, 1, 1]}

    dgm5 = {4: [40, 3, 1], 1: [40, 1, 3], 6: [60, 2, 1], 3: [60, 1, 2],
           0: [75, 2, 1], 5: [75, 1, 2], 2: [60, 1, 1], 7: [100, 1, 1]}



class Subsession(BaseSubsession):
    def creating_session(subsession):
        # Random Matching
        subsession.group_randomly()

class Group(BaseGroup):
    dot1          = models.IntegerField()
    s1            = models.IntegerField()
    g1            = models.IntegerField()
    send1         = models.IntegerField()
    send2         = models.IntegerField()
    keep1         = models.IntegerField()
    keep2         = models.IntegerField()
    roundselect   = models.IntegerField()
    def dict_dgm(self):
        if self.id_in_subsession == 1 or self.id_in_subsession == 6 or self.id_in_subsession == 11:
            self.dot1 = Constants.dgm1[self.round_number-1][0]
            self.s1 = Constants.dgm1[self.round_number-1][1]
            self.g1 = Constants.dgm1[self.round_number-1][2]
        elif self.id_in_subsession == 2 or self.id_in_subsession == 7 or self.id_in_subsession == 12:
            self.dot1 = Constants.dgm2[self.round_number-1][0]
            self.s1 = Constants.dgm2[self.round_number-1][1]
            self.g1 = Constants.dgm2[self.round_number-1][2]
        elif self.id_in_subsession == 3 or self.id_in_subsession == 8 or self.id_in_subsession == 13:
            self.dot1 = Constants.dgm3[self.round_number-1][0]
            self.s1 = Constants.dgm3[self.round_number-1][1]
            self.g1 = Constants.dgm3[self.round_number-1][2]
        elif self.id_in_subsession == 4 or self.id_in_subsession == 9 or self.id_in_subsession == 14:
            self.dot1 = Constants.dgm4[self.round_number-1][0]
            self.s1 = Constants.dgm4[self.round_number-1][1]
            self.g1 = Constants.dgm4[self.round_number-1][2]
        elif self.id_in_subsession == 5 or self.id_in_subsession == 10 or self.id_in_subsession == 15:
            self.dot1 = Constants.dgm5[self.round_number-1][0]
            self.s1 = Constants.dgm5[self.round_number-1][1]
            self.g1 = Constants.dgm5[self.round_number-1][2]
    sent_amount = models.CurrencyField(
        min=0, max=Constants.endowment, doc="""Amount sent by P1"""
    )

    sent_back_amount = models.CurrencyField(doc="""Amount sent back by P2""", min=c(0))

    def sent_back_amount_max(self):
        return self.sent_amount * Constants.multiplier

    def set_payoffs(self):
        p1 = self.get_player_by_id(1)
        p2 = self.get_player_by_id(2)

        self.send1 = p1.sent_amount
        self.send2 = p2.sent_amount
        self.keep1 = p1.keep_amount
        self.keep2 = p2.keep_amount

        if self.round_number == 8:
            self.roundselect = random.randint(1, 8)
            if p1.dictator == 1:
                p1.gain_dgm = 23
                p2.gain_dgm = 25
            else:
                p1.gain_dgm = 233
                p2.gain_dgm = 255


        # p1.payoff = self.keep1 * self.s1
        # p2.payoff = self.send1 * self.g1
        #
        # p1.payoff = Constants.endowment - self.sent_amount + self.sent_back_amount
        # p2.payoff = self.sent_amount * Constants.multiplier - self.sent_back_amount

class Player(BasePlayer):
    def role(self):
        if self.id_in_group == 1:
            return 'dictator1'
        if self.id_in_group == 2:
            return 'dictator2'
    role1 = models.StringField()
    dictator = models.IntegerField()
    r1 = models.IntegerField(
        blank=False
    )
    r2 = models.IntegerField(
        blank=False
    )
    r3 = models.IntegerField(
        blank=False
    )
    r4 = models.IntegerField(
        blank=False
    )
    keep_amount = models.IntegerField(
        blank=False, doc="""Vous gardez""" )
    sent_amount = models.IntegerField(
        blank=False, doc="""Vous donnez""")
    gain_dgm = models.IntegerField()



