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 is_displayed(self):
        return self.participant.vars['versionexp'] == 1


    def before_next_page(self):
        self.player.treatment = self.player.participant.vars['treatment']
        self.player.id_in_group_nn = self.player.participant.vars['role_nn']

        if self.player.treatment == 1:
            self.player.treatmentname ='GP - BW'
        elif self.player.treatment == 2:
            self.player.treatmentname ='BW - GP'


        ###########################

        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_age',
                   'q_43_nationalite', 'q_44_revenu',
                   'q3_countries', 'q_46_logement_ville', 'q_31',
                   'q_47_residence', 'q_48_satisfaction', 'q_49_propositions'
                   ]
    def is_displayed(self):
        return self.participant.vars['versionexp'] == 1

    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.treatment == 1 :
            if self.player.tirageausort < 0.5:
                self.player.gainfinal = self.player.gainrisk_j1  ############ Jeu 1
                self.player.jeu_tire = 1
                self.player.jeuname = "GP"
            else:
                self.player.gainfinal = self.player.gainriskeur  ############ Jeu 2
                self.player.jeu_tire = 2
                self.player.jeuname = "BW"

        if self.player.treatment == 2 :
            if self.player.tirageausort < 0.5:
                self.player.gainfinal = self.player.gainriskeur  ############ Jeu 2
                self.player.jeu_tire = 1
                self.player.jeuname = "BW"
            else:
                self.player.gainfinal = self.player.gainrisk_j1   ############ Jeu 1
                self.player.jeu_tire = 2
                self.player.jeuname = "GP"


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

        self.player.gaineur = round(self.player.gainfinal * Constants.tauxchange,1)

        self.player.gainfinaleur = round(self.player.gaineur + Constants.showupfee,1)

        if  (self.player.gainfinaleur * 100) % 100 <= 50 and (self.player.gainfinaleur * 100) % 100  > 0 :
            self.player.gainfinaleur_v1   = ((round(((self.player.gainfinaleur * 100) - ((self.player.gainfinaleur * 100) % 100)), 0) + 50) / 100) * 1000
        else:
            self.player.gainfinaleur_v1   = (round((self.player.gainfinaleur), 0))*1000

        self.player.payoff = self.player.gainfinaleur_v1

class Result(Page):
    form_model = 'player'
    form_fields = [
        'lastname',
        'firstname',
        'email',
        'room',
    ]
    def is_displayed(self):
        return self.participant.vars['versionexp'] == 1

    def before_next_page(self):
        self.player.participant.vars['treatmentname'] = self.player.treatmentname
        self.player.participant.vars['jeu_tire'] = self.player.jeu_tire
        self.player.participant.vars['jeuname'] = self.player.jeuname
        self.player.participant.vars['gainfinal'] = self.player.gainfinal
        self.player.participant.vars['gaineur'] = self.player.gaineur
        self.player.participant.vars['gainfinaleur'] = self.player.gainfinaleur
        self.player.participant.vars['lastname'] = self.player.lastname
        self.player.participant.vars['firstname'] = self.player.firstname
        self.player.participant.vars['email'] = self.player.email
        self.player.participant.vars['room'] = self.player.room






page_sequence = [
    Risk_BinsWanger_color,
    Questionnaire4,
    Result,


]
