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

import random
import time


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'] < 3

    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 Risk_BinsWanger_qc(Page):
    form_model = 'player'
    form_fields = []

    def is_displayed(self):
        return self.player.participant.vars['app_sequence_select'] < 3 # 1 ou 2

    def before_next_page(self):
        self.player.tirage_color = random.randint(0, 1)
        if self.player.tirage_color == 0:
            self.player.tirage_color_n = 'red'
        else:
            self.player.tirage_color_n = 'green'


class Risk_BinsWanger_qc2(Page):
    form_model = 'player'
    form_fields = ['riskqc_a', 'riskqc_b', 'riskqc_c']

    def is_displayed(self):
        return self.player.participant.vars['app_sequence_select'] < 3 # 1 ou 2

    def error_message(self, values):
        print('values is', values)
        if values['riskqc_a'] != 1:
            return 'Please correct your answer 1!!'
        elif values['riskqc_b'] != 3:
            return 'Please correct your answer 2!!'
        elif values['riskqc_c'] != 1:
            return 'Please correct your answer 3!!'

class Risk_BinsWanger(Page):
    form_model = 'player'
    form_fields = ['choicerisk', 'risk_assessment_bw', 'confidence_level_bw', 'memory_recall_bw']

    def is_displayed(self):
        return self.player.participant.vars['app_sequence_select'] < 3 # 1 ou 2

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

    def before_next_page(self):

        if self.player.choicerisk == 1:
            self.player.choicerisknn = "A"
        elif self.player.choicerisk == 2:
            self.player.choicerisknn = "B"
        elif self.player.choicerisk == 3:
            self.player.choicerisknn = "C"
        elif self.player.choicerisk == 4:
            self.player.choicerisknn = "D"
        elif self.player.choicerisk == 5:
            self.player.choicerisknn = "E"
        elif self.player.choicerisk == 6:
            self.player.choicerisknn = "F"
        elif self.player.choicerisk == 7:
            self.player.choicerisknn = "G"
        elif self.player.choicerisk == 8:
            self.player.choicerisknn = "H"
        elif self.player.choicerisk == 9:
            self.player.choicerisknn = "I"

        self.player.task_time_risk = time.time() - self.player.task_start_risk  ######### count_time

        self.participant.vars['choicerisk'] = self.player.choicerisk
        self.participant.vars['choicerisknn'] = self.player.choicerisknn

        if self.player.tirage_color == 0:
            self.player.gain_bw = Constants.gainrisk_red[self.player.choicerisk-1]
        else:
            self.player.gain_bw = Constants.gainrisk_green[self.player.choicerisk-1]
        self.player.gain_bw_eur = round(self.player.gain_bw  * Constants.tauxchange,1)

        self.participant.vars['tirage_color_n'] = self.player.tirage_color_n
        self.participant.vars['gain_bw'] = self.player.gain_bw
        self.participant.vars['gain_bw_eur'] = self.player.gain_bw_eur


page_sequence = [
    Bienvenue,
    Risk_BinsWanger_qc,
    Risk_BinsWanger_qc2,
    Risk_BinsWanger,
]
