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

class Bienvenue(Page):
    form_model = 'player'
    form_fields = []

    def is_displayed(self):
        return self.round_number == 1 and  self.player.participant.vars['app_sequence_select'] % 2 == 1


    def before_next_page(self):
        self.player.game_num = self.player.participant.vars['game_num'] + 1
        self.player.participant.vars['game_num'] = self.player.game_num
        self.player.app_sequence_select = self.player.participant.vars['app_sequence_select']
        self.player.app_sequence_select_n = self.player.participant.vars['app_sequence_select_n']
        self.player.questionnaire_before = self.player.participant.vars['questionnaire_before']

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

    def is_displayed(self):
        return self.round_number == 1 and  self.player.participant.vars['app_sequence_select'] % 2 == 1


    def error_message(self, values):
        print('values is', values)
        if values['r1'] != 98:
            return 'Please correct your answer 1 !!'
        elif values['r2'] != 2:
            return 'Please correct your answer 2 !!'
        elif values['r3'] != 5:
            return 'Please correct your answer 3 !!'
        elif values['r4'] != 110:
            return 'Please correct your answer 4 !!'

    def before_next_page(self):
        # selection of random dotation/s/g and round to be paid
        self.player.dgm_selected   = random.randint(1, 5)
        self.player.round_selected = random.randint(1, 8)
        self.player.participant.vars['mdgclass_dgm_selected']   = self.player.dgm_selected
        self.player.participant.vars['mdgclass_round_selected'] = self.player.round_selected

class NextPage(Page):
    form_model = 'player'
    form_fields = []

    def is_displayed(self):
        return self.player.participant.vars['app_sequence_select'] % 2 == 1


    def vars_for_template(self):
        return dict(
            round = self.round_number,
         )


    def before_next_page(self):
        self.player.dgm_selected   =  self.player.participant.vars['mdgclass_dgm_selected']
        self.player.round_selected =self.player.participant.vars['mdgclass_round_selected']
        self.player.dot1 = getattr(Constants, f'dgm{self.player.dgm_selected}')[self.round_number][0]
        self.player.s1   = getattr(Constants, f'dgm{self.player.dgm_selected}')[self.round_number][1]
        self.player.g1   = getattr(Constants, f'dgm{self.player.dgm_selected}')[self.round_number][2]

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

    def is_displayed(self):
        return self.player.participant.vars['app_sequence_select'] % 2 == 1


    def error_message(self, values):
        print('values is', values)
        if values['keep_amount'] < 0:
            return 'the amount must be positive '
        elif values['keep_amount'] > self.player.dot1 :
            return f'please choose an amount less than {self.player.dot1}'
        elif values['sent_amount'] < 0:
            return 'the amount must be positive '
        elif values['sent_amount'] + values['keep_amount'] != self.player.dot1:
            return f'the sum of the two amounts must be equal to {self.player.dot1}'




    def vars_for_template(self):
        return dict(
            dot1 = self.player.dot1,
            s1 = self.player.s1,
            g1 = self.player.g1,
            round = self.round_number,
            game_num=self.player.participant.vars['game_num']

        )

    def before_next_page(self):
        self.player.participant.vars[f'dot1_r{self.round_number}'] = self.player.dot1
        self.player.participant.vars[f's1_r{self.round_number}'] = self.player.s1
        self.player.participant.vars[f'g1_r{self.round_number}'] = self.player.g1
        self.player.participant.vars[f'keep_r{self.round_number}'] = self.player.keep_amount
        self.player.participant.vars[f'sent_r{self.round_number}'] = self.player.sent_amount

        if self.round_number == 8:
            self.player.dot1_select   = self.player.participant.vars[f'dot1_r{self.player.round_selected}']
            self.player.s1_select     = self.player.participant.vars[f's1_r{self.player.round_selected}']
            self.player.g1_select     = self.player.participant.vars[f'g1_r{self.player.round_selected}']
            self.player.keep_select   = self.player.participant.vars[f'keep_r{self.player.round_selected}']
            self.player.sent_select   = self.player.participant.vars[f'sent_r{self.player.round_selected}']
            self.player.gain_dgm      = self.player.keep_select * self.player.s1_select
            self.player.gain_joueur2     = self.player.sent_select * self.player.g1_select
            self.player.gain_dgm_eur     = round(self.player.gain_dgm * Constants.exchangerate,1)
            self.player.gain_joueur2_eur = round(self.player.gain_joueur2 * Constants.exchangerate,1)
            self.player.payoff           = round(self.player.gain_dgm_eur,1)

            # to final page:
            self.player.participant.vars['mdgclass_dot1_select'] = self.player.dot1_select
            self.player.participant.vars['mdgclass_s1_select'] = self.player.s1_select
            self.player.participant.vars['mdgclass_g1_select'] = self.player.g1_select
            self.player.participant.vars['mdgclass_keep_select'] = self.player.keep_select
            self.player.participant.vars['mdgclass_sent_select'] = self.player.sent_select
            self.player.participant.vars['mdgclass_gain_dgm'] = self.player.gain_dgm
            self.player.participant.vars['mdgclass_gain_joueur2'] = self.player.gain_joueur2
            self.player.participant.vars['mdgclass_gain_dgm_eur'] = self.player.gain_dgm_eur
            self.player.participant.vars['mdgclass_gain_joueur2_eur'] = self.player.gain_joueur2_eur

class Results(Page):
    def is_displayed(self):
        return self.round_number == 8 and  self.player.participant.vars['app_sequence_select'] % 2 == 1


    def vars_for_template(self):
        #group = player.group
        return dict(
            dot1_select    = self.player.dot1_select,
            s1_select      = self.player.s1_select,
            g1_select      = self.player.g1_select,
            round          = self.round_number,
            round_selected = self.player.round_selected,
            keep_select    = self.player.keep_select,
            sent_select    = self.player.sent_select,
            gain_dgm       = self.player.gain_dgm,
            gain_joueur2   = self.player.gain_joueur2,
            gain_dgm_eur   = self.player.gain_dgm_eur,
            gain_joueur2_eur  = self.player.gain_joueur2_eur,

        )

page_sequence = [
    Bienvenue,
    Introduction,
    NextPage,
    Send,
]


