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

import random
import time


########################################################################################### Jeu 5 : Risk - Gneezy Potters- Questionnaire compréhension
class L_risk11_qc(Page):
    form_model = 'player'
    form_fields = ['risk11_qc_a', 'risk11_qc_b', 'risk11_qc_c']

    def is_displayed(self):
        return self.player.id_in_group % 2 ==0 and self.participant.vars['versionexp'] == 1

    def vars_for_template(self):
        self.player.task_start_19 = time.time()  ###### get_time

        return {
            'page': self.subsession.round_number + 10,
        }


    def error_message(self, values):
        print('values is', values)
        if values['risk11_qc_a'] != 2:
            return 'Merci de corriger votre réponse 1 !!'
        elif values['risk11_qc_b'] != 4:
            return 'Merci de corriger votre réponse 2 !!'
        elif values['risk11_qc_c'] != 1:
            return 'Merci de corriger votre réponse 3 !!'

    def before_next_page(self):
        self.player.task_time_19 = time.time() - self.player.task_start_19     ######### count_time
        self.player.treatment = self.player.participant.vars['treatment']
        self.player.id_in_group_nn = self.player.participant.vars['role_nn']

class M_risk11(Page):
    #    timeout_seconds = 1  # WB

    form_model = 'player'
    form_fields = ['risk11_a', 'risk11_b']

    def is_displayed(self):
        return self.player.id_in_group % 2 == 0 and self.participant.vars['versionexp'] == 1

    def vars_for_template(self):
        self.player.task_start_20 = time.time()  ###### get_time

        return {
            'page': self.subsession.round_number + 11,
        }

    def error_message(self, values):
        print('values is', values)
        if values['risk11_a'] + values['risk11_b'] != 10:
            return 'La somme doit être égale à 10 !!'

    def before_next_page(self):
        self.player.task_time_20 = time.time() - self.player.task_start_20     ######### count_time

        self.player.tiragerisk = round(random.random(), 2)  ##
        if self.player.tiragerisk < 0.5:
            self.player.gainrisk = (self.player.risk11_a * 3) + self.player.risk11_b
            self.player.tirageriskresult = "Succès"
        else:
            self.player.gainrisk = self.player.risk11_b
            self.player.tirageriskresult = "Echec"


        self.participant.vars['tirageriskresult_j1'] = self.player.tirageriskresult
        self.participant.vars['risk11_a_j1'] = self.player.risk11_a
        self.participant.vars['risk11_b_j1'] = self.player.risk11_b
        self.participant.vars['gainrisk_j1'] = self.player.gainrisk

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

    def is_displayed(self):
        return self.player.id_in_group % 2 == 0 and self.participant.vars['versionexp'] == 1


page_sequence = [
    L_risk11_qc,
    M_risk11,
#    M_risk11_result,
    Jeu1End,

]
