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

import random
import time


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

    def vars_for_template(self):
        pass

    def before_next_page(self):
        self.player.choicerisk = self.participant.vars['choicerisk']
        self.player.choicerisknn = self.participant.vars['choicerisknn']
        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 Questionnaire4(Page):
    form_model = 'player'
    form_fields = ['q_40_etude', 'q_41_gender', 'q_42_annee_naissance', 'q_43_nationalite', 'q_44_revenu',
                   'q_45_logement_pays', 'q_46_logement_ville']

    def before_next_page(self):
        ### jeu 2 : risk Binswanger
        self.player.choicerisknn = self.participant.vars['choicerisknn']
        self.player.colorselectnn = self.participant.vars['colorselectnn']
        self.player.gainrisk = self.participant.vars['gainrisk'] ###
        self.player.gainriskeur = self.participant.vars['gainriskeur'] ####

        ### jeu 1 : Gneezy_Potters
        self.player.tirageriskresult_j1 = self.participant.vars['tirageriskresult_j1']
        self.player.risk11_a_j1 = self.participant.vars['risk11_a_j1']
        self.player.risk11_b_j1 = self.participant.vars['risk11_b_j1']
        self.player.gainrisk_j1 = self.participant.vars['gainrisk_j1']


        self.player.tirageausort = round(random.random(), 2)

        if self.player.id_in_group == 1 :
            if self.player.tirageausort < 0.5:
                self.player.gainfinal = self.player.gainrisk_j1  ############ Jeu 1
                self.player.jeu_tire = 1
            else:
                self.player.gainfinal = self.player.gainriskeur  ############ Jeu 2
                self.player.jeu_tire = 2

        if self.player.id_in_group == 2 :
            if self.player.tirageausort < 0.5:
                self.player.gainfinal = self.player.gainriskeur  ############ Jeu 2
                self.player.jeu_tire = 1
            else:
                self.player.gainfinal = self.player.gainrisk_j1   ############ Jeu 1
                self.player.jeu_tire = 2


        self.player.gainfinalplushowup = self.player.gainfinal + Constants.showupfee

        self.player.payoff = self.player.gainfinal + Constants.showupfee


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


page_sequence = [
    Risk_BinsWanger_color,
    Questionnaire4,
    Result,


]
