from otree.api import Currency as c, currency_range
from ._builtin import Page, WaitPage
from .models import Constants
import random

class Introduction(Page):
    form_model = 'player'
    form_fields = ['r1','r2','r3','r4']

    def is_displayed(self):
        return self.round_number == 1

    def error_message(self, values):
        print('values is', values)
        if values['r1'] != 98:
            return 'Merci de corriger votre réponse 1 !!'
        elif values['r2'] != 2:
            return 'Merci de corriger votre réponse 2 !!'
        elif values['r3'] != 5:
            return 'Merci de corriger votre réponse 3 !!'
        elif values['r4'] != 110:
            return 'Merci de corriger votre réponse 4 !!'

    def before_next_page(self):
        if self.player.id_in_group  == 1:
            self.player.role1 = 'dictator'
            self.player.dictator = 1
        else:
            self.player.role1 = 'receiver'
            self.player.dictator = 0
        self.player.participant.vars['role1'] = self.player.role1
        self.player.participant.vars['dictator'] = self.player.dictator
        if self.round_number >1:
            self.player.role1 = self.player.participant.vars['role1']


class InstructionsWaitPage(WaitPage):
    after_all_players_arrive = 'dict_dgm'

class Send(Page):
    """comment
    """
    form_model = 'player'
    form_fields = ['keep_amount', 'sent_amount']

    # def is_displayed(self):
    #     return self.player.id_in_group == 1

    def vars_for_template(player):
        group = player.group
        return dict(
            dot1 = group.dot1,
            s1 = group.s1,
            g1 = group.g1,
            round = group.round_number,
         )

    def before_next_page(self):
        if self.round_number >1:
            self.player.role1 = self.player.participant.vars['role1']
            self.player.dictator = self.player.participant.vars['dictator']
        # if  self.round_number == 1:
        #     self.player.participant.vars['dot1'] = self.group.dot1
        #     self.player.participant.vars['s1'] = self.group.s1
        #     self.player.participant.vars['g1'] = self.group.g1
        #     self.player.participant.vars['keep1'] = self.group.keep1
        #     self.player.participant.vars['send1'] = self.group.send1
        #     self.player.participant.vars['keep2'] = self.group.keep2
        #     self.player.participant.vars['send2'] = self.group.send2



class ResultsWaitPage(WaitPage):
    after_all_players_arrive = 'set_payoffs'

class Results(Page):
    def is_displayed(self):
        return self.round_number == 1


page_sequence = [
    Introduction,
    InstructionsWaitPage,
    Send,
    ResultsWaitPage,
    Results,
]


