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

import random
import time


###### traitement 1 risk au début
class Risk_BinsWanger_qc(Page):
    form_model = 'player'
    form_fields = []

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

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

    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"
        else:
            self.player.choicerisknn = "E"

        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



#######################################################  elle n'est pas active !!! c'est le programme (exp_e4c_en_p5_questfin)
class Risk_BinsWanger_color(Page):
    form_model = 'player'
    form_fields = ['tc3']

    def vars_for_template(self):
        pass

    def before_next_page(self):

        ###########################
        self.participant.vars['list_modif'] = self.player.tc3.split(';;')
        self.player.tiragecolor = float(self.participant.vars['list_modif'][1])
        self.player.colorbutton = int(self.participant.vars['list_modif'][0])

        if self.player.colorbutton == 1:
            if self.player.tiragecolor >= 0.5:
                self.player.colorselect = 1
                self.player.colorselectnn = "Vert"

            elif self.player.tiragecolor < 0.5:
                self.player.colorselect = 0
                self.player.colorselectnn = "Rouge"

        elif self.player.colorbutton == 2:
            if self.player.tiragecolor <= 0.5:
                self.player.colorselect = 1
                self.player.colorselectnn = "Vert"

            elif self.player.tiragecolor > 0.5:
                self.player.colorselect = 0
                self.player.colorselectnn = "Rouge"

        if self.player.colorselect == 0:
            if self.player.choicerisk == 1:
                self.player.gainrisk = Constants.gainriskAred

            elif self.player.choicerisk == 2:
                self.player.gainrisk = Constants.gainriskBred

            elif self.player.choicerisk == 3:
                self.player.gainrisk = Constants.gainriskCred

            elif self.player.choicerisk == 4:
                self.player.gainrisk = Constants.gainriskDred

            elif self.player.choicerisk == 5:
                self.player.gainrisk = Constants.gainriskEred

        elif self.player.colorselect == 1:
            if self.player.choicerisk == 1:
                self.player.gainrisk = Constants.gainriskAgreen

            elif self.player.choicerisk == 2:
                self.player.gainrisk = Constants.gainriskBgreen

            elif self.player.choicerisk == 3:
                self.player.gainrisk = Constants.gainriskCgreen

            elif self.player.choicerisk == 4:
                self.player.gainrisk = Constants.gainriskDgreen

            elif self.player.choicerisk == 5:
                self.player.gainrisk = Constants.gainriskEgreen

        self.player.gainriskeur = self.player.gainrisk


#        self.player.payoff = self.player.gainriskeurfinal * 1000

        self.participant.vars['choicerisknn'] = self.player.choicerisknn
        self.participant.vars['colorselectnn'] = self.player.colorselectnn
        self.participant.vars['gainrisk'] = self.player.gainrisk
        self.participant.vars['gainriskeur'] = self.player.gainriskeur
#######################################################


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

class Jeu2End(Page):
    pass

page_sequence = [
    Risk_BinsWanger_qc,
    Risk_BinsWanger_qc2,
    Risk_BinsWanger,
    Jeu2End,
######    Risk_BinsWanger_color,
######    Risk_BinsWanger_result,


]
