from otree.api import Currency as c, currency_range
from . import pages
from ._builtin import Bot
from .models import Constants
from random import choice
from . import models


class PlayerBot(Bot):

    def play_round(self):
        if self.subsession.round_number == 1:
            yield (pages.Screen2Page)
            yield (pages.Screen4Page)
            yield (pages.Screen5Page)
            yield (pages.ComprehensionPage,
                   {
                       "bonus_diff5_question": "0 tokens",
                       "subcharity_diffanswer_question": "50 tokens",
                       "payment_period_question": "all of the 5",
                       "subcharity_additionally_question": "True"
                   })
        yield (pages.DrawPage, {"reported_number": choice([1, 2, 3, 4, 5, 6])})
        # Check
        if self.player.punishment_rule == "15_tokens":
            # Compute payoff
            temp2 = 0
            if self.player.reported_number == 5:
                if self.player.random_number == 5:
                    assert (self.player.payoff == c(30))
                elif self.player.random_number != 5:
                    assert (self.player.payoff == c(15))
            else:
                if self.player.reported_number != self.player.random_number:
                    assert (self.player.payoff == c(-15))
                else:
                    assert (self.player.payoff == c(0))
            if self.player.reported_number != self.player.random_number:
                temp2 -= 15
            assert(self.player.charity_account == temp2)
        elif self.player.punishment_rule == "12or15or18_tokens":
            # Compute payoff possibilities
            temp2 = 0
            if self.player.reported_number == 5:
                if self.player.random_number == 5:
                    assert (self.player.payoff == c(30))
                elif self.player.random_number != 5:
                    assert (self.player.payoff in [18, 15, 12])
            else:
                if self.player.reported_number != self.player.random_number:
                    assert (self.player.payoff in [-18, -15, -12])
                else:
                    assert (self.player.payoff == c(0))
            if self.player.reported_number != self.player.random_number:
                temp2 -= 15
            assert(self.player.charity_account == temp2)
        elif self.player.punishment_rule == "9or12or15or18or21_tokens":
            # Compute payoff possibilities
            temp2 = 0
            if self.player.reported_number == 5:
                if self.player.random_number == 5:
                    assert (self.player.payoff == c(30))
                elif self.player.random_number != 5:
                    assert (self.player.payoff in [21, 18, 15, 12, 9])
            else:
                if self.player.reported_number != self.player.random_number:
                    assert (self.player.payoff in [-21, -18, -15, -12, -9])
                else:
                    assert (self.player.payoff == c(0))
            if self.player.reported_number != self.player.random_number:
                temp2 -= 15
            assert(self.player.charity_account == temp2)
        elif self.player.punishment_rule == "6or9or12or15or18or21or24_tokens":
            # Compute payoff possibilities
            temp2 = 0
            if self.player.reported_number == 5:
                if self.player.random_number == 5:
                    assert (self.player.payoff == c(30))
                elif self.player.random_number != 5:
                    assert (self.player.payoff in [24, 21, 18, 15, 12, 9, 6])
            else:
                if self.player.reported_number != self.player.random_number:
                    assert (self.player.payoff in [-24, -21, -18, -15, -12, -9, -6])
                else:
                    assert (self.player.payoff == c(0))
            if self.player.reported_number != self.player.random_number:
                temp2 -= 15
            assert(self.player.charity_account == temp2)
        elif self.player.punishment_rule == "12or15or18_or_6or9or12or15or18or21or24_tokens":
            # Compute payoff possibilities
            temp2 = 0
            if self.player.reported_number == 5:
                if self.player.random_number == 5:
                    assert (self.player.payoff == c(30))
                elif self.player.random_number != 5:
                    assert (self.player.payoff in [24, 21, 18, 15, 12, 9, 6])
            else:
                if self.player.reported_number != self.player.random_number:
                    assert (self.player.payoff in [-24, -21, -18, -15, -12, -9, -6])
                else:
                    assert (self.player.payoff == c(0))
            if self.player.reported_number != self.player.random_number:
                temp2 -= 15
            assert(self.player.charity_account == temp2)

        if self.subsession.round_number == Constants.num_rounds:
            yield (pages.PayoffPage)
