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


author = 'Your name here'

doc = """
Your app description
"""


class Constants(BaseConstants):
    name_in_url = 'T1'
    players_per_group = None
    num_rounds = 1

    c_winning_number = 5
    c_player_endowment = c(100)
    c_charity_endowment = c(100)
    c_winning_payoff = c(50)
    c_charity_deduction = c(50)


class Subsession(BaseSubsession):
    def creating_session(self):
        for p in self.get_players():
            p.charity_account = Constants.c_charity_endowment
            p.payoff = Constants.c_player_endowment  # "In addition to your show-up fee, you start with an endowment of 100 tokens"
            p.random_number = randint(1, 6)


class Group(BaseGroup):
    pass


class Player(BasePlayer):
    failed_captcha = models.BooleanField(initial=False)
    roll3_report3_bonus_question = models.PositiveIntegerField(
        label="What is the bonus payment if you roll a 3 and report a 3 ? (enter the number in tokens)",
    )
    roll3_report5_bonus_question = models.PositiveIntegerField(
        label="What is the bonus payment if you roll a 3 and report a 5 ? (enter the number in tokens)",
    )
    roll5_report5_bonus_question = models.PositiveIntegerField(
        label="What is the bonus payment if you roll a 5 and report a 5 ? (enter the number in tokens)",
    )
    roll3_report3_charity_question = models.PositiveIntegerField(
        label="What amount will be subtracted from the charity if you roll a 3 and report a 3 ? "
              "(enter the number in tokens)",
    )
    roll3_report5_charity_question = models.PositiveIntegerField(
        label="What amount will be subtracted from the charity if you roll a 3 and report a 5 ? "
              "(enter the number in tokens)",
    )
    roll5_report5_charity_question = models.PositiveIntegerField(
        label="What amount will be subtracted from the charity if you roll a 5 and report a 5 ? "
              "(enter the number in tokens)",
    )
    charity_account = models.CurrencyField(initial=c(0))
    random_number = models.IntegerField(min=1, max=6)
    reported_number = models.IntegerField(verbose_name="", min=1, max=6)

    def compute_payoff(self):
        # Add the gain according to reported number
        if self.reported_number == Constants.c_winning_number:
            self.payoff += Constants.c_winning_payoff
        if self.reported_number != self.random_number:
            # Deduct the punishment to the charity account
            self.charity_account -= Constants.c_charity_deduction
